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.

144 lines
4.4 KiB

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