Files
pipelines/app/lib/ui/adaptive/segmented_bar_pieces.dart
T
alan a40a6e2e1c Narrow list panes: readable bar title, More overflow menu, rows keep status and actions
The floating bar keeps the title at its own width (up to half the bar) and
moves overflowable actions into one More menu instead of squeezing them
below their touch targets. List rows keep their leading status avatar and
More actions at any list-pane width; the time moves under the subtitle.
2026-11-03 18:04:10 +00:00

316 lines
9.7 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 pill's title and subtitle styles, from the host's type scale.
(TextStyle, TextStyle) segmentedTitleStyles(
BuildContext context, {
required bool cupertino,
Color? foreground,
}) {
if (cupertino) {
final text = CupertinoTheme.of(context).textTheme;
return (
text.textStyle.copyWith(fontWeight: FontWeight.w600, color: foreground),
text.textStyle.copyWith(
fontSize: 13,
color:
foreground ?? CupertinoColors.secondaryLabel.resolveFrom(context),
),
);
}
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 bool cupertino,
required String title,
String? subtitle,
IconData? subtitleIcon,
Color? foreground,
}) {
final (titleStyle, subtitleStyle) = segmentedTitleStyles(
context,
cupertino: cupertino,
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.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 (titleStyle, subtitleStyle) = segmentedTitleStyles(
context,
cupertino: cupertino,
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: [
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);
}
}