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.

175 lines
5.7 KiB

  1. import 'package:flutter/material.dart';
  2. import '/models/conversations.dart';
  3. import '/models/friends.dart';
  4. import '/utils/storage/conversations.dart';
  5. import '/views/main/conversation/create_add_users_list.dart';
  6. import '/views/main/conversation/detail.dart';
  7. class ConversationAddFriendsList extends StatefulWidget {
  8. final List<Friend> friends;
  9. final String title;
  10. const ConversationAddFriendsList({
  11. Key? key,
  12. required this.friends,
  13. required this.title,
  14. }) : super(key: key);
  15. @override
  16. State<ConversationAddFriendsList> createState() => _ConversationAddFriendsListState();
  17. }
  18. class _ConversationAddFriendsListState extends State<ConversationAddFriendsList> {
  19. List<Friend> friends = [];
  20. List<Friend> friendsSelected = [];
  21. @override
  22. Widget build(BuildContext context) {
  23. return Scaffold(
  24. appBar: AppBar(
  25. elevation: 0,
  26. automaticallyImplyLeading: false,
  27. flexibleSpace: SafeArea(
  28. child: Container(
  29. padding: const EdgeInsets.only(right: 16),
  30. child: Row(
  31. children: <Widget>[
  32. IconButton(
  33. onPressed: (){
  34. Navigator.pop(context);
  35. },
  36. icon: const Icon(Icons.arrow_back),
  37. ),
  38. const SizedBox(width: 2,),
  39. Expanded(
  40. child: Column(
  41. crossAxisAlignment: CrossAxisAlignment.start,
  42. mainAxisAlignment: MainAxisAlignment.center,
  43. children: <Widget>[
  44. Text(
  45. friendsSelected.isEmpty ?
  46. 'Select Friends' :
  47. '${friendsSelected.length} Friends Selected',
  48. style: const TextStyle(
  49. fontSize: 16,
  50. fontWeight: FontWeight.w600
  51. ),
  52. ),
  53. ],
  54. ),
  55. ),
  56. ],
  57. ),
  58. ),
  59. ),
  60. ),
  61. body: Column(
  62. crossAxisAlignment: CrossAxisAlignment.start,
  63. children: <Widget>[
  64. Padding(
  65. padding: const EdgeInsets.only(top: 20,left: 16,right: 16),
  66. child: TextField(
  67. decoration: const InputDecoration(
  68. hintText: "Search...",
  69. prefixIcon: Icon(
  70. Icons.search,
  71. size: 20
  72. ),
  73. ),
  74. onChanged: (value) => filterSearchResults(value.toLowerCase())
  75. ),
  76. ),
  77. Padding(
  78. padding: const EdgeInsets.only(top: 0,left: 16,right: 16),
  79. child: list(),
  80. ),
  81. ],
  82. ),
  83. floatingActionButton: Padding(
  84. padding: const EdgeInsets.only(right: 10, bottom: 10),
  85. child: FloatingActionButton(
  86. onPressed: () async {
  87. Conversation conversation = await createConversation(widget.title, friendsSelected);
  88. uploadConversation(conversation);
  89. setState(() {
  90. friendsSelected = [];
  91. });
  92. Navigator.of(context).popUntil((route) => route.isFirst);
  93. Navigator.push(context, MaterialPageRoute(builder: (context){
  94. return ConversationDetail(
  95. conversation: conversation,
  96. );
  97. }));
  98. },
  99. backgroundColor: Theme.of(context).colorScheme.primary,
  100. child: friendsSelected.isEmpty ?
  101. const Text('Skip') :
  102. const Icon(Icons.add, size: 30),
  103. ),
  104. ),
  105. );
  106. }
  107. void filterSearchResults(String query) {
  108. List<Friend> dummySearchList = [];
  109. dummySearchList.addAll(widget.friends);
  110. if(query.isNotEmpty) {
  111. List<Friend> dummyListData = [];
  112. for (Friend friend in dummySearchList) {
  113. if (friend.username.toLowerCase().contains(query)) {
  114. dummyListData.add(friend);
  115. }
  116. }
  117. setState(() {
  118. friends.clear();
  119. friends.addAll(dummyListData);
  120. });
  121. return;
  122. }
  123. setState(() {
  124. friends.clear();
  125. friends.addAll(widget.friends);
  126. });
  127. }
  128. @override
  129. void initState() {
  130. super.initState();
  131. friends.addAll(widget.friends);
  132. setState(() {});
  133. }
  134. Widget list() {
  135. if (friends.isEmpty) {
  136. return const Center(
  137. child: Text('No Friends'),
  138. );
  139. }
  140. return ListView.builder(
  141. itemCount: friends.length,
  142. shrinkWrap: true,
  143. padding: const EdgeInsets.only(top: 16),
  144. physics: const BouncingScrollPhysics(),
  145. itemBuilder: (context, i) {
  146. return ConversationAddFriendItem(
  147. friend: friends[i],
  148. isSelected: (bool value) {
  149. setState(() {
  150. widget.friends[i].selected = value;
  151. if (value) {
  152. friendsSelected.add(friends[i]);
  153. return;
  154. }
  155. friendsSelected.remove(friends[i]);
  156. });
  157. }
  158. );
  159. },
  160. );
  161. }
  162. }