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.

170 lines
5.7 KiB

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