feat: Converted message_search_list_view.dart
This commit is contained in:
@@ -119,89 +119,50 @@ class MessageSearchListView extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _MessageSearchListViewState extends State<MessageSearchListView> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final messageSearchBloc = MessageSearchBloc.of(context);
|
||||
messageSearchBloc.search(
|
||||
filter: widget.filters,
|
||||
sort: widget.sortOptions,
|
||||
query: widget.messageQuery,
|
||||
pagination: widget.paginationParams,
|
||||
messageFilter: widget.messageFilters,
|
||||
);
|
||||
}
|
||||
MessageSearchListController _messageSearchListController =
|
||||
MessageSearchListController();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final messageSearchBloc = MessageSearchBloc.of(context);
|
||||
return _buildListView(messageSearchBloc);
|
||||
return MessageSearchListCore(
|
||||
filters: widget.filters,
|
||||
sortOptions: widget.sortOptions,
|
||||
messageQuery: widget.messageQuery,
|
||||
paginationParams: widget.paginationParams,
|
||||
messageFilters: widget.messageFilters,
|
||||
messageSearchListController: _messageSearchListController,
|
||||
emptyBuilder: (context) {
|
||||
if (widget.emptyBuilder != null) {
|
||||
return widget.emptyBuilder(context, widget.messageQuery);
|
||||
}
|
||||
|
||||
Widget _separatorBuilder(BuildContext context, int index) {
|
||||
return Container(
|
||||
height: 1,
|
||||
color: StreamChatTheme.of(context).colorTheme.greyWhisper,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _listItemBuilder(
|
||||
BuildContext context, GetMessageResponse getMessageResponse) {
|
||||
if (widget.itemBuilder != null) {
|
||||
return widget.itemBuilder(context, getMessageResponse);
|
||||
}
|
||||
return MessageSearchItem(
|
||||
getMessageResponse: getMessageResponse,
|
||||
onTap: () => widget.onItemTap(getMessageResponse),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildQueryProgressIndicator(
|
||||
context, MessageSearchBlocState messageSearchBloc) {
|
||||
return StreamBuilder<bool>(
|
||||
stream: messageSearchBloc.queryMessagesLoading,
|
||||
initialData: false,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasError) {
|
||||
return Container(
|
||||
color: StreamChatTheme.of(context)
|
||||
.colorTheme
|
||||
.accentRed
|
||||
.withOpacity(.2),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
||||
return LayoutBuilder(
|
||||
builder: (context, viewportConstraints) {
|
||||
return SingleChildScrollView(
|
||||
physics: AlwaysScrollableScrollPhysics(),
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(
|
||||
minHeight: viewportConstraints.maxHeight,
|
||||
),
|
||||
child: Center(
|
||||
child: Text('Error loading messages'),
|
||||
child: Text('There are no messages currently'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return Container(
|
||||
height: 100,
|
||||
padding: EdgeInsets.all(32),
|
||||
child: Center(
|
||||
child: snapshot.data ? CircularProgressIndicator() : Container(),
|
||||
),
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Widget _buildListView(MessageSearchBlocState messageSearchBloc) {
|
||||
return StreamBuilder<List<GetMessageResponse>>(
|
||||
stream: messageSearchBloc.messagesStream,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasError) {
|
||||
if (snapshot.error is Error) {
|
||||
print((snapshot.error as Error).stackTrace);
|
||||
},
|
||||
errorBuilder: (error) {
|
||||
if (error is Error) {
|
||||
print((error).stackTrace);
|
||||
}
|
||||
|
||||
if (widget.errorBuilder != null) {
|
||||
return widget.errorBuilder(snapshot.error);
|
||||
return widget.errorBuilder(error);
|
||||
}
|
||||
|
||||
var message = snapshot.error.toString();
|
||||
if (snapshot.error is DioError) {
|
||||
final dioError = snapshot.error as DioError;
|
||||
var message = error.toString();
|
||||
if (error is DioError) {
|
||||
final dioError = error as DioError;
|
||||
if (dioError.type == DioErrorType.RESPONSE) {
|
||||
message = dioError.message;
|
||||
} else {
|
||||
@@ -237,13 +198,7 @@ class _MessageSearchListViewState extends State<MessageSearchListView> {
|
||||
),
|
||||
RaisedButton(
|
||||
onPressed: () {
|
||||
messageSearchBloc.search(
|
||||
filter: widget.filters,
|
||||
sort: widget.sortOptions,
|
||||
query: widget.messageQuery,
|
||||
pagination: widget.paginationParams,
|
||||
messageFilter: widget.messageFilters,
|
||||
);
|
||||
_messageSearchListController.loadData();
|
||||
},
|
||||
child: Text('Retry'),
|
||||
),
|
||||
@@ -251,9 +206,8 @@ class _MessageSearchListViewState extends State<MessageSearchListView> {
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (!snapshot.hasData) {
|
||||
},
|
||||
loadingBuilder: (context) {
|
||||
return LayoutBuilder(
|
||||
builder: (context, viewportConstraints) {
|
||||
return SingleChildScrollView(
|
||||
@@ -269,31 +223,65 @@ class _MessageSearchListViewState extends State<MessageSearchListView> {
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
final items = snapshot.data;
|
||||
|
||||
if (items.isEmpty) {
|
||||
if (widget.emptyBuilder != null) {
|
||||
return widget.emptyBuilder(context, widget.messageQuery);
|
||||
}
|
||||
return LayoutBuilder(
|
||||
builder: (context, viewportConstraints) {
|
||||
return SingleChildScrollView(
|
||||
physics: AlwaysScrollableScrollPhysics(),
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(
|
||||
minHeight: viewportConstraints.maxHeight,
|
||||
),
|
||||
child: Center(
|
||||
child: Text('There are no messages currently'),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
childBuilder: (list) {
|
||||
return _buildListView(list);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _separatorBuilder(BuildContext context, int index) {
|
||||
return Container(
|
||||
height: 1,
|
||||
color: StreamChatTheme.of(context).colorTheme.greyWhisper,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _listItemBuilder(
|
||||
BuildContext context, GetMessageResponse getMessageResponse) {
|
||||
if (widget.itemBuilder != null) {
|
||||
return widget.itemBuilder(context, getMessageResponse);
|
||||
}
|
||||
return MessageSearchItem(
|
||||
getMessageResponse: getMessageResponse,
|
||||
onTap: () => widget.onItemTap(getMessageResponse),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildQueryProgressIndicator(context) {
|
||||
MessageSearchBlocState messageSearchBloc = MessageSearchBloc.of(context);
|
||||
|
||||
return StreamBuilder<bool>(
|
||||
stream: messageSearchBloc.queryMessagesLoading,
|
||||
initialData: false,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasError) {
|
||||
return Container(
|
||||
color: StreamChatTheme.of(context)
|
||||
.colorTheme
|
||||
.accentRed
|
||||
.withOpacity(.2),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
||||
child: Center(
|
||||
child: Text('Error loading messages'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return Container(
|
||||
height: 100,
|
||||
padding: EdgeInsets.all(32),
|
||||
child: Center(
|
||||
child: snapshot.data ? CircularProgressIndicator() : Container(),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Widget _buildListView(List<GetMessageResponse> data) {
|
||||
final items = data;
|
||||
|
||||
Widget child = ListView.separated(
|
||||
physics: AlwaysScrollableScrollPhysics(),
|
||||
itemCount: items.isNotEmpty ? items.length + 1 : items.length,
|
||||
@@ -307,32 +295,22 @@ class _MessageSearchListViewState extends State<MessageSearchListView> {
|
||||
if (index < items.length) {
|
||||
return _listItemBuilder(context, items[index]);
|
||||
}
|
||||
return _buildQueryProgressIndicator(context, messageSearchBloc);
|
||||
return _buildQueryProgressIndicator(context);
|
||||
},
|
||||
);
|
||||
if (widget.pullToRefresh) {
|
||||
child = RefreshIndicator(
|
||||
onRefresh: () async => messageSearchBloc.search(
|
||||
filter: widget.filters,
|
||||
sort: widget.sortOptions,
|
||||
query: widget.messageQuery,
|
||||
pagination: widget.paginationParams,
|
||||
messageFilter: widget.messageFilters,
|
||||
),
|
||||
onRefresh: () async {
|
||||
_messageSearchListController.loadData();
|
||||
},
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
|
||||
child = LazyLoadScrollView(
|
||||
onEndOfPage: () => messageSearchBloc.loadMore(
|
||||
filter: widget.filters,
|
||||
sort: widget.sortOptions,
|
||||
pagination: widget.paginationParams.copyWith(
|
||||
offset: messageSearchBloc.messageResponses?.length ?? 0,
|
||||
),
|
||||
query: widget.messageQuery,
|
||||
messageFilter: widget.messageFilters,
|
||||
),
|
||||
onEndOfPage: () async {
|
||||
return _messageSearchListController.paginateData();
|
||||
},
|
||||
child: child,
|
||||
);
|
||||
|
||||
@@ -362,28 +340,5 @@ class _MessageSearchListViewState extends State<MessageSearchListView> {
|
||||
);
|
||||
}
|
||||
return child;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(MessageSearchListView oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.filters?.toString() != oldWidget.filters?.toString() ||
|
||||
jsonEncode(widget.sortOptions) != jsonEncode(oldWidget.sortOptions) ||
|
||||
widget.paginationParams?.toJson()?.toString() !=
|
||||
oldWidget.paginationParams?.toJson()?.toString() ||
|
||||
widget.messageQuery?.toString() != oldWidget.messageQuery?.toString() ||
|
||||
widget.messageFilters?.toString() !=
|
||||
oldWidget.messageFilters?.toString()) {
|
||||
final messageSearchBloc = MessageSearchBloc.of(context);
|
||||
messageSearchBloc.search(
|
||||
filter: widget.filters,
|
||||
sort: widget.sortOptions,
|
||||
query: widget.messageQuery,
|
||||
pagination: widget.paginationParams,
|
||||
messageFilter: widget.messageFilters,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user