import 'package:Envelope/models/conversations.dart'; import 'package:Envelope/utils/storage/conversations.dart'; import 'package:Envelope/views/main/conversation_create_add_users_list.dart'; import 'package:Envelope/views/main/conversation_detail.dart'; import 'package:flutter/material.dart'; import '/models/friends.dart'; class ConversationAddFriendsList extends StatefulWidget { final List friends; final String title; const ConversationAddFriendsList({ Key? key, required this.friends, required this.title, }) : super(key: key); @override State createState() => _ConversationAddFriendsListState(); } class _ConversationAddFriendsListState extends State { List friends = []; List friendsSelected = []; @override void initState() { super.initState(); friends.addAll(widget.friends); setState(() {}); } void filterSearchResults(String query) { List dummySearchList = []; dummySearchList.addAll(widget.friends); if(query.isNotEmpty) { List dummyListData = []; for (Friend friend in dummySearchList) { if (friend.username.toLowerCase().contains(query)) { dummyListData.add(friend); } } setState(() { friends.clear(); friends.addAll(dummyListData); }); return; } setState(() { friends.clear(); friends.addAll(widget.friends); }); } Widget list() { if (friends.isEmpty) { return const Center( child: Text('No Friends'), ); } return ListView.builder( itemCount: friends.length, shrinkWrap: true, padding: const EdgeInsets.only(top: 16), physics: const BouncingScrollPhysics(), itemBuilder: (context, i) { return ConversationAddFriendItem( friend: friends[i], isSelected: (bool value) { setState(() { widget.friends[i].selected = value; if (value) { friendsSelected.add(friends[i]); return; } friendsSelected.remove(friends[i]); }); } ); }, ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( elevation: 0, automaticallyImplyLeading: false, flexibleSpace: SafeArea( child: Container( padding: const EdgeInsets.only(right: 16), child: Row( children: [ IconButton( onPressed: (){ Navigator.pop(context); }, icon: const Icon(Icons.arrow_back), ), const SizedBox(width: 2,), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [ Text( friendsSelected.isEmpty ? 'Select Friends' : '${friendsSelected.length} Friends Selected', style: const TextStyle( fontSize: 16, fontWeight: FontWeight.w600 ), ), ], ), ), ], ), ), ), ), body: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsets.only(top: 20,left: 16,right: 16), child: TextField( decoration: const InputDecoration( hintText: "Search...", prefixIcon: Icon( Icons.search, size: 20 ), ), onChanged: (value) => filterSearchResults(value.toLowerCase()) ), ), Padding( padding: const EdgeInsets.only(top: 0,left: 16,right: 16), child: list(), ), ], ), floatingActionButton: Padding( padding: const EdgeInsets.only(right: 10, bottom: 10), child: FloatingActionButton( onPressed: () async { Conversation conversation = await createConversation(widget.title, friendsSelected); uploadConversation(conversation); setState(() { friendsSelected = []; }); Navigator.of(context).popUntil((route) => route.isFirst); Navigator.push(context, MaterialPageRoute(builder: (context){ return ConversationDetail( conversation: conversation, ); })); }, backgroundColor: Theme.of(context).colorScheme.primary, child: friendsSelected.isEmpty ? const Text('Skip') : const Icon(Icons.add, size: 30), ), ), ); } }