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.

111 lines
3.3 KiB

  1. import 'package:flutter/material.dart';
  2. import '/models/conversations.dart';
  3. import '/views/main/conversation_list_item.dart';
  4. class ConversationList extends StatefulWidget {
  5. final List<Conversation> conversations;
  6. const ConversationList({
  7. Key? key,
  8. required this.conversations,
  9. }) : super(key: key);
  10. @override
  11. State<ConversationList> createState() => _ConversationListState();
  12. }
  13. class _ConversationListState extends State<ConversationList> {
  14. List<Conversation> conversations = [];
  15. @override
  16. void initState() {
  17. super.initState();
  18. conversations.addAll(widget.conversations);
  19. setState(() {});
  20. }
  21. void filterSearchResults(String query) {
  22. List<Conversation> dummySearchList = [];
  23. dummySearchList.addAll(widget.conversations);
  24. if(query.isNotEmpty) {
  25. List<Conversation> dummyListData = [];
  26. dummySearchList.forEach((item) {
  27. if (item.name.toLowerCase().contains(query)) {
  28. dummyListData.add(item);
  29. }
  30. });
  31. setState(() {
  32. conversations.clear();
  33. conversations.addAll(dummyListData);
  34. });
  35. return;
  36. }
  37. setState(() {
  38. conversations.clear();
  39. conversations.addAll(widget.conversations);
  40. });
  41. }
  42. Widget list() {
  43. if (conversations.isEmpty) {
  44. return const Center(
  45. child: Text('No Conversations'),
  46. );
  47. }
  48. return ListView.builder(
  49. itemCount: conversations.length,
  50. shrinkWrap: true,
  51. padding: const EdgeInsets.only(top: 16),
  52. physics: const NeverScrollableScrollPhysics(),
  53. itemBuilder: (context, i) {
  54. return ConversationListItem(
  55. conversation: conversations[i],
  56. );
  57. },
  58. );
  59. }
  60. @override
  61. Widget build(BuildContext context) {
  62. return Scaffold(
  63. body: SingleChildScrollView(
  64. physics: const BouncingScrollPhysics(),
  65. child: Column(
  66. crossAxisAlignment: CrossAxisAlignment.start,
  67. children: <Widget>[
  68. SafeArea(
  69. child: Padding(
  70. padding: const EdgeInsets.only(left: 16,right: 16,top: 10),
  71. child: Row(
  72. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  73. children: const <Widget>[
  74. Text("Conversations",style: TextStyle(fontSize: 32,fontWeight: FontWeight.bold),),
  75. ],
  76. ),
  77. ),
  78. ),
  79. Padding(
  80. padding: const EdgeInsets.only(top: 16,left: 16,right: 16),
  81. child: TextField(
  82. decoration: const InputDecoration(
  83. hintText: "Search...",
  84. prefixIcon: Icon(
  85. Icons.search,
  86. size: 20
  87. ),
  88. ),
  89. onChanged: (value) => filterSearchResults(value.toLowerCase())
  90. ),
  91. ),
  92. Padding(
  93. padding: const EdgeInsets.only(top: 16,left: 16,right: 16),
  94. child: list(),
  95. ),
  96. ],
  97. ),
  98. ),
  99. );
  100. }
  101. }