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.

167 lines
5.4 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 Function(List<Friend> friendsSelected) saveCallback;
  10. const ConversationAddFriendsList({
  11. Key? key,
  12. required this.friends,
  13. required this.saveCallback,
  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: () {
  87. widget.saveCallback(friendsSelected);
  88. setState(() {
  89. friendsSelected = [];
  90. });
  91. },
  92. backgroundColor: Theme.of(context).colorScheme.primary,
  93. child: friendsSelected.isEmpty ?
  94. const Text('Skip') :
  95. const Icon(Icons.add, size: 30),
  96. ),
  97. ),
  98. );
  99. }
  100. void filterSearchResults(String query) {
  101. List<Friend> dummySearchList = [];
  102. dummySearchList.addAll(widget.friends);
  103. if(query.isNotEmpty) {
  104. List<Friend> dummyListData = [];
  105. for (Friend friend in dummySearchList) {
  106. if (friend.username.toLowerCase().contains(query)) {
  107. dummyListData.add(friend);
  108. }
  109. }
  110. setState(() {
  111. friends.clear();
  112. friends.addAll(dummyListData);
  113. });
  114. return;
  115. }
  116. setState(() {
  117. friends.clear();
  118. friends.addAll(widget.friends);
  119. });
  120. }
  121. @override
  122. void initState() {
  123. super.initState();
  124. friends.addAll(widget.friends);
  125. setState(() {});
  126. }
  127. Widget list() {
  128. if (friends.isEmpty) {
  129. return const Center(
  130. child: Text('No Friends'),
  131. );
  132. }
  133. return ListView.builder(
  134. itemCount: friends.length,
  135. shrinkWrap: true,
  136. padding: const EdgeInsets.only(top: 16),
  137. physics: const BouncingScrollPhysics(),
  138. itemBuilder: (context, i) {
  139. return ConversationAddFriendItem(
  140. friend: friends[i],
  141. isSelected: (bool value) {
  142. setState(() {
  143. widget.friends[i].selected = value;
  144. if (value) {
  145. friendsSelected.add(friends[i]);
  146. return;
  147. }
  148. friendsSelected.remove(friends[i]);
  149. });
  150. }
  151. );
  152. },
  153. );
  154. }
  155. }