import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../adaptive/host.dart'; import '../motion/motion.dart'; import 'section_heading.dart'; import 'sliver_animated_extent.dart'; /// A titled group in a [CustomScrollView]: a [SectionHeading] strip (bold /// title, count, chevron) that reads as a divider, and opens or closes its /// rows on a tap. /// /// The rows are a lazy sliver list: only the visible ones are built, however /// many there are. The section grows and shrinks like a clipped, top-aligned /// `AnimatedSize` when it opens or closes and when rows arrive or leave. class SliverCollapsibleListSection extends StatefulWidget { const SliverCollapsibleListSection({ super.key, required this.title, required this.itemCount, required this.itemBuilder, this.itemKey, this.count, this.initiallyOpen = true, this.onOpenChanged, }); final String title; final int? count; final bool initiallyOpen; /// Reports the operator opening or folding the section. final ValueChanged? onOpenChanged; final int itemCount; final IndexedWidgetBuilder itemBuilder; /// The key of the item at an index, so a row keeps its element when rows /// before it arrive or leave. final Key Function(int index)? itemKey; @override State createState() => _SliverCollapsibleListSectionState(); } class _SliverCollapsibleListSectionState extends State { late bool _open = widget.initiallyOpen; @override Widget build(BuildContext context) { final duration = Motion.of(context, Motion.standard); return SliverMainAxisGroup( slivers: [ SliverToBoxAdapter( child: Padding( padding: const EdgeInsets.only(top: 8), child: _heading(context), ), ), SliverAnimatedExtent( duration: duration, curve: Motion.curve, changeToken: (_open, widget.itemCount), sliver: _open ? _rows() : const SliverToBoxAdapter(), ), ], ); } Widget _rows() { final itemKey = widget.itemKey; Map? indexOf; return SliverList( delegate: SliverChildBuilderDelegate( (context, index) => itemKey == null ? widget.itemBuilder(context, index) : KeyedSubtree( key: itemKey(index), child: widget.itemBuilder(context, index), ), childCount: widget.itemCount, findChildIndexCallback: itemKey == null ? null : (key) => (indexOf ??= { for (var i = 0; i < widget.itemCount; i++) itemKey(i): i, })[key], ), ); } Widget _heading(BuildContext context) { final cupertino = usesCupertinoHost(context); final muted = cupertino ? CupertinoColors.secondaryLabel.resolveFrom(context) : Theme.of(context).colorScheme.onSurfaceVariant; return Semantics( container: true, header: true, button: true, expanded: _open, label: widget.title, excludeSemantics: true, onTap: _toggle, child: GestureDetector( behavior: HitTestBehavior.opaque, onTap: _toggle, child: MouseRegion( cursor: SystemMouseCursors.click, child: SectionHeading( title: widget.title, minHeight: 44, trailing: [ if (widget.count != null) Padding( padding: const EdgeInsetsDirectional.only(end: 6), child: Text( '${widget.count}', style: TextStyle(color: muted), ), ), AnimatedRotation( turns: _open ? 0.5 : 0, duration: Motion.of(context, Motion.standard), curve: Motion.curve, child: Icon( cupertino ? CupertinoIcons.chevron_down : Icons.expand_more, color: muted, ), ), ], ), ), ), ); } void _toggle() { setState(() => _open = !_open); widget.onOpenChanged?.call(_open); } }