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.

190 lines
5.7 KiB

  1. import 'dart:io';
  2. import 'package:Capsule/components/file_picker.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:image_picker/image_picker.dart';
  5. import '/components/custom_circle_avatar.dart';
  6. import '/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. showFileSelector ?
  112. Padding(
  113. padding: const EdgeInsets.only(bottom: 10),
  114. child: FilePicker(
  115. cameraHandle: (XFile image) {
  116. setState(() {
  117. conversationIcon = File(image.path);
  118. showFileSelector = false;
  119. });
  120. },
  121. galleryHandleSingle: (XFile image) async {
  122. setState(() {
  123. conversationIcon = File(image.path);
  124. showFileSelector = false;
  125. });
  126. },
  127. ),
  128. ) :
  129. const SizedBox(height: 10),
  130. TextFormField(
  131. controller: conversationNameController,
  132. textAlign: TextAlign.center,
  133. decoration: InputDecoration(
  134. hintText: 'Title',
  135. enabledBorder: inputBorderStyle,
  136. focusedBorder: inputBorderStyle,
  137. ),
  138. style: inputTextStyle,
  139. // The validator receives the text that the user has entered.
  140. validator: (value) {
  141. if (value == null || value.isEmpty) {
  142. return 'Add a title';
  143. }
  144. return null;
  145. },
  146. ),
  147. const SizedBox(height: 30),
  148. ElevatedButton(
  149. style: buttonStyle,
  150. onPressed: () {
  151. if (!_formKey.currentState!.validate()) {
  152. return;
  153. }
  154. widget.saveCallback(
  155. conversationNameController.text,
  156. conversationIcon,
  157. );
  158. },
  159. child: const Text('Save'),
  160. ),
  161. ],
  162. ),
  163. ),
  164. ),
  165. ),
  166. );
  167. }
  168. }