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