import 'dart:math' as math; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../adaptive/host.dart'; import '../adaptive/window_margins.dart'; import '../theme/app_theme.dart'; import '../motion/motion.dart'; import 'section_heading.dart'; import 'sliver_reveal.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, this.body, this.sliverBody, this.trailing = const [], this.titleColor, }) : assert((body == null) != (sliverBody == null)); final String title; final bool open; final VoidCallback onToggle; /// The content shown while open (a box widget). final Widget? body; /// The content shown while open as a sliver, in place of [body]: a long /// body (hundreds of items) then builds and lays out only what is visible. final Widget? sliverBody; /// 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: [ SliverPersistentHeader( pinned: true, delegate: _StickySectionHeaderDelegate( extent: extent, title: title, open: open, onToggle: onToggle, trailing: trailing, color: titleColor, ), ), // The content grows and shrinks under the pinned title, on its own // surface with the title's accent continuing down its edge. if (sliverBody case final sliver?) SliverReveal( open: open, duration: Motion.of(context, Motion.standard), curve: Motion.curve, // The body's padding; its rows bring their own ink surface. sliver: SliverPadding( padding: _SectionBody.padding, sliver: sliver, ), ) else SliverToBoxAdapter( child: ClipRect( child: AnimatedSize( duration: Motion.of(context, Motion.standard), curve: Motion.curve, alignment: Alignment.topCenter, child: open ? _SectionBody(child: body!) : const SizedBox(width: double.infinity), ), ), ), ], ); } } class _StickySectionHeaderDelegate extends SliverPersistentHeaderDelegate { _StickySectionHeaderDelegate({ required this.extent, required this.title, required this.open, required this.onToggle, required this.trailing, required this.color, }); final double extent; final String title; final bool open; final VoidCallback onToggle; final List trailing; final Color? color; @override double get minExtent => extent; @override double get maxExtent => extent; @override Widget build( BuildContext context, double shrinkOffset, bool overlapsContent, ) { final heading = SectionHeadingStyle.of(context); final inset = MediaQuery.paddingOf(context).top; return Stack( clipBehavior: Clip.none, fit: StackFit.expand, children: [ if (overlapsContent) Positioned( top: -inset, left: 0, right: 0, bottom: 0, // The top-bar clearance is pinned ahead of this header. Once // this header is pinned, cover that clearance too so scrolled // content cannot remain visible above the section title. child: ColoredBox(color: heading.background), ), StickySectionTitle( title: title, open: open, onToggle: onToggle, trailing: trailing, color: color, pinned: overlapsContent, ), ], ); } @override bool shouldRebuild(_StickySectionHeaderDelegate oldDelegate) => extent != oldDelegate.extent || title != oldDelegate.title || open != oldDelegate.open || color != oldDelegate.color || !identical(onToggle, oldDelegate.onToggle) || !identical(trailing, oldDelegate.trailing); } /// 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, this.pinned = false, }); final String title; final bool open; final VoidCallback onToggle; final List trailing; final Color? color; final bool pinned; @override Widget build(BuildContext context) { final cupertino = usesCupertinoHost(context); // The shared section-title look is deliberately quiet, like a grouped-list // label rather than a separate, filled row. final heading = SectionHeadingStyle.of(context); final muted = cupertino ? CupertinoColors.secondaryLabel.resolveFrom(context) : Theme.of(context).colorScheme.onSurfaceVariant; final style = color == null ? heading.title : heading.title.copyWith(color: color); final chevron = 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, ), ); // On a wide window the chevron keeps the title's 16 px from the edge, in // line with the top bar's trailing piece; a phone keeps it closer. final row = Padding( padding: EdgeInsetsDirectional.only( start: 16, end: WindowMargins.wideOf(context) ? 16 : 8, ), child: Row( children: [ Expanded( // The title is the section's heading and its toggle for // assistive tech; the strip's own tap target is not announced. child: Semantics( container: true, header: true, button: true, expanded: open, label: title, excludeSemantics: true, onTap: onToggle, child: Text( title, style: style, maxLines: 1, overflow: TextOverflow.ellipsis, ), ), ), ...trailing, ExcludeSemantics(child: chevron), ], ), ); // The whole strip opens and closes the section; its own buttons (copy) // still take their taps first. // The page-colour scrim keeps this pinned heading readable as content moves // underneath without returning to the old solid section band. final scrim = pinned ? heading.background : heading.background.withValues(alpha: 0.94); final Widget strip = cupertino ? GestureDetector( behavior: HitTestBehavior.opaque, excludeFromSemantics: true, onTap: onToggle, child: ColoredBox(color: scrim, child: row), ) : Material( color: scrim, child: InkWell( onTap: onToggle, excludeFromSemantics: true, child: row, ), ); return Stack( clipBehavior: Clip.none, fit: StackFit.expand, children: [ // A few pixels of the page scrim above it prevent content peeking // through at fractional pixel ratios. Positioned( left: 0, right: 0, top: -3, height: 4, child: ColoredBox(color: heading.background), ), MouseRegion(cursor: SystemMouseCursors.click, child: strip), ], ); } } /// A section's open content, with room between the heading and first line. class _SectionBody extends StatelessWidget { const _SectionBody({required this.child}); final Widget child; static const padding = EdgeInsets.only(top: 12, bottom: 4); @override Widget build(BuildContext context) { final content = Padding(padding: padding, child: child); if (usesCupertinoHost(context)) { return content; } // Rows inside retain a Material ancestor for their ink reactions. return Material(type: MaterialType.transparency, child: content); } } /// 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 ? AppColors.groupedBackground.resolveFrom(context) : Theme.of(context).colorScheme.surface, child: child, ), ); } }