Files
pipelines/app/lib/ui/adaptive/segmented_bar_pieces.dart
alan 9551eceaed Complete Material-only APIs and fixtures
Remove obsolete control-family flags and icon metadata across all callers, dead alternate themes and host helpers, obsolete test setup, and the unused icon-font dependency. Preserve native Material Back transitions. Make navigation fixtures independent of update plugins and answer auxiliary shell snapshots so the complete application suite can run deterministically.
2026-11-11 21:55:39 +00:00

264 lines
8.2 KiB
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() => 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.child, this.fill});
final Widget child;
/// Overrides the fill (a dark viewer's pieces).
final Color? fill;
@override
Widget build(BuildContext context) {
final height = segmentedPieceHeight();
// A stadium whose content is as wide as it is tall is a circle.
return GlassPill(
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 app's Material Back button for a segmented bar. A null [onPressed] keeps the
/// stock action (pop the route).
class SegmentedBackButton extends StatelessWidget {
const SegmentedBackButton({super.key, this.onPressed});
final VoidCallback? onPressed;
@override
Widget build(BuildContext context) => 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) {
final route = ModalRoute.of(context);
if (route == null) return null;
final dialog = route is PageRoute && route.fullscreenDialog;
if (!route.impliesAppBarDismissal) return null;
return dialog ? const CloseButton() : const BackButton();
}
/// The title pill's title and subtitle styles, from the host's type scale.
(TextStyle, TextStyle) segmentedTitleStyles(
BuildContext context, {
Color? foreground,
}) {
final theme = Theme.of(context);
return (
(theme.textTheme.titleSmall ?? const TextStyle()).copyWith(
color: foreground ?? theme.colorScheme.onSurface,
fontWeight: FontWeight.w600,
),
(theme.textTheme.labelSmall ?? const TextStyle()).copyWith(
color: foreground ?? theme.colorScheme.onSurfaceVariant,
),
);
}
/// The title pill's horizontal padding on each side.
const double segmentedTitlePadding = 16;
/// The width the title pill needs to show [title] and [subtitle] whole (at
/// the text scale at [context]).
double segmentedTitleWidth(
BuildContext context, {
required String title,
String? subtitle,
IconData? subtitleIcon,
Color? foreground,
}) {
final (titleStyle, subtitleStyle) = segmentedTitleStyles(
context,
foreground: foreground,
);
final scaler = MediaQuery.textScalerOf(context);
final direction = Directionality.of(context);
double measure(String text, TextStyle style) {
if (text.isEmpty) return 0;
final painter = TextPainter(
text: TextSpan(text: text, style: style),
textDirection: direction,
textScaler: scaler,
maxLines: 1,
)..layout();
final width = painter.width;
painter.dispose();
return width;
}
final sub = subtitle ?? '';
final subWidth = sub.isEmpty
? 0.0
: measure(sub, subtitleStyle) +
(subtitleIcon == null
? 0
: scaler.scale(subtitleStyle.fontSize ?? 12) +
measure(' ', subtitleStyle));
final text = measure(title, titleStyle);
return (text > subWidth ? text : subWidth).ceilToDouble() +
segmentedTitlePadding * 2;
}
/// 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.title,
this.subtitle,
this.subtitleIcon,
this.onPressed,
this.semanticLabel,
this.buttonKey,
this.expanded = false,
this.titleAction,
this.leading,
this.foreground,
this.fill,
});
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 Widget? leading;
final Color? foreground;
final Color? fill;
@override
Widget build(BuildContext context) {
final (titleStyle, subtitleStyle) = segmentedTitleStyles(
context,
foreground: foreground,
);
final subtitle = this.subtitle ?? '';
final content = Padding(
padding: const EdgeInsets.symmetric(
horizontal: segmentedTitlePadding,
vertical: 4,
),
child: Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
if (leading != null)
Flexible(
child: FittedBox(
fit: BoxFit.scaleDown,
child: Padding(
padding: const EdgeInsetsDirectional.only(end: 8),
child: leading!,
),
),
),
Flexible(
flex: leading == null ? 1 : 2,
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: 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(
fill: fill,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
fit: FlexFit.loose,
child: IntrinsicWidth(
child: split && onPressed != null
? SegmentedPress(tint: foreground, child: body)
: body,
),
),
if (titleAction != null)
SegmentedPress(tint: foreground, child: titleAction),
],
),
);
if (split || onPressed == null) return pill;
return SegmentedPress(tint: foreground, child: pill);
}
}