Files
pipelines/app/lib/features/chat/conversation/chat_transcript_scroll_view.dart

65 lines
2.3 KiB
Dart

import 'package:flutter/widgets.dart';
/// The transcript's scrollable: older rows above a centre, newer rows below.
///
/// With [splitAfter], the children up to and including the one with that key
/// sit above the centre and the rest below it. The centre is at the viewport's
/// bottom edge at offset zero: the older children grow upwards from it and the
/// newer ones downwards, so the newest rows are laid out first and an older row
/// only when it is scrolled to. Offsets above the centre are negative.
///
/// Without one (or when no child has that key) this is a plain top-anchored
/// list starting at its first child. A transcript that fits its viewport stays
/// top-anchored, so a short conversation still reads from the top.
///
/// [padding] keeps the first child below the floating bar and the last child
/// above the floating composer, as a list's padding would.
class ChatTranscriptScrollView extends StatelessWidget {
const ChatTranscriptScrollView({
super.key,
required this.controller,
required this.padding,
required this.children,
this.splitAfter,
});
static const _centre = ValueKey<String>('chat-thread-newer');
final ScrollController controller;
final EdgeInsets padding;
final List<Widget> children;
final Key? splitAfter;
@override
Widget build(BuildContext context) {
final split = splitAfter == null
? -1
: children.indexWhere((child) => child.key == splitAfter);
final anchored = split >= 0;
return CustomScrollView(
controller: controller,
center: _centre,
anchor: anchored ? 1 : 0,
slivers: [
// Before the centre, a sliver grows upwards: its first child sits at
// the centre, so the older children go in newest-first.
SliverPadding(
padding: anchored ? padding.copyWith(bottom: 0) : EdgeInsets.zero,
sliver: SliverList(
delegate: SliverChildListDelegate([
for (var index = split; index >= 0; index--) children[index],
]),
),
),
SliverPadding(
key: _centre,
padding: anchored ? padding.copyWith(top: 0) : padding,
sliver: SliverList(
delegate: SliverChildListDelegate(children.sublist(split + 1)),
),
),
],
);
}
}