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.

135 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(30),
  82. color: Colors.pink[50],
  83. ),
  84. child: Row(
  85. children: const <Widget>[
  86. Icon(Icons.add,color: Colors.pink,size: 20,),
  87. SizedBox(width: 2,),
  88. Text("Add",style: TextStyle(fontSize: 14,fontWeight: FontWeight.bold),),
  89. ],
  90. ),
  91. )
  92. ],
  93. ),
  94. ),
  95. ),
  96. Padding(
  97. padding: const EdgeInsets.only(top: 16,left: 16,right: 16),
  98. child: TextField(
  99. decoration: InputDecoration(
  100. hintText: "Search...",
  101. hintStyle: TextStyle(color: Colors.grey.shade600),
  102. prefixIcon: Icon(Icons.search,color: Colors.grey.shade600, size: 20,),
  103. filled: true,
  104. fillColor: Colors.grey.shade100,
  105. contentPadding: const EdgeInsets.all(8),
  106. enabledBorder: OutlineInputBorder(
  107. borderRadius: BorderRadius.circular(20),
  108. borderSide: BorderSide(
  109. color: Colors.grey.shade100
  110. )
  111. ),
  112. ),
  113. onChanged: (value) => filterSearchResults(value.toLowerCase())
  114. ),
  115. ),
  116. Padding(
  117. padding: const EdgeInsets.only(top: 16,left: 16,right: 16),
  118. child: list(),
  119. ),
  120. ],
  121. ),
  122. ),
  123. );
  124. }
  125. }