-
-
Save sendbird-community/6b95537992aa632bb18596812193f663 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:sendbird_sdk/sendbird_sdk.dart'; | |
import 'group_channel_view.dart'; | |
class ChannelListView extends StatefulWidget { | |
const ChannelListView({Key? key}) : super(key: key); | |
@override | |
ChannelListViewState createState() => ChannelListViewState(); | |
} | |
class ChannelListViewState extends State<ChannelListView> | |
with ChannelEventHandler { | |
Future<List<GroupChannel>> getGroupChannels() async { | |
try { | |
final query = GroupChannelListQuery() | |
..includeEmptyChannel = true | |
..order = GroupChannelListOrder.latestLastMessage | |
..limit = 15; | |
return await query.loadNext(); | |
} catch (e) { | |
print('channel_list_view: getGroupChannel: ERROR: $e'); | |
return []; | |
} | |
} | |
@override | |
void initState() { | |
super.initState(); | |
SendbirdSdk().addChannelEventHandler('channel_list_view', this); | |
} | |
@override | |
void dispose() { | |
SendbirdSdk().removeChannelEventHandler("channel_list_view"); | |
super.dispose(); | |
} | |
@override | |
void onChannelChanged(BaseChannel channel) { | |
setState(() { | |
// Force the list future builder to rebuild. | |
}); | |
} | |
@override | |
void onChannelDeleted(String channelUrl, ChannelType channelType) { | |
setState(() { | |
// Force the list future builder to rebuild. | |
}); | |
} | |
@override | |
void onUserJoined(GroupChannel channel, User user) { | |
setState(() { | |
// Force the list future builder to rebuild. | |
}); | |
} | |
@override | |
void onUserLeaved(GroupChannel channel, User user) { | |
setState(() { | |
// Force the list future builder to rebuild. | |
}); | |
super.onUserLeaved(channel, user); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: navigationBar(), | |
body: body(context), | |
); | |
} | |
PreferredSizeWidget navigationBar() { | |
return AppBar( | |
automaticallyImplyLeading: true, | |
backgroundColor: Colors.white, | |
centerTitle: true, | |
leading: BackButton(color: Theme.of(context).primaryColor), | |
title: const Text( | |
'Channels', | |
style: TextStyle(color: Colors.black), | |
), | |
actions: [ | |
Container( | |
width: 60, | |
padding: const EdgeInsets.fromLTRB(0, 10, 0, 10), | |
child: TextButton( | |
onPressed: () { | |
Navigator.pushNamed(context, '/create_channel'); | |
}, | |
child: Image.asset("assets/iconCreate@3x.png")), | |
), | |
], | |
); | |
} | |
Widget body(BuildContext context) { | |
return FutureBuilder( | |
future: getGroupChannels(), | |
builder: (context, snapshot) { | |
if (snapshot.hasData == false || snapshot.data == null) { | |
// Nothing to display yet - good place for a loading indicator | |
return Container(); | |
} | |
List<GroupChannel> channels = snapshot.data as List<GroupChannel>; | |
return ListView.builder( | |
itemCount: channels.length, | |
itemBuilder: (context, index) { | |
GroupChannel channel = channels[index]; | |
return ListTile( | |
// Display all channel members as the title | |
title: Text( | |
[for (final member in channel.members) member.nickname] | |
.join(", "), | |
), | |
// Display the last message presented | |
subtitle: Text(channel.lastMessage?.message ?? ''), | |
onTap: () { | |
gotoChannel(channel.channelUrl); | |
}, | |
); | |
}); | |
}, | |
); | |
} | |
void gotoChannel(String channelUrl) { | |
GroupChannel.getChannel(channelUrl).then((channel) { | |
Navigator.pushNamed(context, '/channel_list'); | |
Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (context) => GroupChannelView(groupChannel: channel), | |
), | |
); | |
}).catchError((e) { | |
//handle error | |
print('channel_list_view: gotoChannel: ERROR: $e'); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment