import 'dart:math' as math; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../adaptive/host.dart'; /// A collapsible section for a [CustomScrollView]: its title stays pinned at /// the top while its content scrolls under it, so it can always be closed /// without scrolling back. Tapping the title opens or closes it. class StickySection extends StatelessWidget { const StickySection({ super.key, required this.title, required this.open, required this.onToggle, required this.body, this.trailing = const [], this.titleColor, }); final String title; final bool open; final VoidCallback onToggle; /// The content shown while open (a box widget). final Widget body; /// Small actions beside the title (a count, a copy button). final List trailing; final Color? titleColor; @override Widget build(BuildContext context) { final extent = math.max( 44.0, MediaQuery.textScalerOf(context).scale(16) + 28, ); return SliverMainAxisGroup( slivers: [ PinnedHeaderSliver( child: SizedBox( height: extent, child: StickySectionTitle( title: title, open: open, onToggle: onToggle, trailing: trailing, color: titleColor, ), ), ), if (open) SliverToBoxAdapter(child: body), ], ); } } /// A sticky section's title bar: name, actions and the open/close chevron. class StickySectionTitle extends StatelessWidget { const StickySectionTitle({ super.key, required this.title, required this.open, required this.onToggle, this.trailing = const [], this.color, }); final String title; final bool open; final VoidCallback onToggle; final List trailing; final Color? color; @override Widget build(BuildContext context) { final cupertino = usesCupertinoHost(context); final background = cupertino ? CupertinoColors.systemGroupedBackground.resolveFrom(context) : Theme.of(context).colorScheme.surface; final muted = cupertino ? CupertinoColors.secondaryLabel.resolveFrom(context) : Theme.of(context).colorScheme.onSurfaceVariant; final style = TextStyle( fontWeight: FontWeight.w600, color: color ?? (cupertino ? CupertinoColors.label.resolveFrom(context) : Theme.of(context).colorScheme.onSurface), ); return ColoredBox( color: background, child: Padding( padding: const EdgeInsetsDirectional.only(start: 16, end: 8), child: Row( children: [ Expanded( child: Semantics( container: true, header: true, button: true, expanded: open, label: title, excludeSemantics: true, onTap: onToggle, child: GestureDetector( behavior: HitTestBehavior.opaque, onTap: onToggle, child: Row( children: [ Expanded( child: Text( title, style: style, maxLines: 1, overflow: TextOverflow.ellipsis, ), ), Icon( open ? (cupertino ? CupertinoIcons.chevron_up : Icons.expand_less) : (cupertino ? CupertinoIcons.chevron_down : Icons.expand_more), color: muted, ), ], ), ), ), ), ...trailing, ], ), ), ); } } /// A pinned bar for controls that should stay reachable while a list /// scrolls (a view switch). class StickyBar extends StatelessWidget { const StickyBar({super.key, required this.child}); final Widget child; @override Widget build(BuildContext context) { final cupertino = usesCupertinoHost(context); return PinnedHeaderSliver( child: ColoredBox( color: cupertino ? CupertinoColors.systemGroupedBackground.resolveFrom(context) : Theme.of(context).colorScheme.surface, child: child, ), ); } }