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.

195 lines
5.1 KiB

  1. import 'package:Capsule/components/custom_title_bar.dart';
  2. import 'package:Capsule/components/qr_reader.dart';
  3. import 'package:Capsule/views/main/friend/add_search.dart';
  4. import 'package:Capsule/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> dummySearchList = [];
  93. dummySearchList.addAll(friends);
  94. if(query.isNotEmpty) {
  95. List<Friend> dummyListData = [];
  96. for (Friend item in dummySearchList) {
  97. if(item.username.toLowerCase().contains(query)) {
  98. dummyListData.add(item);
  99. }
  100. }
  101. setState(() {
  102. friends.clear();
  103. friends.addAll(dummyListData);
  104. });
  105. return;
  106. }
  107. setState(() {
  108. friends.clear();
  109. friends.addAll(friends);
  110. });
  111. }
  112. @override
  113. void initState() {
  114. super.initState();
  115. friends.addAll(widget.friends);
  116. friendRequests.addAll(widget.friendRequests);
  117. }
  118. Future<void> initFriends() async {
  119. friends = await getFriends(accepted: true);
  120. friendRequests = await getFriends(accepted: false);
  121. setState(() {});
  122. widget.callback();
  123. }
  124. Widget headingOrNull(String heading) {
  125. if (friends.isEmpty || friendRequests.isEmpty) {
  126. return const SizedBox.shrink();
  127. }
  128. return Padding(
  129. padding: const EdgeInsets.only(top: 16),
  130. child: Align(
  131. alignment: Alignment.centerLeft,
  132. child: Text(
  133. heading,
  134. style: TextStyle(
  135. fontSize: 16,
  136. color: Theme.of(context).colorScheme.tertiary,
  137. ),
  138. ),
  139. )
  140. );
  141. }
  142. Widget friendRequestList() {
  143. if (friendRequests.isEmpty) {
  144. return const SizedBox.shrink();
  145. }
  146. return ListView.builder(
  147. itemCount: friendRequests.length,
  148. shrinkWrap: true,
  149. padding: const EdgeInsets.only(top: 16),
  150. physics: const NeverScrollableScrollPhysics(),
  151. itemBuilder: (context, i) {
  152. return FriendRequestListItem(
  153. friend: friendRequests[i],
  154. callback: initFriends,
  155. );
  156. },
  157. );
  158. }
  159. Widget friendList() {
  160. if (friends.isEmpty) {
  161. return const Center(
  162. child: Text('No Friends'),
  163. );
  164. }
  165. return ListView.builder(
  166. itemCount: friends.length,
  167. shrinkWrap: true,
  168. padding: const EdgeInsets.only(top: 16),
  169. physics: const NeverScrollableScrollPhysics(),
  170. itemBuilder: (context, i) {
  171. return FriendListItem(
  172. friend: friends[i],
  173. );
  174. },
  175. );
  176. }
  177. }