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.

138 lines
4.6 KiB

  1. import 'package:flutter/material.dart';
  2. import '/models/friends.dart';
  3. import '/views/main/friend_list_item.dart';
  4. class FriendList extends StatefulWidget {
  5. final List<Friend> friends;
  6. const FriendList({
  7. Key? key,
  8. required this.friends,
  9. }) : super(key: key);
  10. @override
  11. State<FriendList> createState() => _FriendListState();
  12. }
  13. class _FriendListState extends State<FriendList> {
  14. List<Friend> friends = [];
  15. List<Friend> friendsDuplicate = [];
  16. @override
  17. void initState() {
  18. super.initState();
  19. friends.addAll(widget.friends);
  20. setState(() {});
  21. }
  22. void filterSearchResults(String query) {
  23. List<Friend> dummySearchList = [];
  24. dummySearchList.addAll(widget.friends);
  25. if(query.isNotEmpty) {
  26. List<Friend> dummyListData = [];
  27. dummySearchList.forEach((item) {
  28. if(item.username.toLowerCase().contains(query)) {
  29. dummyListData.add(item);
  30. }
  31. });
  32. setState(() {
  33. friends.clear();
  34. friends.addAll(dummyListData);
  35. });
  36. return;
  37. }
  38. setState(() {
  39. friends.clear();
  40. friends.addAll(widget.friends);
  41. });
  42. }
  43. Widget list() {
  44. if (friends.isEmpty) {
  45. return const Center(
  46. child: Text('No Friends'),
  47. );
  48. }
  49. return ListView.builder(
  50. itemCount: friends.length,
  51. shrinkWrap: true,
  52. padding: const EdgeInsets.only(top: 16),
  53. physics: const NeverScrollableScrollPhysics(),
  54. itemBuilder: (context, i) {
  55. return FriendListItem(
  56. id: friends[i].id,
  57. username: friends[i].username,
  58. );
  59. },
  60. );
  61. }
  62. @override
  63. Widget build(BuildContext context) {
  64. return Scaffold(
  65. body: SingleChildScrollView(
  66. physics: const BouncingScrollPhysics(),
  67. child: Column(
  68. crossAxisAlignment: CrossAxisAlignment.start,
  69. children: <Widget>[
  70. SafeArea(
  71. child: Padding(
  72. padding: const EdgeInsets.only(left: 16,right: 16,top: 10),
  73. child: Row(
  74. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  75. children: <Widget>[
  76. const Text("Friends",style: TextStyle(fontSize: 32,fontWeight: FontWeight.bold),),
  77. Container(
  78. padding: const EdgeInsets.only(left: 8,right: 8,top: 2,bottom: 2),
  79. height: 30,
  80. decoration: BoxDecoration(
  81. borderRadius: BorderRadius.circular(20),
  82. color: Theme.of(context).colorScheme.tertiary
  83. ),
  84. child: Row(
  85. children: <Widget>[
  86. Icon(
  87. Icons.add,
  88. color: Theme.of(context).primaryColor,
  89. size: 20
  90. ),
  91. const SizedBox(width: 2,),
  92. const Text(
  93. "Add",
  94. style: TextStyle(
  95. fontSize: 14,
  96. fontWeight: FontWeight.bold
  97. )
  98. ),
  99. ],
  100. ),
  101. )
  102. ],
  103. ),
  104. ),
  105. ),
  106. Padding(
  107. padding: const EdgeInsets.only(top: 16,left: 16,right: 16),
  108. child: TextField(
  109. decoration: const InputDecoration(
  110. hintText: "Search...",
  111. prefixIcon: Icon(
  112. Icons.search,
  113. size: 20
  114. ),
  115. ),
  116. onChanged: (value) => filterSearchResults(value.toLowerCase())
  117. ),
  118. ),
  119. Padding(
  120. padding: const EdgeInsets.only(top: 16,left: 16,right: 16),
  121. child: list(),
  122. ),
  123. ],
  124. ),
  125. ),
  126. );
  127. }
  128. }