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.

169 lines
4.9 KiB

  1. import 'dart:io';
  2. import 'package:Envelope/components/custom_title_bar.dart';
  3. import 'package:Envelope/components/flash_message.dart';
  4. import 'package:Envelope/models/friends.dart';
  5. import 'package:Envelope/utils/storage/conversations.dart';
  6. import 'package:flutter/material.dart';
  7. import '/models/conversations.dart';
  8. import '/views/main/conversation/edit_details.dart';
  9. import '/views/main/conversation/list_item.dart';
  10. import 'create_add_users.dart';
  11. import 'detail.dart';
  12. class ConversationList extends StatefulWidget {
  13. final List<Conversation> conversations;
  14. final List<Friend> friends;
  15. const ConversationList({
  16. Key? key,
  17. required this.conversations,
  18. required this.friends,
  19. }) : super(key: key);
  20. @override
  21. State<ConversationList> createState() => _ConversationListState();
  22. }
  23. class _ConversationListState extends State<ConversationList> {
  24. List<Conversation> conversations = [];
  25. List<Friend> friends = [];
  26. @override
  27. Widget build(BuildContext context) {
  28. return Scaffold(
  29. appBar: const CustomTitleBar(
  30. title: Text(
  31. 'Conversations',
  32. style: TextStyle(
  33. fontSize: 32,
  34. fontWeight: FontWeight.bold
  35. )
  36. ),
  37. showBack: false,
  38. backgroundColor: Colors.transparent,
  39. ),
  40. body: Padding(
  41. padding: const EdgeInsets.only(top: 16,left: 16,right: 16),
  42. child: SingleChildScrollView(
  43. child: Column(
  44. children: <Widget>[
  45. TextField(
  46. decoration: const InputDecoration(
  47. hintText: 'Search...',
  48. prefixIcon: Icon(
  49. Icons.search,
  50. size: 20
  51. ),
  52. ),
  53. onChanged: (value) => filterSearchResults(value.toLowerCase())
  54. ),
  55. list(),
  56. ],
  57. ),
  58. ),
  59. ),
  60. floatingActionButton: Padding(
  61. padding: const EdgeInsets.only(right: 10, bottom: 10),
  62. child: FloatingActionButton(
  63. onPressed: () {
  64. Navigator.of(context).push(
  65. MaterialPageRoute(builder: (context) => ConversationEditDetails(
  66. saveCallback: (String conversationName, File? file) {
  67. Navigator.of(context).push(
  68. MaterialPageRoute(builder: (context) => ConversationAddFriendsList(
  69. friends: friends,
  70. saveCallback: (List<Friend> friendsSelected) async {
  71. Conversation conversation = await createConversation(
  72. conversationName,
  73. friendsSelected,
  74. false,
  75. );
  76. uploadConversation(conversation)
  77. .catchError((dynamic) {
  78. showMessage('Failed to create conversation', context);
  79. });
  80. Navigator.of(context).popUntil((route) => route.isFirst);
  81. Navigator.push(context, MaterialPageRoute(builder: (context) {
  82. return ConversationDetail(
  83. conversation: conversation,
  84. );
  85. }));
  86. },
  87. ))
  88. );
  89. },
  90. )),
  91. ).then(onGoBack);
  92. },
  93. backgroundColor: Theme.of(context).colorScheme.primary,
  94. child: Icon(
  95. Icons.add,
  96. size: 30,
  97. color: Theme.of(context).colorScheme.onPrimary,
  98. ),
  99. ),
  100. ),
  101. );
  102. }
  103. void filterSearchResults(String query) {
  104. List<Conversation> dummySearchList = [];
  105. dummySearchList.addAll(widget.conversations);
  106. if(query.isNotEmpty) {
  107. List<Conversation> dummyListData = [];
  108. for (Conversation item in dummySearchList) {
  109. if (item.name.toLowerCase().contains(query)) {
  110. dummyListData.add(item);
  111. }
  112. }
  113. setState(() {
  114. conversations.clear();
  115. conversations.addAll(dummyListData);
  116. });
  117. return;
  118. }
  119. setState(() {
  120. conversations.clear();
  121. conversations.addAll(widget.conversations);
  122. });
  123. }
  124. @override
  125. void initState() {
  126. super.initState();
  127. conversations.addAll(widget.conversations);
  128. friends.addAll(widget.friends);
  129. setState(() {});
  130. }
  131. Widget list() {
  132. if (conversations.isEmpty) {
  133. return const Center(
  134. child: Text('No Conversations'),
  135. );
  136. }
  137. return ListView.builder(
  138. itemCount: conversations.length,
  139. shrinkWrap: true,
  140. padding: const EdgeInsets.only(top: 16),
  141. physics: const NeverScrollableScrollPhysics(),
  142. itemBuilder: (context, i) {
  143. return ConversationListItem(
  144. conversation: conversations[i],
  145. );
  146. },
  147. );
  148. }
  149. onGoBack(dynamic value) async {
  150. conversations = await getConversations();
  151. friends = await getFriends();
  152. setState(() {});
  153. }
  154. }