|
| 1 | +// Copyright 2025 LiveKit, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import 'dart:async'; |
| 16 | + |
| 17 | +import 'package:flutter/material.dart'; |
| 18 | + |
| 19 | +import 'package:livekit_client/livekit_client.dart'; |
| 20 | + |
| 21 | +import '../../context/session_context.dart'; |
| 22 | + |
| 23 | +/// A scrollable list that renders [Session.messages] with newest messages at |
| 24 | +/// the bottom and auto-scrolls when new messages arrive. |
| 25 | +/// |
| 26 | +/// Provide a [Session] via [session] or a surrounding [SessionContext]. Use |
| 27 | +/// [messageBuilder] to render each [ReceivedMessage]; the builder runs in |
| 28 | +/// reverse order so index `0` corresponds to the latest message. |
| 29 | +class ChatScrollView extends StatefulWidget { |
| 30 | + const ChatScrollView({ |
| 31 | + super.key, |
| 32 | + required this.messageBuilder, |
| 33 | + this.session, |
| 34 | + this.autoScroll = true, |
| 35 | + this.scrollController, |
| 36 | + this.padding, |
| 37 | + this.physics, |
| 38 | + }); |
| 39 | + |
| 40 | + /// Optional session instance. If omitted, [SessionContext.of] is used. |
| 41 | + final Session? session; |
| 42 | + |
| 43 | + /// Builder for each message. |
| 44 | + final Widget Function(BuildContext context, ReceivedMessage message) messageBuilder; |
| 45 | + |
| 46 | + /// Whether the list should automatically scroll to the latest message when |
| 47 | + /// the message count changes. |
| 48 | + final bool autoScroll; |
| 49 | + |
| 50 | + /// Optional scroll controller. If not provided, an internal controller is |
| 51 | + /// created and disposed automatically. |
| 52 | + final ScrollController? scrollController; |
| 53 | + |
| 54 | + /// Optional padding applied to the list. |
| 55 | + final EdgeInsetsGeometry? padding; |
| 56 | + |
| 57 | + /// Optional scroll physics. |
| 58 | + final ScrollPhysics? physics; |
| 59 | + |
| 60 | + @override |
| 61 | + State<ChatScrollView> createState() => _ChatScrollViewState(); |
| 62 | +} |
| 63 | + |
| 64 | +class _ChatScrollViewState extends State<ChatScrollView> { |
| 65 | + ScrollController? _internalController; |
| 66 | + int _lastMessageCount = 0; |
| 67 | + |
| 68 | + ScrollController get _controller => widget.scrollController ?? _internalController!; |
| 69 | + |
| 70 | + @override |
| 71 | + void initState() { |
| 72 | + super.initState(); |
| 73 | + _internalController = widget.scrollController ?? ScrollController(); |
| 74 | + } |
| 75 | + |
| 76 | + @override |
| 77 | + void didUpdateWidget(ChatScrollView oldWidget) { |
| 78 | + super.didUpdateWidget(oldWidget); |
| 79 | + if (oldWidget.scrollController != widget.scrollController) { |
| 80 | + _internalController?.dispose(); |
| 81 | + _internalController = widget.scrollController ?? ScrollController(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @override |
| 86 | + void dispose() { |
| 87 | + if (widget.scrollController == null) { |
| 88 | + _internalController?.dispose(); |
| 89 | + } |
| 90 | + super.dispose(); |
| 91 | + } |
| 92 | + |
| 93 | + Session _resolveSession(BuildContext context) { |
| 94 | + return widget.session ?? SessionContext.of(context); |
| 95 | + } |
| 96 | + |
| 97 | + void _autoScrollIfNeeded(List<ReceivedMessage> messages) { |
| 98 | + if (!widget.autoScroll) { |
| 99 | + _lastMessageCount = messages.length; |
| 100 | + return; |
| 101 | + } |
| 102 | + if (messages.length == _lastMessageCount) { |
| 103 | + return; |
| 104 | + } |
| 105 | + _lastMessageCount = messages.length; |
| 106 | + WidgetsBinding.instance.addPostFrameCallback((_) { |
| 107 | + if (!mounted) { |
| 108 | + return; |
| 109 | + } |
| 110 | + if (!_controller.hasClients) { |
| 111 | + return; |
| 112 | + } |
| 113 | + unawaited(_controller.animateTo( |
| 114 | + 0, |
| 115 | + duration: const Duration(milliseconds: 200), |
| 116 | + curve: Curves.easeOut, |
| 117 | + )); |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + @override |
| 122 | + Widget build(BuildContext context) { |
| 123 | + final session = _resolveSession(context); |
| 124 | + |
| 125 | + return AnimatedBuilder( |
| 126 | + animation: session, |
| 127 | + builder: (context, _) { |
| 128 | + final messages = [...session.messages]..sort((a, b) => a.timestamp.compareTo(b.timestamp)); |
| 129 | + _autoScrollIfNeeded(messages); |
| 130 | + |
| 131 | + return ListView.builder( |
| 132 | + reverse: true, |
| 133 | + controller: _controller, |
| 134 | + padding: widget.padding, |
| 135 | + physics: widget.physics, |
| 136 | + itemCount: messages.length, |
| 137 | + itemBuilder: (context, index) { |
| 138 | + final message = messages[messages.length - 1 - index]; |
| 139 | + return widget.messageBuilder(context, message); |
| 140 | + }, |
| 141 | + ); |
| 142 | + }, |
| 143 | + ); |
| 144 | + } |
| 145 | +} |
0 commit comments