From d2a115be6fe9584bd5e7a25cb0e5592088005eb3 Mon Sep 17 00:00:00 2001 From: Deven Joshi Date: Tue, 15 Dec 2020 19:47:21 +0530 Subject: [PATCH] feat: Added change name, group name, etc --- lib/src/group_info_screen.dart | 85 ++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 4 deletions(-) diff --git a/lib/src/group_info_screen.dart b/lib/src/group_info_screen.dart index ceb33dc8..7dd7ceaa 100644 --- a/lib/src/group_info_screen.dart +++ b/lib/src/group_info_screen.dart @@ -27,6 +27,11 @@ class _GroupInfoScreenState extends State { Timer _debounce; Function modalSetStateCallback; + final FocusNode _focusNode = FocusNode(); + + bool namedChanged = false; + String changedName = ''; + void _userNameListener() { if (_debounce?.isActive ?? false) _debounce.cancel(); _debounce = Timer(const Duration(milliseconds: 350), () { @@ -49,6 +54,10 @@ class _GroupInfoScreenState extends State { _nameController = TextEditingController.fromValue( TextEditingValue(text: channel.channel.extraData['name'])); _searchController = TextEditingController()..addListener(_userNameListener); + + _nameController.addListener(() { + setState(() {}); + }); } @override @@ -74,10 +83,19 @@ class _GroupInfoScreenState extends State { title: Column( children: [ // TODO: Add member names instead of Demo (if name = null) - Text( - channel.channel.extraData['name'] ?? 'Demo', - style: TextStyle(color: Colors.black), - ), + FutureBuilder( + future: _memberQueryFuture, + builder: (context, snapshot) { + return Text( + _getChannelName(MediaQuery.of(context).size.width, + members: snapshot.data?.members + ?.map((e) => e.user) + ?.toList()), + style: TextStyle(color: Colors.black), + maxLines: 1, + overflow: TextOverflow.ellipsis, + ); + }), SizedBox( height: 3.0, ), @@ -208,6 +226,9 @@ class _GroupInfoScreenState extends State { } Widget _buildNameTile() { + var channel = StreamChannel.of(context).channel; + var channelName = channel.extraData['name']; + return Material( color: Colors.white, child: Container( @@ -228,6 +249,7 @@ class _GroupInfoScreenState extends State { ), Expanded( child: TextField( + focusNode: _focusNode, controller: _nameController, cursorColor: Colors.black, decoration: InputDecoration.collapsed( @@ -242,6 +264,48 @@ class _GroupInfoScreenState extends State { ), ), ), + if ((channelName == null) || + (channelName != _nameController.text.trim())) + Row( + mainAxisSize: MainAxisSize.min, + children: [ + InkWell( + child: StreamSvgIcon.close_small(), + onTap: () { + setState(() { + _nameController.text = + _getChannelName(MediaQuery.of(context).size.width); + _focusNode.unfocus(); + }); + }, + ), + Padding( + padding: const EdgeInsets.only(right: 16.0, left: 8.0), + child: InkWell( + child: StreamSvgIcon.check( + color: StreamChatTheme.of(context).accentColor, + size: 24.0, + ), + onTap: () { + StreamChannel.of(context).channel.update({ + 'name': _nameController.text.trim(), + }).then((value) { + setState(() { + _focusNode.unfocus(); + namedChanged = true; + changedName = _nameController.text.trim(); + }); + }).catchError((err) { + setState(() { + _nameController.text = channelName; + _focusNode.unfocus(); + }); + }); + }, + ), + ), + ], + ), ], ), ), @@ -492,6 +556,19 @@ class _GroupInfoScreenState extends State { ); } + String _getChannelName(double width, {List members}) { + if (namedChanged) { + return changedName; + } + + if (StreamChannel.of(context).channel.extraData['name'] == null && + members != null) { + return '${members[0].name} & ${members.length - 1} others'; + } + + return StreamChannel.of(context).channel.extraData['name'] ?? ''; + } + String _getLastSeen(User user) { if (user.online) { return 'Online';