import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'glass_pill.dart'; /// The smallest height of every piece of a [SegmentedBar]: the platform's /// minimum touch target, so a round Back is a circle. double segmentedPieceHeight(bool cupertino) => cupertino ? kMinInteractiveDimensionCupertino : kMinInteractiveDimension; /// One rounded piece of a segmented bar: a [GlassPill] at least the /// platform's touch-target size. class SegmentedPill extends StatelessWidget { const SegmentedPill({ super.key, required this.cupertino, required this.child, this.fill, }); final bool cupertino; final Widget child; /// Overrides the fill (a dark viewer's pieces). final Color? fill; @override Widget build(BuildContext context) { final height = segmentedPieceHeight(cupertino); // A stadium whose content is as wide as it is tall is a circle. return GlassPill( cupertino: cupertino, fill: fill, child: ConstrainedBox( // The minimum reaches the content, so a pressable piece is pressable // (and ripples) across its whole height. constraints: BoxConstraints(minHeight: height, minWidth: height), child: child, ), ); } } /// The platform's own Back for a segmented bar: Material [BackButton], /// Cupertino [CupertinoNavigationBarBackButton]. A null [onPressed] keeps the /// stock action (pop the route). class SegmentedBackButton extends StatelessWidget { const SegmentedBackButton({ super.key, required this.cupertino, this.onPressed, }); final bool cupertino; final VoidCallback? onPressed; @override Widget build(BuildContext context) => cupertino ? CupertinoNavigationBarBackButton(onPressed: onPressed) : BackButton(onPressed: onPressed); } /// What a bar implies on the leading edge when the page gave none, matching /// the stock bars: Back on a route that can go back, the platform's close /// control on a full-screen dialog, nothing on a root page. Widget? impliedSegmentedLeading(BuildContext context, bool cupertino) { final route = ModalRoute.of(context); if (route == null) return null; final dialog = route is PageRoute && route.fullscreenDialog; if (cupertino) { if (!route.canPop) return null; if (dialog) { return CupertinoButton( padding: const EdgeInsets.symmetric(horizontal: 16), onPressed: () => route.navigator?.maybePop(), child: Text(CupertinoLocalizations.of(context).cancelButtonLabel), ); } return const SegmentedBackButton(cupertino: true); } if (!route.impliesAppBarDismissal) return null; return dialog ? const CloseButton() : const BackButton(); } /// The title, with an optional one-line subtitle, in one pill; pressing it /// runs [onPressed] when given. class SegmentedTitlePill extends StatelessWidget { const SegmentedTitlePill({ super.key, required this.cupertino, required this.title, this.subtitle, this.subtitleIcon, this.onPressed, this.semanticLabel, this.buttonKey, this.foreground, this.fill, }); final bool cupertino; final String title; final String? subtitle; final IconData? subtitleIcon; final VoidCallback? onPressed; final String? semanticLabel; /// The key of the pressable title, for finders. final Key? buttonKey; final Color? foreground; final Color? fill; @override Widget build(BuildContext context) { final TextStyle titleStyle; final TextStyle subtitleStyle; if (cupertino) { final text = CupertinoTheme.of(context).textTheme; titleStyle = text.textStyle.copyWith( fontWeight: FontWeight.w600, color: foreground, ); subtitleStyle = text.tabLabelTextStyle.copyWith( color: foreground ?? CupertinoColors.secondaryLabel.resolveFrom(context), ); } else { final theme = Theme.of(context); titleStyle = (theme.textTheme.titleSmall ?? const TextStyle()).copyWith( color: foreground ?? theme.colorScheme.onSurface, ); subtitleStyle = (theme.textTheme.labelSmall ?? const TextStyle()) .copyWith(color: foreground ?? theme.colorScheme.onSurfaceVariant); } final subtitle = this.subtitle ?? ''; final content = Padding( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4), child: Column( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, children: [ Semantics( header: true, child: Text( title, maxLines: 1, overflow: TextOverflow.ellipsis, style: titleStyle, ), ), if (subtitle.isNotEmpty) // One line that ellipsises as a whole, so a narrow pill never // overflows on the icon. Text.rich( TextSpan( children: [ if (subtitleIcon != null) ...[ WidgetSpan( alignment: PlaceholderAlignment.middle, child: Icon( subtitleIcon, size: subtitleStyle.fontSize, color: subtitleStyle.color, ), ), const TextSpan(text: ' '), ], TextSpan(text: subtitle), ], ), maxLines: 1, overflow: TextOverflow.ellipsis, style: subtitleStyle, ), ], ), ); final onPressed = this.onPressed; final Widget body; if (onPressed == null) { body = KeyedSubtree(key: buttonKey, child: content); } else { body = Semantics( button: true, label: semanticLabel, child: cupertino ? CupertinoButton( key: buttonKey, padding: EdgeInsets.zero, minimumSize: Size.zero, onPressed: onPressed, child: DefaultTextStyle.merge( style: titleStyle, child: content, ), ) : InkWell(key: buttonKey, onTap: onPressed, child: content), ); } return SegmentedPill(cupertino: cupertino, fill: fill, child: body); } }