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.

143 lines
4.4 KiB

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