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.

181 lines
4.6 KiB

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