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.

211 lines
5.6 KiB

  1. import 'package:Envelope/components/custom_title_bar.dart';
  2. import 'package:Envelope/components/qr_reader.dart';
  3. import 'package:Envelope/views/main/friend/add_search.dart';
  4. import 'package:Envelope/views/main/friend/request_list_item.dart';
  5. import 'package:flutter/material.dart';
  6. import '/models/friends.dart';
  7. import '/components/custom_expandable_fab.dart';
  8. import '/views/main/friend/list_item.dart';
  9. class FriendList extends StatefulWidget {
  10. final List<Friend> friends;
  11. final List<Friend> friendRequests;
  12. final Function callback;
  13. const FriendList({
  14. Key? key,
  15. required this.friends,
  16. required this.friendRequests,
  17. required this.callback,
  18. }) : super(key: key);
  19. @override
  20. State<FriendList> createState() => _FriendListState();
  21. }
  22. class _FriendListState extends State<FriendList> {
  23. List<Friend> friends = [];
  24. List<Friend> friendRequests = [];
  25. List<Friend> friendsDuplicate = [];
  26. List<Friend> friendRequestsDuplicate = [];
  27. @override
  28. Widget build(BuildContext context) {
  29. return Scaffold(
  30. appBar: const CustomTitleBar(
  31. title: Text(
  32. 'Friends',
  33. style: TextStyle(
  34. fontSize: 32,
  35. fontWeight: FontWeight.bold
  36. )
  37. ),
  38. showBack: false,
  39. backgroundColor: Colors.transparent,
  40. ),
  41. body: Padding(
  42. padding: const EdgeInsets.only(top: 16,left: 16,right: 16),
  43. child: SingleChildScrollView(
  44. child: Column(
  45. children: <Widget>[
  46. TextField(
  47. decoration: const InputDecoration(
  48. hintText: 'Search...',
  49. prefixIcon: Icon(
  50. Icons.search,
  51. size: 20
  52. ),
  53. ),
  54. onChanged: (value) => filterSearchResults(value.toLowerCase())
  55. ),
  56. headingOrNull('Friend Requests'),
  57. friendRequestList(),
  58. headingOrNull('Friends'),
  59. friendList(),
  60. ],
  61. ),
  62. ),
  63. ),
  64. floatingActionButton: Padding(
  65. padding: const EdgeInsets.only(right: 10, bottom: 10),
  66. child: ExpandableFab(
  67. icon: const Icon(Icons.add, size: 30),
  68. distance: 90.0,
  69. children: [
  70. ActionButton(
  71. onPressed: () {
  72. Navigator.of(context).push(
  73. MaterialPageRoute(builder: (context) => const QrReader())
  74. );//.then(onGoBack); // TODO
  75. },
  76. icon: const Icon(Icons.qr_code_2, size: 25),
  77. ),
  78. ActionButton(
  79. onPressed: () {
  80. Navigator.of(context).push(
  81. MaterialPageRoute(builder: (context) => const FriendAddSearch())
  82. );//.then(onGoBack); // TODO
  83. },
  84. icon: const Icon(Icons.search, size: 25),
  85. ),
  86. ],
  87. )
  88. )
  89. );
  90. }
  91. void filterSearchResults(String query) {
  92. List<Friend> dummyFriendsList = [];
  93. List<Friend> dummyFriendRequestsList = [];
  94. dummyFriendsList.addAll(friends);
  95. dummyFriendRequestsList.addAll(friendRequests);
  96. if (query.isNotEmpty) {
  97. List<Friend> dummyFriendData = [];
  98. List<Friend> dummyFriendRequestData = [];
  99. for (Friend item in dummyFriendsList) {
  100. if(item.username.toLowerCase().contains(query)) {
  101. dummyFriendData.add(item);
  102. }
  103. }
  104. for (Friend item in dummyFriendRequestsList) {
  105. if(item.username.toLowerCase().contains(query)) {
  106. dummyFriendRequestData.add(item);
  107. }
  108. }
  109. setState(() {
  110. friends.clear();
  111. friends.addAll(dummyFriendData);
  112. friendRequests.clear();
  113. friendRequests.addAll(dummyFriendRequestData);
  114. });
  115. return;
  116. }
  117. setState(() {
  118. friends.clear();
  119. friends.addAll(widget.friends);
  120. friendRequests.clear();
  121. friendRequests.addAll(widget.friendRequests);
  122. });
  123. }
  124. @override
  125. void initState() {
  126. super.initState();
  127. friends.addAll(widget.friends);
  128. friendRequests.addAll(widget.friendRequests);
  129. }
  130. Future<void> initFriends() async {
  131. friends = await getFriends(accepted: true);
  132. friendRequests = await getFriends(accepted: false);
  133. setState(() {});
  134. widget.callback();
  135. }
  136. Widget headingOrNull(String heading) {
  137. if (friends.isEmpty || friendRequests.isEmpty) {
  138. return const SizedBox.shrink();
  139. }
  140. return Padding(
  141. padding: const EdgeInsets.only(top: 16),
  142. child: Align(
  143. alignment: Alignment.centerLeft,
  144. child: Text(
  145. heading,
  146. style: TextStyle(
  147. fontSize: 16,
  148. color: Theme.of(context).colorScheme.tertiary,
  149. ),
  150. ),
  151. )
  152. );
  153. }
  154. Widget friendRequestList() {
  155. if (friendRequests.isEmpty) {
  156. return const SizedBox.shrink();
  157. }
  158. return ListView.builder(
  159. itemCount: friendRequests.length,
  160. shrinkWrap: true,
  161. padding: const EdgeInsets.only(top: 16),
  162. physics: const NeverScrollableScrollPhysics(),
  163. itemBuilder: (context, i) {
  164. return FriendRequestListItem(
  165. friend: friendRequests[i],
  166. callback: initFriends,
  167. );
  168. },
  169. );
  170. }
  171. Widget friendList() {
  172. if (friends.isEmpty) {
  173. return const Center(
  174. child: Text('No Friends'),
  175. );
  176. }
  177. return ListView.builder(
  178. itemCount: friends.length,
  179. shrinkWrap: true,
  180. padding: const EdgeInsets.only(top: 16),
  181. physics: const NeverScrollableScrollPhysics(),
  182. itemBuilder: (context, i) {
  183. return FriendListItem(
  184. friend: friends[i],
  185. );
  186. },
  187. );
  188. }
  189. }