Skip to content

Commit

Permalink
Merge pull request #1728 from GetStream/feat/stream-channel-builders
Browse files Browse the repository at this point in the history
  • Loading branch information
xsahil03x authored Sep 6, 2023
2 parents 7625257 + 7cbdb3b commit 4c19fed
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
14 changes: 11 additions & 3 deletions packages/stream_chat_flutter_core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Upcoming

- Added support for `StreamChannel.loadingBuilder` and `StreamChannel.errorBuilder` to customize
loading and error states.

## 6.8.0

- Updated minimum supported `SDK` version to Flutter 3.10/Dart 3.0
Expand Down Expand Up @@ -35,9 +40,11 @@

- Updated `dart` sdk environment range to support `3.0.0`.
- Updated `stream_chat` dependency to [`6.1.0`](https://pub.dev/packages/stream_chat/changelog).
- [[#1356]](https://github.com/GetStream/stream-chat-flutter/issues/1356) Channel doesn't auto display again after being
- [[#1356]](https://github.com/GetStream/stream-chat-flutter/issues/1356) Channel doesn't auto
display again after being
hidden.
- [[#1540]](https://github.com/GetStream/stream-chat-flutter/issues/1540) Use `CircularProgressIndicator.adaptive`
- [[#1540]](https://github.com/GetStream/stream-chat-flutter/issues/1540)
Use `CircularProgressIndicator.adaptive`
instead of material indicator.

## 6.0.0
Expand All @@ -56,7 +63,8 @@

## 5.1.0

- Deprecated the `sort` parameter in the `StreamChannelListController` in favor of `channelStateSort`.
- Deprecated the `sort` parameter in the `StreamChannelListController` in favor
of `channelStateSort`.

## 5.0.0

Expand Down
52 changes: 40 additions & 12 deletions packages/stream_chat_flutter_core/lib/src/stream_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ enum QueryDirection {
bottom,
}

/// Signature used by [StreamChannel.errorBuilder] to create a replacement
/// widget for an error that occurs while asynchronously building the channel.
// TODO: Remove once ErrorBuilder supports passing stacktrace.
typedef ErrorWidgetBuilder = Widget Function(
BuildContext context,
Object error,
StackTrace? stackTrace,
);

/// Widget used to provide information about the channel to the widget tree
///
/// Use [StreamChannel.of] to get the current [StreamChannelState] instance.
Expand All @@ -27,6 +36,8 @@ class StreamChannel extends StatefulWidget {
required this.channel,
this.showLoading = true,
this.initialMessageId,
this.errorBuilder = _defaultErrorBuilder,
this.loadingBuilder = _defaultLoadingBuilder,
});

/// The child of the widget
Expand All @@ -41,6 +52,31 @@ class StreamChannel extends StatefulWidget {
/// If passed the channel will load from this particular message.
final String? initialMessageId;

/// Widget builder used in case the channel is initialising.
final WidgetBuilder loadingBuilder;

/// Widget builder used in case an error occurs while building the channel.
final ErrorWidgetBuilder errorBuilder;

static Widget _defaultLoadingBuilder(BuildContext context) {
return const Center(child: CircularProgressIndicator.adaptive());
}

static Widget _defaultErrorBuilder(
BuildContext context,
Object error,
StackTrace? stackTrace,
) {
if (error is DioException) {
if (error.type == DioExceptionType.badResponse) {
return Center(child: Text(error.message ?? 'Bad response'));
}
return const Center(child: Text('Check your connection and retry'));
}

return Center(child: Text(error.toString()));
}

/// Use this method to get the current [StreamChannelState] instance
static StreamChannelState of(BuildContext context) {
StreamChannelState? streamChannelState;
Expand Down Expand Up @@ -430,22 +466,14 @@ class StreamChannelState extends State<StreamChannel> {
],
builder: (context, snapshot) {
if (snapshot.hasError) {
final error = snapshot.error;
if (error is DioException) {
if (error.type == DioExceptionType.badResponse) {
return Center(child: Text(error.message ?? 'Bad response'));
}
return const Center(child: Text('Check your connection and retry'));
}

return Center(child: Text(error.toString()));
final error = snapshot.error!;
final stackTrace = snapshot.stackTrace;
return widget.errorBuilder(context, error, stackTrace);
}

final dataLoaded = snapshot.data?.every((it) => it) == true;
if (widget.showLoading && !dataLoaded) {
return const Center(
child: CircularProgressIndicator.adaptive(),
);
return widget.loadingBuilder(context);
}
return widget.child;
},
Expand Down
1 change: 1 addition & 0 deletions packages/stream_chat_flutter_core/lib/src/typedef.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:stream_chat/stream_chat.dart';
/// A signature for a callback which exposes an error and returns a function.
/// This Callback can be used in cases where an API failure occurs and the
/// widget is unable to render data.
// TODO: Add stacktrace as a parameter in v7.0.0
typedef ErrorBuilder = Widget Function(BuildContext context, Object error);

/// A Signature for a handler function which will expose a [event].
Expand Down

0 comments on commit 4c19fed

Please sign in to comment.