align with v4

This commit is contained in:
Salvatore Giordano
2022-04-29 15:24:35 +02:00
parent ccfbe367d0
commit 3e048711f5
9 changed files with 502 additions and 729 deletions
@@ -1,40 +1,29 @@
import 'package:example/localizations.dart'; import 'package:example/localizations.dart';
import 'package:example/routes/routes.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart'; import 'package:stream_chat_flutter/stream_chat_flutter.dart';
import 'package:video_player/video_player.dart';
import 'channel_page.dart';
class ChannelFileDisplayScreen extends StatefulWidget { class ChannelFileDisplayScreen extends StatefulWidget {
/// The sorting used for the channels matching the filters. final StreamMessageThemeData messageTheme;
/// Sorting is based on field and direction, multiple sorting options can be provided.
/// You can sort based on last_updated, last_message_at, updated_at, created_at or member_count.
/// Direction can be ascending or descending.
final List<SortOption>? sortOptions;
/// Pagination parameters
/// limit: the number of users to return (max is 30)
/// offset: the offset (max is 1000)
/// message_limit: how many messages should be included to each channel
final PaginationParams paginationParams;
/// The builder used when the file list is empty.
final WidgetBuilder? emptyBuilder;
const ChannelFileDisplayScreen({ const ChannelFileDisplayScreen({
this.sortOptions, Key? key,
this.paginationParams = const PaginationParams(limit: 20), required this.messageTheme,
this.emptyBuilder, }) : super(key: key);
});
@override @override
_ChannelFileDisplayScreenState createState() => State<ChannelFileDisplayScreen> createState() =>
_ChannelFileDisplayScreenState(); _ChannelFileDisplayScreenState();
} }
class _ChannelFileDisplayScreenState extends State<ChannelFileDisplayScreen> { class _ChannelFileDisplayScreenState extends State<ChannelFileDisplayScreen> {
@override final Map<String?, VideoPlayerController?> controllerCache = {};
void initState() {
super.initState(); late final controller = StreamMessageSearchListController(
final messageSearchBloc = MessageSearchBloc.of(context); client: StreamChat.of(context).client,
messageSearchBloc.search(
filter: Filter.in_( filter: Filter.in_(
'cid', 'cid',
[StreamChannel.of(context).channel.cid!], [StreamChannel.of(context).channel.cid!],
@@ -43,10 +32,14 @@ class _ChannelFileDisplayScreenState extends State<ChannelFileDisplayScreen> {
'attachments.type', 'attachments.type',
['file'], ['file'],
), ),
sort: widget.sortOptions, sort: [
pagination: widget.paginationParams, SortOption(
'created_at',
direction: SortOption.ASC,
),
],
limit: 20,
); );
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -59,44 +52,22 @@ class _ChannelFileDisplayScreenState extends State<ChannelFileDisplayScreen> {
AppLocalizations.of(context).files, AppLocalizations.of(context).files,
style: TextStyle( style: TextStyle(
color: StreamChatTheme.of(context).colorTheme.textHighEmphasis, color: StreamChatTheme.of(context).colorTheme.textHighEmphasis,
fontSize: 16.0), fontSize: 16.0,
),
leading: Center(
child: InkWell(
onTap: () {
Navigator.of(context).pop();
},
child: Container(
width: 24.0,
height: 24.0,
child: StreamSvgIcon.left(
color: StreamChatTheme.of(context).colorTheme.textHighEmphasis,
size: 24.0,
),
),
), ),
), ),
leading: StreamBackButton(),
backgroundColor: StreamChatTheme.of(context).colorTheme.barsBg, backgroundColor: StreamChatTheme.of(context).colorTheme.barsBg,
), ),
body: _buildMediaGrid(), body: ValueListenableBuilder(
); valueListenable: controller,
} builder: (
BuildContext context,
Widget _buildMediaGrid() { PagedValue<String, GetMessageResponse> value,
final messageSearchBloc = MessageSearchBloc.of(context); Widget? child,
) {
return StreamBuilder<List<GetMessageResponse>>( return value.when(
builder: (context, snapshot) { (items, nextPageKey, error) {
if (snapshot.data == null) { if (items.isEmpty) {
return Center(
child: const CircularProgressIndicator(),
);
}
if (snapshot.data!.isEmpty) {
if (widget.emptyBuilder != null) {
return widget.emptyBuilder!(context);
}
return Center( return Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
@@ -110,8 +81,9 @@ class _ChannelFileDisplayScreenState extends State<ChannelFileDisplayScreen> {
AppLocalizations.of(context).noFiles, AppLocalizations.of(context).noFiles,
style: TextStyle( style: TextStyle(
fontSize: 14.0, fontSize: 14.0,
color: color: StreamChatTheme.of(context)
StreamChatTheme.of(context).colorTheme.textHighEmphasis, .colorTheme
.textHighEmphasis,
), ),
), ),
SizedBox(height: 8.0), SizedBox(height: 8.0),
@@ -130,30 +102,22 @@ class _ChannelFileDisplayScreenState extends State<ChannelFileDisplayScreen> {
), ),
); );
} }
final media = <Attachment, Message>{}; final media = <Attachment, Message>{};
for (var item in snapshot.data!) { for (var item in items) {
item.message.attachments.where((e) => e.type == 'file').forEach((e) { item.message.attachments
.where((e) => e.type == 'file')
.forEach((e) {
media[e] = item.message; media[e] = item.message;
}); });
} }
return LazyLoadScrollView( return LazyLoadScrollView(
onEndOfPage: () => messageSearchBloc.search( onEndOfPage: () async {
filter: Filter.in_( if (nextPageKey != null) {
'cid', controller.loadMore(nextPageKey);
[StreamChannel.of(context).channel.cid!], }
), },
messageFilter: Filter.in_(
'attachments.type',
['file'],
),
sort: widget.sortOptions,
pagination: widget.paginationParams.copyWith(
offset: messageSearchBloc.messageResponses?.length ?? 0,
),
),
child: ListView.builder( child: ListView.builder(
itemBuilder: (context, position) { itemBuilder: (context, position) {
return Padding( return Padding(
@@ -171,7 +135,25 @@ class _ChannelFileDisplayScreenState extends State<ChannelFileDisplayScreen> {
), ),
); );
}, },
stream: messageSearchBloc.messagesStream, loading: () => Center(
child: const CircularProgressIndicator(),
),
error: (_) => Offstage(),
);
},
),
); );
} }
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
void initState() {
controller.doInitialLoad();
super.initState();
}
} }
@@ -278,12 +278,15 @@ class _ChannelList extends State<ChannelList> {
emptyBuilder: (_) { emptyBuilder: (_) {
return Center( return Center(
child: Padding( child: Padding(
padding: EdgeInsets.all(8), padding: const EdgeInsets.all(8),
child: Column( child: StreamScrollViewEmptyWidget(
mainAxisAlignment: MainAxisAlignment.center, emptyIcon: StreamSvgIcon.message(
children: [ size: 148,
Expanded(child: StreamChannelListEmptyWidget()), color: StreamChatTheme.of(context)
TextButton( .colorTheme
.disabled,
),
emptyTitle: TextButton(
onPressed: () { onPressed: () {
Navigator.pushNamed( Navigator.pushNamed(
context, context,
@@ -302,7 +305,6 @@ class _ChannelList extends State<ChannelList> {
), ),
), ),
), ),
],
), ),
), ),
); );
@@ -100,9 +100,7 @@ class _ChannelListPageState extends State<ChannelListPage> {
body: IndexedStack( body: IndexedStack(
index: _currentIndex, index: _currentIndex,
children: [ children: [
MessageSearchBloc( ChannelList(),
child: ChannelList(),
),
UserMentionsPage(), UserMentionsPage(),
], ],
), ),
@@ -1,49 +1,29 @@
import 'package:example/localizations.dart'; import 'package:example/localizations.dart';
import 'package:example/routes/routes.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart'; import 'package:stream_chat_flutter/stream_chat_flutter.dart';
import 'package:video_player/video_player.dart'; import 'package:video_player/video_player.dart';
import 'channel_page.dart';
class ChannelMediaDisplayScreen extends StatefulWidget { class ChannelMediaDisplayScreen extends StatefulWidget {
/// The sorting used for the channels matching the filters.
/// Sorting is based on field and direction, multiple sorting options can be provided.
/// You can sort based on last_updated, last_message_at, updated_at, created_at or member_count.
/// Direction can be ascending or descending.
final List<SortOption>? sortOptions;
/// Pagination parameters
/// limit: the number of users to return (max is 30)
/// offset: the offset (max is 1000)
/// message_limit: how many messages should be included to each channel
final PaginationParams paginationParams;
/// The builder used when the file list is empty.
final WidgetBuilder? emptyBuilder;
final ShowMessageCallback? onShowMessage;
final StreamMessageThemeData messageTheme; final StreamMessageThemeData messageTheme;
const ChannelMediaDisplayScreen({ const ChannelMediaDisplayScreen({
Key? key,
required this.messageTheme, required this.messageTheme,
this.sortOptions, }) : super(key: key);
this.paginationParams = const PaginationParams(limit: 20),
this.emptyBuilder,
this.onShowMessage,
});
@override @override
_ChannelMediaDisplayScreenState createState() => State<ChannelMediaDisplayScreen> createState() =>
_ChannelMediaDisplayScreenState(); _ChannelMediaDisplayScreenState();
} }
class _ChannelMediaDisplayScreenState extends State<ChannelMediaDisplayScreen> { class _ChannelMediaDisplayScreenState extends State<ChannelMediaDisplayScreen> {
Map<String?, VideoPlayerController?> controllerCache = {}; final Map<String?, VideoPlayerController?> controllerCache = {};
@override late final controller = StreamMessageSearchListController(
void initState() { client: StreamChat.of(context).client,
super.initState();
final messageSearchBloc = MessageSearchBloc.of(context);
messageSearchBloc.search(
filter: Filter.in_( filter: Filter.in_(
'cid', 'cid',
[StreamChannel.of(context).channel.cid!], [StreamChannel.of(context).channel.cid!],
@@ -52,10 +32,14 @@ class _ChannelMediaDisplayScreenState extends State<ChannelMediaDisplayScreen> {
'attachments.type', 'attachments.type',
['image', 'video'], ['image', 'video'],
), ),
sort: widget.sortOptions, sort: [
pagination: widget.paginationParams, SortOption(
'created_at',
direction: SortOption.ASC,
),
],
limit: 20,
); );
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -74,25 +58,13 @@ class _ChannelMediaDisplayScreenState extends State<ChannelMediaDisplayScreen> {
leading: StreamBackButton(), leading: StreamBackButton(),
backgroundColor: StreamChatTheme.of(context).colorTheme.barsBg, backgroundColor: StreamChatTheme.of(context).colorTheme.barsBg,
), ),
body: _buildMediaGrid(), body: ValueListenableBuilder(
); valueListenable: controller,
} builder: (BuildContext context,
PagedValue<String, GetMessageResponse> value, Widget? child) {
Widget _buildMediaGrid() { return value.when(
final messageSearchBloc = MessageSearchBloc.of(context); (items, nextPageKey, error) {
if (items.isEmpty) {
return StreamBuilder<List<GetMessageResponse>>(
builder: (context, snapshot) {
if (snapshot.data == null) {
return Center(
child: const CircularProgressIndicator(),
);
}
if (snapshot.data!.isEmpty) {
if (widget.emptyBuilder != null) {
return widget.emptyBuilder!(context);
}
return Center( return Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
@@ -106,13 +78,15 @@ class _ChannelMediaDisplayScreenState extends State<ChannelMediaDisplayScreen> {
AppLocalizations.of(context).noMedia, AppLocalizations.of(context).noMedia,
style: TextStyle( style: TextStyle(
fontSize: 14.0, fontSize: 14.0,
color: color: StreamChatTheme.of(context)
StreamChatTheme.of(context).colorTheme.textHighEmphasis, .colorTheme
.textHighEmphasis,
), ),
), ),
SizedBox(height: 8.0), SizedBox(height: 8.0),
Text( Text(
AppLocalizations.of(context).photosOrVideosWillAppearHere, AppLocalizations.of(context)
.photosOrVideosWillAppearHere,
textAlign: TextAlign.center, textAlign: TextAlign.center,
style: TextStyle( style: TextStyle(
fontSize: 14.0, fontSize: 14.0,
@@ -126,10 +100,9 @@ class _ChannelMediaDisplayScreenState extends State<ChannelMediaDisplayScreen> {
), ),
); );
} }
final media = <_AssetPackage>[]; final media = <_AssetPackage>[];
for (var item in snapshot.data!) { for (var item in value.asSuccess.items) {
item.message.attachments item.message.attachments
.where((e) => .where((e) =>
(e.type == 'image' || e.type == 'video') && (e.type == 'image' || e.type == 'video') &&
@@ -152,23 +125,14 @@ class _ChannelMediaDisplayScreenState extends State<ChannelMediaDisplayScreen> {
} }
return LazyLoadScrollView( return LazyLoadScrollView(
onEndOfPage: () => messageSearchBloc.search( onEndOfPage: () async {
filter: Filter.in_( if (nextPageKey != null) {
'cid', controller.loadMore(nextPageKey);
[StreamChannel.of(context).channel.cid!], }
), },
messageFilter: Filter.in_(
'attachments.type',
['image', 'video'],
),
sort: widget.sortOptions,
pagination: widget.paginationParams.copyWith(
offset: messageSearchBloc.messageResponses?.length ?? 0,
),
),
child: GridView.builder( child: GridView.builder(
gridDelegate: gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3), crossAxisCount: 3),
itemBuilder: (context, position) { itemBuilder: (context, position) {
var channel = StreamChannel.of(context).channel; var channel = StreamChannel.of(context).channel;
return Padding( return Padding(
@@ -191,7 +155,26 @@ class _ChannelMediaDisplayScreenState extends State<ChannelMediaDisplayScreen> {
.toList(), .toList(),
startIndex: position, startIndex: position,
userName: media[position].message.user!.name, userName: media[position].message.user!.name,
onShowMessage: widget.onShowMessage, onShowMessage: (m, c) async {
final client =
StreamChat.of(context).client;
final message = m;
final channel = client.channel(
c.type,
id: c.id,
);
if (channel.state == null) {
await channel.watch();
}
Navigator.pushNamed(
context,
Routes.CHANNEL_PAGE,
arguments: ChannelPageArgs(
channel: channel,
initialMessage: message,
),
);
},
), ),
), ),
), ),
@@ -218,16 +201,26 @@ class _ChannelMediaDisplayScreenState extends State<ChannelMediaDisplayScreen> {
), ),
); );
}, },
stream: messageSearchBloc.messagesStream, loading: () => Center(
child: const CircularProgressIndicator(),
),
error: (_) => Offstage(),
);
},
),
); );
} }
@override @override
void dispose() { void dispose() {
controller.dispose();
super.dispose(); super.dispose();
for (var c in controllerCache.values) {
c!.dispose();
} }
@override
void initState() {
controller.doInitialLoad();
super.initState();
} }
} }
@@ -35,7 +35,7 @@ class ChannelPage extends StatefulWidget {
class _ChannelPageState extends State<ChannelPage> { class _ChannelPageState extends State<ChannelPage> {
FocusNode? _focusNode; FocusNode? _focusNode;
MessageInputController _messageInputController = MessageInputController(); StreamMessageInputController _messageInputController = StreamMessageInputController();
@override @override
void initState() { void initState() {
@@ -218,36 +218,7 @@ class _ChatInfoScreenState extends State<ChatInfoScreen> {
MaterialPageRoute( MaterialPageRoute(
builder: (context) => StreamChannel( builder: (context) => StreamChannel(
channel: channel, channel: channel,
child: MessageSearchBloc( child: PinnedMessagesScreen(),
child: PinnedMessagesScreen(
messageTheme: widget.messageTheme,
sortOptions: [
SortOption(
'created_at',
direction: SortOption.ASC,
),
],
onShowMessage: (m, c) async {
final client = StreamChat.of(context).client;
final message = m;
final channel = client.channel(
c.type,
id: c.id,
);
if (channel.state == null) {
await channel.watch();
}
Navigator.pushNamed(
context,
Routes.CHANNEL_PAGE,
arguments: ChannelPageArgs(
channel: channel,
initialMessage: message,
),
);
},
),
),
), ),
), ),
); );
@@ -278,35 +249,8 @@ class _ChatInfoScreenState extends State<ChatInfoScreen> {
MaterialPageRoute( MaterialPageRoute(
builder: (context) => StreamChannel( builder: (context) => StreamChannel(
channel: channel, channel: channel,
child: MessageSearchBloc(
child: ChannelMediaDisplayScreen( child: ChannelMediaDisplayScreen(
messageTheme: widget.messageTheme, messageTheme: widget.messageTheme,
sortOptions: [
SortOption(
'created_at',
direction: SortOption.ASC,
),
],
onShowMessage: (m, c) async {
final client = StreamChat.of(context).client;
final message = m;
final channel = client.channel(
c.type,
id: c.id,
);
if (channel.state == null) {
await channel.watch();
}
Navigator.pushNamed(
context,
Routes.CHANNEL_PAGE,
arguments: ChannelPageArgs(
channel: channel,
initialMessage: message,
),
);
},
),
), ),
), ),
), ),
@@ -338,15 +282,8 @@ class _ChatInfoScreenState extends State<ChatInfoScreen> {
MaterialPageRoute( MaterialPageRoute(
builder: (context) => StreamChannel( builder: (context) => StreamChannel(
channel: channel, channel: channel,
child: MessageSearchBloc(
child: ChannelFileDisplayScreen( child: ChannelFileDisplayScreen(
sortOptions: [ messageTheme: widget.messageTheme,
SortOption(
'created_at',
direction: SortOption.ASC,
),
],
),
), ),
), ),
), ),
@@ -40,16 +40,52 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
ValueNotifier<bool?> mutedBool = ValueNotifier(false); ValueNotifier<bool?> mutedBool = ValueNotifier(false);
late final channel = StreamChannel.of(context).channel;
late final userListController = StreamUserListController(
client: StreamChat.of(context).client,
limit: 25,
filter: Filter.and(
[
if (_searchController!.text.isNotEmpty)
Filter.autoComplete('name', _userNameQuery),
Filter.notIn('id', [
StreamChat.of(context).currentUser!.id,
...channel.state!.members
.map<String?>(((e) => e.userId))
.whereType<String>(),
]),
],
),
sort: [
SortOption(
'name',
direction: 1,
),
],
);
void _userNameListener() { void _userNameListener() {
if (_searchController!.text == _userNameQuery) { if (_searchController!.text == _userNameQuery) {
return; return;
} }
if (_debounce?.isActive ?? false) _debounce!.cancel(); if (_debounce?.isActive ?? false) _debounce!.cancel();
_debounce = Timer(const Duration(milliseconds: 350), () { _debounce = Timer(const Duration(milliseconds: 350), () {
if (mounted && modalSetStateCallback != null) { if (mounted) {
modalSetStateCallback!(() {
_userNameQuery = _searchController!.text; _userNameQuery = _searchController!.text;
}); userListController.filter = Filter.and(
[
if (_searchController!.text.isNotEmpty)
Filter.autoComplete('name', _userNameQuery),
Filter.notIn('id', [
StreamChat.of(context).currentUser!.id,
...channel.state!.members
.map<String?>(((e) => e.userId))
.whereType<String>(),
]),
],
);
userListController.doInitialLoad();
} }
}); });
} }
@@ -57,25 +93,28 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
var channel = StreamChannel.of(context);
_nameController = TextEditingController.fromValue( _nameController = TextEditingController.fromValue(
TextEditingValue( TextEditingValue(text: (channel.extraData['name'] as String?) ?? ''),
text: (channel.channel.extraData['name'] as String?) ?? ''),
); );
_searchController = TextEditingController()..addListener(_userNameListener); _searchController = TextEditingController()..addListener(_userNameListener);
_nameController!.addListener(() { _nameController!.addListener(() {
setState(() {}); setState(() {});
}); });
mutedBool = ValueNotifier(StreamChannel.of(context).channel.isMuted); mutedBool = ValueNotifier(channel.isMuted);
}
@override
void dispose() {
userListController.dispose();
super.dispose();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var channel = StreamChannel.of(context);
return StreamBuilder<List<Member>>( return StreamBuilder<List<Member>>(
stream: channel.channel.state!.membersStream, stream: channel.state!.membersStream,
builder: (context, snapshot) { builder: (context, snapshot) {
if (!snapshot.hasData) { if (!snapshot.hasData) {
return Container( return Container(
@@ -94,7 +133,7 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
title: Column( title: Column(
children: [ children: [
StreamBuilder<ChannelState>( StreamBuilder<ChannelState>(
stream: channel.channelStateStream, stream: channel.state?.channelStateStream,
builder: (context, state) { builder: (context, state) {
if (!state.hasData) { if (!state.hasData) {
return Text( return Text(
@@ -131,7 +170,7 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
height: 3.0, height: 3.0,
), ),
Text( Text(
'${channel.channel.memberCount} ${AppLocalizations.of(context).members}, ${snapshot.data?.where((e) => e.user!.online).length ?? 0} ${AppLocalizations.of(context).online}', '${channel.memberCount} ${AppLocalizations.of(context).members}, ${snapshot.data?.where((e) => e.user!.online).length ?? 0} ${AppLocalizations.of(context).online}',
style: TextStyle( style: TextStyle(
color: StreamChatTheme.of(context) color: StreamChatTheme.of(context)
.colorTheme .colorTheme
@@ -144,7 +183,7 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
), ),
centerTitle: true, centerTitle: true,
actions: [ actions: [
if (channel.channel.ownCapabilities if (channel.ownCapabilities
.contains(PermissionType.updateChannelMembers)) .contains(PermissionType.updateChannelMembers))
StreamNeumorphicButton( StreamNeumorphicButton(
child: InkWell( child: InkWell(
@@ -169,7 +208,7 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
height: 8.0, height: 8.0,
color: StreamChatTheme.of(context).colorTheme.disabled, color: StreamChatTheme.of(context).colorTheme.disabled,
), ),
if (channel.channel.ownCapabilities if (channel.ownCapabilities
.contains(PermissionType.updateChannel)) .contains(PermissionType.updateChannel))
_buildNameTile(), _buildNameTile(),
_buildOptionListTiles(), _buildOptionListTiles(),
@@ -336,7 +375,6 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
} }
Widget _buildNameTile() { Widget _buildNameTile() {
var channel = StreamChannel.of(context).channel;
var channelName = (channel.extraData['name'] as String?) ?? ''; var channelName = (channel.extraData['name'] as String?) ?? '';
return Material( return Material(
@@ -412,7 +450,7 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
size: 24.0, size: 24.0,
), ),
onTap: () { onTap: () {
StreamChannel.of(context).channel.update({ channel.update({
'name': _nameController!.text.trim(), 'name': _nameController!.text.trim(),
}).catchError((err) { }).catchError((err) {
setState(() { setState(() {
@@ -432,8 +470,6 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
} }
Widget _buildOptionListTiles() { Widget _buildOptionListTiles() {
var channel = StreamChannel.of(context);
return Column( return Column(
children: [ children: [
// OptionListTile( // OptionListTile(
@@ -448,10 +484,9 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
// ), // ),
// onTap: () {}, // onTap: () {},
// ), // ),
if (channel.channel.ownCapabilities if (channel.ownCapabilities.contains(PermissionType.muteChannel))
.contains(PermissionType.muteChannel))
StreamBuilder<bool>( StreamBuilder<bool>(
stream: StreamChannel.of(context).channel.isMutedStream, stream: channel.isMutedStream,
builder: (context, snapshot) { builder: (context, snapshot) {
mutedBool.value = snapshot.data; mutedBool.value = snapshot.data;
@@ -482,9 +517,9 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
mutedBool.value = val; mutedBool.value = val;
if (snapshot.data!) { if (snapshot.data!) {
channel.channel.unmute(); channel.unmute();
} else { } else {
channel.channel.mute(); channel.mute();
} }
}, },
); );
@@ -517,36 +552,7 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
MaterialPageRoute( MaterialPageRoute(
builder: (context) => StreamChannel( builder: (context) => StreamChannel(
channel: channel, channel: channel,
child: MessageSearchBloc( child: PinnedMessagesScreen(),
child: PinnedMessagesScreen(
messageTheme: widget.messageTheme,
sortOptions: [
SortOption(
'created_at',
direction: SortOption.ASC,
),
],
onShowMessage: (m, c) async {
final client = StreamChat.of(context).client;
final message = m;
final channel = client.channel(
c.type,
id: c.id,
);
if (channel.state == null) {
await channel.watch();
}
Navigator.pushNamed(
context,
Routes.CHANNEL_PAGE,
arguments: ChannelPageArgs(
channel: channel,
initialMessage: message,
),
);
},
),
),
), ),
), ),
); );
@@ -578,36 +584,8 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
MaterialPageRoute( MaterialPageRoute(
builder: (context) => StreamChannel( builder: (context) => StreamChannel(
channel: channel, channel: channel,
child: MessageSearchBloc(
child: ChannelMediaDisplayScreen( child: ChannelMediaDisplayScreen(
messageTheme: widget.messageTheme, messageTheme: widget.messageTheme,
sortOptions: [
SortOption(
'created_at',
direction: SortOption.ASC,
),
],
paginationParams: PaginationParams(limit: 20),
onShowMessage: (m, c) async {
final client = StreamChat.of(context).client;
final message = m;
final channel = client.channel(
c.type,
id: c.id,
);
if (channel.state == null) {
await channel.watch();
}
await Navigator.pushNamed(
context,
Routes.CHANNEL_PAGE,
arguments: ChannelPageArgs(
channel: channel,
initialMessage: message,
),
);
},
),
), ),
), ),
), ),
@@ -640,25 +618,16 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
MaterialPageRoute( MaterialPageRoute(
builder: (context) => StreamChannel( builder: (context) => StreamChannel(
channel: channel, channel: channel,
child: MessageSearchBloc(
child: ChannelFileDisplayScreen( child: ChannelFileDisplayScreen(
sortOptions: [ messageTheme: widget.messageTheme,
SortOption(
'created_at',
direction: SortOption.ASC,
),
],
paginationParams: PaginationParams(limit: 20),
),
), ),
), ),
), ),
); );
}, },
), ),
if (!channel.channel.isDistinct && if (!channel.isDistinct &&
channel.channel.ownCapabilities channel.ownCapabilities.contains(PermissionType.leaveChannel))
.contains(PermissionType.leaveChannel))
StreamOptionListTile( StreamOptionListTile(
tileColor: StreamChatTheme.of(context).colorTheme.appBg, tileColor: StreamChatTheme.of(context).colorTheme.appBg,
separatorColor: StreamChatTheme.of(context).colorTheme.disabled, separatorColor: StreamChatTheme.of(context).colorTheme.disabled,
@@ -703,37 +672,11 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
} }
void _buildAddUserModal(context) { void _buildAddUserModal(context) {
var channel = StreamChannel.of(context).channel;
final userListController = StreamUserListController(
client: channel.client,
limit: 25,
filter: Filter.and(
[
if (_searchController!.text.isNotEmpty)
Filter.autoComplete('name', _userNameQuery),
Filter.notIn('id', [
StreamChat.of(context).currentUser!.id,
...channel.state!.members
.map<String?>(((e) => e.userId))
.whereType<String>(),
]),
],
),
sort: [
SortOption(
'name',
direction: 1,
),
],
);
showDialog( showDialog(
useRootNavigator: false, useRootNavigator: false,
context: context, context: context,
barrierColor: StreamChatTheme.of(context).colorTheme.overlay, barrierColor: StreamChatTheme.of(context).colorTheme.overlay,
builder: (context) { builder: (context) {
return StatefulBuilder(builder: (context, modalSetState) {
modalSetStateCallback = modalSetState;
return Padding( return Padding(
padding: EdgeInsets.only(top: 16.0, left: 8.0, right: 8.0), padding: EdgeInsets.only(top: 16.0, left: 8.0, right: 8.0),
child: Material( child: Material(
@@ -747,10 +690,10 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
children: [ children: [
Padding( Padding(
padding: const EdgeInsets.all(16), padding: const EdgeInsets.all(16),
child: _buildTextInputSection(modalSetState), child: _buildTextInputSection(),
), ),
Expanded( Expanded(
child: StreamUserListView( child: StreamUserGridView(
controller: userListController, controller: userListController,
onUserTap: (user) async { onUserTap: (user) async {
_searchController!.clear(); _searchController!.clear();
@@ -797,12 +740,13 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
), ),
), ),
); );
});
}, },
).then((_) => userListController.dispose()); ).whenComplete(() {
_searchController?.clear();
});
} }
Widget _buildTextInputSection(modalSetState) { Widget _buildTextInputSection() {
final theme = StreamChatTheme.of(context); final theme = StreamChatTheme.of(context);
return Column( return Column(
children: [ children: [
@@ -862,7 +806,6 @@ class _GroupInfoScreenState extends State<GroupInfoScreen> {
} }
void _showUserInfoModal(User? user, bool isUserAdmin) { void _showUserInfoModal(User? user, bool isUserAdmin) {
var channel = StreamChannel.of(context).channel;
final color = StreamChatTheme.of(context).colorTheme.barsBg; final color = StreamChatTheme.of(context).colorTheme.barsBg;
showModalBottomSheet( showModalBottomSheet(
@@ -1,48 +1,18 @@
import 'package:example/localizations.dart'; import 'package:example/localizations.dart';
import 'package:example/routes/routes.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart'; import 'package:stream_chat_flutter/stream_chat_flutter.dart';
import 'package:video_player/video_player.dart';
import 'channel_page.dart';
class PinnedMessagesScreen extends StatefulWidget { class PinnedMessagesScreen extends StatefulWidget {
/// The sorting used for the channels matching the filters.
/// Sorting is based on field and direction, multiple sorting options can be provided.
/// You can sort based on last_updated, last_message_at, updated_at, created_at or member_count.
/// Direction can be ascending or descending.
final List<SortOption>? sortOptions;
/// Pagination parameters
/// limit: the number of users to return (max is 30)
/// offset: the offset (max is 1000)
/// message_limit: how many messages should be included to each channel
final PaginationParams paginationParams;
/// The builder used when the file list is empty.
final WidgetBuilder? emptyBuilder;
final ShowMessageCallback? onShowMessage;
final StreamMessageThemeData messageTheme;
const PinnedMessagesScreen({
required this.messageTheme,
this.sortOptions,
this.paginationParams = const PaginationParams(limit: 20),
this.emptyBuilder,
this.onShowMessage,
});
@override @override
_PinnedMessagesScreenState createState() => _PinnedMessagesScreenState(); State<PinnedMessagesScreen> createState() => _PinnedMessagesScreenState();
} }
class _PinnedMessagesScreenState extends State<PinnedMessagesScreen> { class _PinnedMessagesScreenState extends State<PinnedMessagesScreen> {
Map<String?, VideoPlayerController?> controllerCache = {}; late final controller = StreamMessageSearchListController(
client: StreamChat.of(context).client,
@override
void initState() {
super.initState();
final messageSearchBloc = MessageSearchBloc.of(context);
messageSearchBloc.search(
filter: Filter.in_( filter: Filter.in_(
'cid', 'cid',
[StreamChannel.of(context).channel.cid!], [StreamChannel.of(context).channel.cid!],
@@ -51,10 +21,14 @@ class _PinnedMessagesScreenState extends State<PinnedMessagesScreen> {
'pinned', 'pinned',
true, true,
), ),
sort: widget.sortOptions, sort: [
pagination: widget.paginationParams, SortOption(
'created_at',
direction: SortOption.ASC,
),
],
limit: 20,
); );
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -73,25 +47,9 @@ class _PinnedMessagesScreenState extends State<PinnedMessagesScreen> {
leading: StreamBackButton(), leading: StreamBackButton(),
backgroundColor: StreamChatTheme.of(context).colorTheme.barsBg, backgroundColor: StreamChatTheme.of(context).colorTheme.barsBg,
), ),
body: _buildMediaGrid(), body: StreamMessageSearchListView(
); controller: controller,
} emptyBuilder: (_) {
Widget _buildMediaGrid() {
final messageSearchBloc = MessageSearchBloc.of(context);
return StreamBuilder<List<GetMessageResponse>>(
builder: (context, snapshot) {
if (snapshot.data == null) {
return Center(
child: const CircularProgressIndicator(),
);
}
if (snapshot.data!.isEmpty) {
if (widget.emptyBuilder != null) {
return widget.emptyBuilder!(context);
}
return Center( return Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
@@ -140,74 +98,33 @@ class _PinnedMessagesScreenState extends State<PinnedMessagesScreen> {
], ],
), ),
); );
},
onMessageTap: (messageResponse) async {
final client = StreamChat.of(context).client;
final message = messageResponse.message;
final channel = client.channel(
messageResponse.channel!.type,
id: messageResponse.channel!.id,
);
if (channel.state == null) {
await channel.watch();
} }
Navigator.pushNamed(
var data = snapshot.data ?? []; context,
Routes.CHANNEL_PAGE,
return LazyLoadScrollView( arguments: ChannelPageArgs(
onEndOfPage: () => messageSearchBloc.search( channel: channel,
filter: Filter.in_( initialMessage: message,
'cid',
[StreamChannel.of(context).channel.cid!],
),
messageFilter: Filter.equal(
'pinned',
true,
),
sort: widget.sortOptions,
pagination: widget.paginationParams.copyWith(
offset: messageSearchBloc.messageResponses?.length ?? 0,
),
),
child: ListView.builder(
itemBuilder: (context, position) {
var user = data[position].message.user!;
var attachments = data[position].message.attachments;
var text = data[position].message.text ?? '';
return ListTile(
leading: StreamUserAvatar(
user: user,
constraints: BoxConstraints.tightFor(
width: 40.0,
height: 40.0,
),
borderRadius: BorderRadius.circular(28),
),
title: Text(
user.name,
style: TextStyle(
color: StreamChatTheme.of(context)
.colorTheme
.textHighEmphasis,
fontWeight: FontWeight.bold),
),
subtitle: Text(
text != ''
? text
: (attachments.isNotEmpty
? '${attachments.length} ${attachments.length > 1 ? AppLocalizations.of(context).attachments : AppLocalizations.of(context).attachment}'
: ''),
),
onTap: () {
widget.onShowMessage?.call(data[position].message,
StreamChannel.of(context).channel);
},
);
},
itemCount: snapshot.data!.length,
), ),
); );
}, },
stream: messageSearchBloc.messagesStream, ),
); );
} }
@override @override
void dispose() { void dispose() {
controller.dispose();
super.dispose(); super.dispose();
for (var c in controllerCache.values) {
c!.dispose();
}
} }
} }
+3 -2
View File
@@ -19,12 +19,13 @@ class ThreadPage extends StatefulWidget {
class _ThreadPageState extends State<ThreadPage> { class _ThreadPageState extends State<ThreadPage> {
FocusNode _focusNode = FocusNode(); FocusNode _focusNode = FocusNode();
late MessageInputController _messageInputController; late StreamMessageInputController _messageInputController;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_messageInputController = MessageInputController(message: widget.parent); _messageInputController =
StreamMessageInputController(message: widget.parent);
} }
@override @override