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> {
|
class _MessageSearchListViewState extends State<MessageSearchListView> {
|
||||||
@override
|
MessageSearchListController _messageSearchListController =
|
||||||
void initState() {
|
MessageSearchListController();
|
||||||
super.initState();
|
|
||||||
final messageSearchBloc = MessageSearchBloc.of(context);
|
|
||||||
messageSearchBloc.search(
|
|
||||||
filter: widget.filters,
|
|
||||||
sort: widget.sortOptions,
|
|
||||||
query: widget.messageQuery,
|
|
||||||
pagination: widget.paginationParams,
|
|
||||||
messageFilter: widget.messageFilters,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final messageSearchBloc = MessageSearchBloc.of(context);
|
return MessageSearchListCore(
|
||||||
return _buildListView(messageSearchBloc);
|
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);
|
||||||
}
|
}
|
||||||
|
return LayoutBuilder(
|
||||||
Widget _separatorBuilder(BuildContext context, int index) {
|
builder: (context, viewportConstraints) {
|
||||||
return Container(
|
return SingleChildScrollView(
|
||||||
height: 1,
|
physics: AlwaysScrollableScrollPhysics(),
|
||||||
color: StreamChatTheme.of(context).colorTheme.greyWhisper,
|
child: ConstrainedBox(
|
||||||
);
|
constraints: BoxConstraints(
|
||||||
}
|
minHeight: viewportConstraints.maxHeight,
|
||||||
|
),
|
||||||
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),
|
|
||||||
child: Center(
|
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(),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
});
|
},
|
||||||
}
|
errorBuilder: (error) {
|
||||||
|
if (error is Error) {
|
||||||
Widget _buildListView(MessageSearchBlocState messageSearchBloc) {
|
print((error).stackTrace);
|
||||||
return StreamBuilder<List<GetMessageResponse>>(
|
|
||||||
stream: messageSearchBloc.messagesStream,
|
|
||||||
builder: (context, snapshot) {
|
|
||||||
if (snapshot.hasError) {
|
|
||||||
if (snapshot.error is Error) {
|
|
||||||
print((snapshot.error as Error).stackTrace);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (widget.errorBuilder != null) {
|
if (widget.errorBuilder != null) {
|
||||||
return widget.errorBuilder(snapshot.error);
|
return widget.errorBuilder(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
var message = snapshot.error.toString();
|
var message = error.toString();
|
||||||
if (snapshot.error is DioError) {
|
if (error is DioError) {
|
||||||
final dioError = snapshot.error as DioError;
|
final dioError = error as DioError;
|
||||||
if (dioError.type == DioErrorType.RESPONSE) {
|
if (dioError.type == DioErrorType.RESPONSE) {
|
||||||
message = dioError.message;
|
message = dioError.message;
|
||||||
} else {
|
} else {
|
||||||
@@ -237,13 +198,7 @@ class _MessageSearchListViewState extends State<MessageSearchListView> {
|
|||||||
),
|
),
|
||||||
RaisedButton(
|
RaisedButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
messageSearchBloc.search(
|
_messageSearchListController.loadData();
|
||||||
filter: widget.filters,
|
|
||||||
sort: widget.sortOptions,
|
|
||||||
query: widget.messageQuery,
|
|
||||||
pagination: widget.paginationParams,
|
|
||||||
messageFilter: widget.messageFilters,
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
child: Text('Retry'),
|
child: Text('Retry'),
|
||||||
),
|
),
|
||||||
@@ -251,9 +206,8 @@ class _MessageSearchListViewState extends State<MessageSearchListView> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
},
|
||||||
|
loadingBuilder: (context) {
|
||||||
if (!snapshot.hasData) {
|
|
||||||
return LayoutBuilder(
|
return LayoutBuilder(
|
||||||
builder: (context, viewportConstraints) {
|
builder: (context, viewportConstraints) {
|
||||||
return SingleChildScrollView(
|
return SingleChildScrollView(
|
||||||
@@ -269,31 +223,65 @@ class _MessageSearchListViewState extends State<MessageSearchListView> {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
},
|
||||||
|
childBuilder: (list) {
|
||||||
final items = snapshot.data;
|
return _buildListView(list);
|
||||||
|
|
||||||
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'),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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(
|
Widget child = ListView.separated(
|
||||||
physics: AlwaysScrollableScrollPhysics(),
|
physics: AlwaysScrollableScrollPhysics(),
|
||||||
itemCount: items.isNotEmpty ? items.length + 1 : items.length,
|
itemCount: items.isNotEmpty ? items.length + 1 : items.length,
|
||||||
@@ -307,32 +295,22 @@ class _MessageSearchListViewState extends State<MessageSearchListView> {
|
|||||||
if (index < items.length) {
|
if (index < items.length) {
|
||||||
return _listItemBuilder(context, items[index]);
|
return _listItemBuilder(context, items[index]);
|
||||||
}
|
}
|
||||||
return _buildQueryProgressIndicator(context, messageSearchBloc);
|
return _buildQueryProgressIndicator(context);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
if (widget.pullToRefresh) {
|
if (widget.pullToRefresh) {
|
||||||
child = RefreshIndicator(
|
child = RefreshIndicator(
|
||||||
onRefresh: () async => messageSearchBloc.search(
|
onRefresh: () async {
|
||||||
filter: widget.filters,
|
_messageSearchListController.loadData();
|
||||||
sort: widget.sortOptions,
|
},
|
||||||
query: widget.messageQuery,
|
|
||||||
pagination: widget.paginationParams,
|
|
||||||
messageFilter: widget.messageFilters,
|
|
||||||
),
|
|
||||||
child: child,
|
child: child,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
child = LazyLoadScrollView(
|
child = LazyLoadScrollView(
|
||||||
onEndOfPage: () => messageSearchBloc.loadMore(
|
onEndOfPage: () async {
|
||||||
filter: widget.filters,
|
return _messageSearchListController.paginateData();
|
||||||
sort: widget.sortOptions,
|
},
|
||||||
pagination: widget.paginationParams.copyWith(
|
|
||||||
offset: messageSearchBloc.messageResponses?.length ?? 0,
|
|
||||||
),
|
|
||||||
query: widget.messageQuery,
|
|
||||||
messageFilter: widget.messageFilters,
|
|
||||||
),
|
|
||||||
child: child,
|
child: child,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -362,28 +340,5 @@ class _MessageSearchListViewState extends State<MessageSearchListView> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
return child;
|
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