import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../adaptive/host.dart'; import '../motion/motion.dart'; import 'section_heading.dart'; /// A titled group in a list: a [SectionHeading] strip (bold title, count, /// chevron) that reads as a divider, and opens or closes its rows on a tap. class CollapsibleListSection extends StatefulWidget { const CollapsibleListSection({ super.key, required this.title, required this.children, this.count, this.initiallyOpen = true, }); final String title; final int? count; final bool initiallyOpen; final List children; @override State createState() => _CollapsibleListSectionState(); } class _CollapsibleListSectionState extends State { late bool _open = widget.initiallyOpen; @override Widget build(BuildContext context) { final cupertino = usesCupertinoHost(context); final muted = cupertino ? CupertinoColors.secondaryLabel.resolveFrom(context) : Theme.of(context).colorScheme.onSurfaceVariant; final heading = 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, ), ), ], ), ), ), ); final rows = _open ? Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: widget.children, ) : const SizedBox(width: double.infinity); final duration = Motion.of(context, Motion.standard); return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Padding(padding: const EdgeInsets.only(top: 8), child: heading), ClipRect( // A zero-duration AnimatedSize can notify during its own layout when // rows arrive asynchronously. Reduced motion should skip it entirely. child: duration == Duration.zero ? rows : AnimatedSize( duration: duration, curve: Motion.curve, alignment: Alignment.topCenter, child: rows, ), ), ], ); } void _toggle() => setState(() => _open = !_open); }