|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/services.dart'; |
| 3 | +import 'package:flutter_mqtt/abstraction/models/ContactChat.dart'; |
| 4 | +import 'package:flutter_mqtt/db/appdata/AppData.dart'; |
| 5 | +import 'package:flutter_mqtt/ui/widgets/avatar_cancellable.dart'; |
| 6 | + |
| 7 | +class CreateGroupPage extends StatefulWidget { |
| 8 | + const CreateGroupPage({Key? key}) : super(key: key); |
| 9 | + |
| 10 | + @override |
| 11 | + _CreateGroupPageState createState() => _CreateGroupPageState(); |
| 12 | +} |
| 13 | + |
| 14 | +class _CreateGroupPageState extends State<CreateGroupPage> { |
| 15 | + int globalCount = 0; |
| 16 | + List<ContactChat> selected = List<ContactChat>.empty(growable: true); |
| 17 | + TextEditingController _groupNameController = TextEditingController(); |
| 18 | + |
| 19 | + int maxNameLength = 25; |
| 20 | + int currentNameLength = 0; |
| 21 | + @override |
| 22 | + Widget build(BuildContext context) { |
| 23 | + return Scaffold( |
| 24 | + appBar: AppBar( |
| 25 | + centerTitle: false, |
| 26 | + title: Column( |
| 27 | + mainAxisAlignment: MainAxisAlignment.start, |
| 28 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 29 | + children: [ |
| 30 | + Text("New Group"), |
| 31 | + Text(selected.length.toString() + "/" + globalCount.toString()) |
| 32 | + ], |
| 33 | + ), |
| 34 | + ), |
| 35 | + body: Column( |
| 36 | + children: [ |
| 37 | + _groupNameView(), |
| 38 | + Divider(), |
| 39 | + _selectedView(), |
| 40 | + Expanded(child: _contactsView()) |
| 41 | + ], |
| 42 | + ), |
| 43 | + floatingActionButton: Visibility( |
| 44 | + child: FloatingActionButton.extended( |
| 45 | + onPressed: () {}, |
| 46 | + label: Text("Continue"), |
| 47 | + icon: Icon(Icons.arrow_right_alt_outlined)), |
| 48 | + visible: selected.length > 0), |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + Widget _groupNameView() { |
| 53 | + return Padding( |
| 54 | + padding: const EdgeInsets.all(8.0), |
| 55 | + child: Row( |
| 56 | + crossAxisAlignment: CrossAxisAlignment.center, |
| 57 | + children: [ |
| 58 | + CircleAvatar( |
| 59 | + child: Icon( |
| 60 | + Icons.group_sharp, |
| 61 | + size: 30, |
| 62 | + ), |
| 63 | + radius: 30, |
| 64 | + ), |
| 65 | + SizedBox(width: 5), |
| 66 | + Expanded( |
| 67 | + child: TextField( |
| 68 | + controller: _groupNameController, |
| 69 | + maxLength: maxNameLength, |
| 70 | + onChanged: (txt) { |
| 71 | + setState(() { |
| 72 | + currentNameLength = txt.length; |
| 73 | + }); |
| 74 | + }, |
| 75 | + decoration: InputDecoration( |
| 76 | + border: InputBorder.none, |
| 77 | + hintText: 'Group Name', |
| 78 | + counterText: "", |
| 79 | + suffixText: |
| 80 | + '${currentNameLength.toString()}/${maxNameLength.toString()}', |
| 81 | + ), |
| 82 | + )) |
| 83 | + ], |
| 84 | + ), |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + Widget _contactsView() { |
| 89 | + return StreamBuilder<List<ContactChat>>( |
| 90 | + stream: AppData.instance()!.contactsHandler.getContacts(), |
| 91 | + builder: (context, snapshot) { |
| 92 | + if (snapshot.hasError) { |
| 93 | + return Text(snapshot.error.toString()); |
| 94 | + } |
| 95 | + if (snapshot.hasData) { |
| 96 | + var chats = snapshot.data; |
| 97 | + globalCount = chats != null ? chats.length : 0; |
| 98 | + return Column( |
| 99 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 100 | + children: [ |
| 101 | + Padding( |
| 102 | + padding: const EdgeInsets.all(8.0), |
| 103 | + child: Text("Select your group members", |
| 104 | + style: TextStyle( |
| 105 | + fontWeight: FontWeight.bold, |
| 106 | + fontSize: 23, |
| 107 | + color: Colors.grey)), |
| 108 | + ), |
| 109 | + Expanded( |
| 110 | + child: ListView.builder( |
| 111 | + itemCount: chats!.length, |
| 112 | + itemBuilder: (context, position) { |
| 113 | + return InkWell( |
| 114 | + onTap: () { |
| 115 | + _toggleSelection(chats[position]); |
| 116 | + }, |
| 117 | + child: ListTile( |
| 118 | + title: Text(chats[position].firstName + |
| 119 | + " " + |
| 120 | + chats[position].lastName), |
| 121 | + subtitle: Text("Room: " + chats[position].roomId), |
| 122 | + leading: AvatarWithBadge( |
| 123 | + radius: 25, |
| 124 | + badgeRadius: 8, |
| 125 | + foregroundImage: NetworkImage( |
| 126 | + chats[position].avatar ?? "", |
| 127 | + ), |
| 128 | + showBadge: selected.where((element) => element.id == chats[position].id).length > 0, |
| 129 | + ), |
| 130 | + |
| 131 | + ), |
| 132 | + ); |
| 133 | + }), |
| 134 | + ), |
| 135 | + ], |
| 136 | + ); |
| 137 | + } else { |
| 138 | + return Text("Loading..."); |
| 139 | + } |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + Widget _selectedView() { |
| 144 | + return Align( |
| 145 | + child: Column( |
| 146 | + children: [ |
| 147 | + AnimatedContainer( |
| 148 | + curve: Curves.fastOutSlowIn, |
| 149 | + duration: Duration(milliseconds: 400), |
| 150 | + height: selected.length > 0 ? 80 : 0, |
| 151 | + child: ListView.builder( |
| 152 | + scrollDirection: Axis.horizontal, |
| 153 | + itemCount: selected.length, |
| 154 | + itemBuilder: (context, index) { |
| 155 | + return AvatarWithBadge( |
| 156 | + title: selected[index].firstName, |
| 157 | + foregroundImage: NetworkImage(selected[index].avatar ?? ""), |
| 158 | + onTap: () { |
| 159 | + _toggleSelection(selected[index]); |
| 160 | + }, |
| 161 | + ); |
| 162 | + return Container(); |
| 163 | + }), |
| 164 | + ), |
| 165 | + AnimatedOpacity( |
| 166 | + duration: Duration(milliseconds: 500), |
| 167 | + opacity: selected.length > 0 ? 1 : 0, |
| 168 | + child: Divider(), |
| 169 | + ), |
| 170 | + ], |
| 171 | + ), |
| 172 | + alignment: Alignment.topCenter, |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + _toggleSelection(ContactChat contactChat) { |
| 177 | + bool alreadySelected = |
| 178 | + selected.where((element) => element.id == contactChat.id).isNotEmpty; |
| 179 | + if (alreadySelected) { |
| 180 | + setState(() { |
| 181 | + selected = selected |
| 182 | + .where((element) => element.id != contactChat.id) |
| 183 | + .toList(growable: true); |
| 184 | + }); |
| 185 | + } else { |
| 186 | + setState(() { |
| 187 | + selected.add(contactChat); |
| 188 | + }); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments