Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
6.0 KiB

  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:image_picker/image_picker.dart';
  4. import '/components/custom_circle_avatar.dart';
  5. import '/components/file_picker.dart';
  6. import '/database/models/conversations.dart';
  7. class ConversationEditDetails extends StatefulWidget {
  8. final Function(String conversationName, File? conversationIcon) saveCallback;
  9. final Conversation? conversation;
  10. const ConversationEditDetails({
  11. Key? key,
  12. required this.saveCallback,
  13. this.conversation,
  14. }) : super(key: key);
  15. @override
  16. State<ConversationEditDetails> createState() => _ConversationEditDetails();
  17. }
  18. class _ConversationEditDetails extends State<ConversationEditDetails> {
  19. final _formKey = GlobalKey<FormState>();
  20. List<Conversation> conversations = [];
  21. TextEditingController conversationNameController = TextEditingController();
  22. File? conversationIcon;
  23. bool showFileSelector = false;
  24. @override
  25. void initState() {
  26. if (widget.conversation != null) {
  27. conversationNameController.text = widget.conversation!.name;
  28. conversationIcon = widget.conversation!.icon;
  29. }
  30. super.initState();
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. const TextStyle inputTextStyle = TextStyle(
  35. fontSize: 25,
  36. );
  37. final OutlineInputBorder inputBorderStyle = OutlineInputBorder(
  38. borderRadius: BorderRadius.circular(5),
  39. borderSide: const BorderSide(
  40. color: Colors.transparent,
  41. )
  42. );
  43. final ButtonStyle buttonStyle = ElevatedButton.styleFrom(
  44. padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 10),
  45. textStyle: TextStyle(
  46. fontSize: 15,
  47. fontWeight: FontWeight.bold,
  48. color: Theme.of(context).colorScheme.error,
  49. ),
  50. );
  51. return Scaffold(
  52. appBar: AppBar(
  53. elevation: 0,
  54. automaticallyImplyLeading: false,
  55. flexibleSpace: SafeArea(
  56. child: Container(
  57. padding: const EdgeInsets.only(right: 16),
  58. child: Row(
  59. children: <Widget>[
  60. IconButton(
  61. onPressed: (){
  62. Navigator.pop(context);
  63. },
  64. icon: const Icon(Icons.arrow_back),
  65. ),
  66. const SizedBox(width: 2,),
  67. Expanded(
  68. child: Column(
  69. crossAxisAlignment: CrossAxisAlignment.start,
  70. mainAxisAlignment: MainAxisAlignment.center,
  71. children: <Widget>[
  72. Text(
  73. widget.conversation != null ?
  74. widget.conversation!.name + ' Settings' :
  75. 'Add Conversation',
  76. style: const TextStyle(
  77. fontSize: 16,
  78. fontWeight: FontWeight.w600
  79. ),
  80. ),
  81. ],
  82. ),
  83. ),
  84. ],
  85. ),
  86. ),
  87. ),
  88. ),
  89. body: Center(
  90. child: Padding(
  91. padding: const EdgeInsets.only(
  92. top: 50,
  93. left: 25,
  94. right: 25,
  95. ),
  96. child: Form(
  97. key: _formKey,
  98. child: Column(
  99. children: [
  100. CustomCircleAvatar(
  101. icon: const Icon(Icons.people, size: 60),
  102. image: conversationIcon,
  103. radius: 50,
  104. editImageCallback: () {
  105. setState(() {
  106. showFileSelector = true;
  107. });
  108. },
  109. ),
  110. const SizedBox(height: 20),
  111. AnimatedSwitcher(
  112. duration: const Duration(milliseconds: 250),
  113. transitionBuilder: (Widget child, Animation<double> animation) {
  114. return SizeTransition(sizeFactor: animation, child: child);
  115. },
  116. child: showFileSelector ?
  117. Padding(
  118. padding: const EdgeInsets.only(bottom: 10),
  119. child: FilePicker(
  120. cameraHandle: (XFile image) {
  121. setState(() {
  122. conversationIcon = File(image.path);
  123. showFileSelector = false;
  124. });
  125. },
  126. galleryHandleSingle: (XFile image) async {
  127. setState(() {
  128. conversationIcon = File(image.path);
  129. showFileSelector = false;
  130. });
  131. },
  132. ),
  133. ) :
  134. const SizedBox(height: 10),
  135. ),
  136. TextFormField(
  137. controller: conversationNameController,
  138. textAlign: TextAlign.center,
  139. decoration: InputDecoration(
  140. hintText: 'Title',
  141. enabledBorder: inputBorderStyle,
  142. focusedBorder: inputBorderStyle,
  143. ),
  144. style: inputTextStyle,
  145. // The validator receives the text that the user has entered.
  146. validator: (value) {
  147. if (value == null || value.isEmpty) {
  148. return 'Add a title';
  149. }
  150. return null;
  151. },
  152. ),
  153. const SizedBox(height: 30),
  154. ElevatedButton(
  155. style: buttonStyle,
  156. onPressed: () {
  157. if (!_formKey.currentState!.validate()) {
  158. return;
  159. }
  160. widget.saveCallback(
  161. conversationNameController.text,
  162. conversationIcon,
  163. );
  164. },
  165. child: const Text('Save'),
  166. ),
  167. ],
  168. ),
  169. ),
  170. ),
  171. ),
  172. );
  173. }
  174. }