import 'dart:math' as math; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../adaptive/host.dart'; import '../theme/app_theme.dart'; import '../motion/motion.dart'; import 'section_heading.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, ), ), ), // The content grows and shrinks under the pinned title, on its own // surface with the title's accent continuing down its edge. 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), ), ), ), ], ); } } /// 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); // The shared section-title look: it reads as a divider, not a 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, ), ); final row = Padding( padding: const EdgeInsetsDirectional.only(start: 13, end: 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), ], ), ); final decoration = BoxDecoration( color: heading.background, border: Border( left: BorderSide(color: heading.accent, width: 3), bottom: BorderSide(color: heading.line, width: 0.5), ), ); // The whole strip opens and closes the section; its own buttons (copy) // still take their taps first. final Widget strip = cupertino ? GestureDetector( behavior: HitTestBehavior.opaque, excludeFromSemantics: true, onTap: onToggle, child: DecoratedBox(decoration: decoration, child: row), ) : Material( color: heading.background, child: Ink( decoration: decoration, child: InkWell( onTap: onToggle, excludeFromSemantics: true, child: row, ), ), ); return Stack( clipBehavior: Clip.none, fit: StackFit.expand, children: [ // A few pixels of the title's colour above it: at fractional pixel // ratios content scrolling under a pinned title could peek through. 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: its own surface, a faint continuation of the /// title's accent, and room between the title and the first line. class _SectionBody extends StatelessWidget { const _SectionBody({required this.child}); final Widget child; @override Widget build(BuildContext context) { final heading = SectionHeadingStyle.of(context); final decoration = BoxDecoration( color: heading.body, border: Border( left: BorderSide( color: heading.accent.withValues(alpha: 0.35), width: 3, ), bottom: BorderSide(color: heading.line, width: 0.5), ), ); final content = Padding( padding: const EdgeInsets.only(top: 12, bottom: 4), child: child, ); if (usesCupertinoHost(context)) { return DecoratedBox(decoration: decoration, child: content); } // Rows inside paint their ripples on this surface. return Material( color: heading.body, child: Ink(decoration: decoration, 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, ), ); } }