Files
pipelines/app/lib/ui/adaptive/segmented_bar_pieces.dart
T
alan 009bf3d853 Pop the top bar's islands in and give its controls press feedback
Each island (Back, title, actions pill, each action) scales in from 0.85
with a gentle overshoot, staggered from the leading edge on first show and
at once when it newly appears; rebuilds and title changes keep it. Glass
fades its fill and blur together so backdrop grouping is unchanged.
Every tappable piece sinks to 0.94 under a stadium tint while pressed and
springs back. Reduced motion skips the animation.
2026-11-02 16:47:01 +00:00

255 lines
8.1 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'glass_pill.dart';
import 'segmented_bar_motion.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.expanded = false,
this.titleAction,
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 bool expanded;
final Widget? titleAction;
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.textStyle.copyWith(
fontSize: 13,
color:
foreground ?? CupertinoColors.secondaryLabel.resolveFrom(context),
);
} else {
final theme = Theme.of(context);
titleStyle = (theme.textTheme.titleSmall ?? const TextStyle()).copyWith(
color: foreground ?? theme.colorScheme.onSurface,
fontWeight: FontWeight.w600,
);
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: Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Flexible(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Semantics(
header: true,
child: Text(
title,
maxLines: expanded ? null : 1,
overflow: expanded
? TextOverflow.visible
: 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: expanded ? null : 1,
overflow: expanded
? TextOverflow.visible
: 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),
);
}
final titleAction = this.titleAction;
// A pressable title sinks as a whole pill; beside a title action, the
// title and the action each give their own feedback.
final split = titleAction != null;
final pill = SegmentedPill(
cupertino: cupertino,
fill: fill,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
fit: FlexFit.loose,
child: IntrinsicWidth(
child: split && onPressed != null
? SegmentedPress(
cupertino: cupertino,
tint: foreground,
child: body,
)
: body,
),
),
if (titleAction != null)
SegmentedPress(
cupertino: cupertino,
tint: foreground,
child: titleAction,
),
],
),
);
if (split || onPressed == null) return pill;
return SegmentedPress(cupertino: cupertino, tint: foreground, child: pill);
}
}