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.

137 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. friend: friends[i],
  57. );
  58. },
  59. );
  60. }
  61. @override
  62. Widget build(BuildContext context) {
  63. return Scaffold(
  64. body: SingleChildScrollView(
  65. physics: const BouncingScrollPhysics(),
  66. child: Column(
  67. crossAxisAlignment: CrossAxisAlignment.start,
  68. children: <Widget>[
  69. SafeArea(
  70. child: Padding(
  71. padding: const EdgeInsets.only(left: 16,right: 16,top: 10),
  72. child: Row(
  73. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  74. children: <Widget>[
  75. const Text("Friends",style: TextStyle(fontSize: 32,fontWeight: FontWeight.bold),),
  76. Container(
  77. padding: const EdgeInsets.only(left: 8,right: 8,top: 2,bottom: 2),
  78. height: 30,
  79. decoration: BoxDecoration(
  80. borderRadius: BorderRadius.circular(20),
  81. color: Theme.of(context).colorScheme.tertiary
  82. ),
  83. child: Row(
  84. children: <Widget>[
  85. Icon(
  86. Icons.add,
  87. color: Theme.of(context).primaryColor,
  88. size: 20
  89. ),
  90. const SizedBox(width: 2,),
  91. const Text(
  92. "Add",
  93. style: TextStyle(
  94. fontSize: 14,
  95. fontWeight: FontWeight.bold
  96. )
  97. ),
  98. ],
  99. ),
  100. )
  101. ],
  102. ),
  103. ),
  104. ),
  105. Padding(
  106. padding: const EdgeInsets.only(top: 16,left: 16,right: 16),
  107. child: TextField(
  108. decoration: const InputDecoration(
  109. hintText: "Search...",
  110. prefixIcon: Icon(
  111. Icons.search,
  112. size: 20
  113. ),
  114. ),
  115. onChanged: (value) => filterSearchResults(value.toLowerCase())
  116. ),
  117. ),
  118. Padding(
  119. padding: const EdgeInsets.only(top: 16,left: 16,right: 16),
  120. child: list(),
  121. ),
  122. ],
  123. ),
  124. ),
  125. );
  126. }
  127. }