Skip to content

Commit a3fd1c4

Browse files
committed
Add scroll to the first unread docs
1 parent c575685 commit a3fd1c4

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

doc/advanced-usage.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,47 @@ class _MyHomePageState extends State<MyHomePage> {
555555
## User avatars & names
556556

557557
To show user avatars & names use `showUserAvatars` and `showUserNames` parameters. Can be used separately. By default, the chat will select one of 10 provided colors as an avatar background and name text color. Color is calculated based on the user's `id` hash code, so it is unique in different rooms. To modify provided colors use `userAvatarNameColors` parameter in [theme](themes). If you want to have one color for everyone, just pass this color as a single item in the `userAvatarNameColors` list.
558+
559+
## Scroll to the first unread
560+
561+
### Without pagination
562+
563+
Just pass this parameter to the `Chat` widget. You need to pass last read message ID (banner will be shown automatically, after the message with the same ID).
564+
565+
```dart
566+
scrollToUnreadOptions: const ScrollToUnreadOptions(
567+
lastReadMessageId: 'lastReadMessageId',
568+
scrollOnOpen: true,
569+
)
570+
```
571+
572+
### With pagination
573+
574+
You will need to use `GlobalKey` to do this. First, create a `GlobalKey` for the `ChatState`
575+
576+
```dart
577+
final GlobalKey<ChatState> _chatKey = GlobalKey();
578+
```
579+
580+
and pass it to the `Chat` widget.
581+
582+
```dart
583+
Chat(
584+
key: _chatKey,
585+
// ...
586+
)
587+
```
588+
589+
Now, you still need to pass `scrollToUnreadOptions` with the `lastReadMessageId`, but keep the `scrollOnOpen` parameter as a default `false`. In your pagination code (in the example I have in [pagination section](#pagination) that would be end of the `_handleEndReached` function) you need to keep fetching pages until you find a page that contains `lastReadMessageId`, then using the `_chatKey` you will be able to scroll to the first unread message.
590+
591+
```dart
592+
if (_messages.where((e) => e.id == 'lastReadMessageId').isEmpty) {
593+
// Recursively call to fetch more pages
594+
await _handleEndReached();
595+
} else {
596+
// Give some delay for the library to calculate correct indices
597+
Future.delayed(const Duration(milliseconds: 20), () {
598+
_chatKey.currentState?.scrollToUnreadHeader();
599+
});
600+
}
601+
```

0 commit comments

Comments
 (0)