feat(app): segmented glass top bar across the app

Extract the chat's segmented bar into lib/ui/adaptive (SegmentedBar,
GlassPill, SegmentedBarOverlay) and use it for the shell bar, DetailPane
and every page-level bar. Pieces are frosted glass over a fading scrim;
only pills take taps. Actions keep their natural width so the title gives
way before a model name. The chat transcript scrolls behind its header.
This commit is contained in:
2026-10-31 00:21:27 +00:00
parent 4a85a12cf7
commit 0582bfc34f
42 changed files with 1391 additions and 571 deletions
+80
View File
@@ -0,0 +1,80 @@
import 'dart:ui' show ImageFilter;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
/// A frosted-glass piece floating over content: the content behind is
/// blurred inside the piece's [shape], under a translucent fill in the host's
/// raised surface role (Material `surfaceContainerHigh`, Cupertino
/// `systemGrey6`) and a hairline border.
///
/// Under the system's high-contrast setting the fill is solid and nothing is
/// blurred. The Cupertino piece uses no Material widget; the Material piece
/// is a transparent [Material] so ink responses inside it show.
///
/// Shared by the segmented top bar and any floating bar (a tab bar) that
/// wants the same pieces.
class GlassPill extends StatelessWidget {
const GlassPill({
super.key,
required this.cupertino,
required this.child,
this.shape = const StadiumBorder(),
this.fill,
});
final bool cupertino;
final Widget child;
final OutlinedBorder shape;
/// Overrides the fill (a dark viewer's pieces); used as given.
final Color? fill;
/// Blur radius of the content behind a piece.
static const double blurSigma = 16;
/// How much of the fill covers the blurred content.
static const double fillOpacity = 0.72;
@override
Widget build(BuildContext context) {
final solid = MediaQuery.maybeHighContrastOf(context) ?? false;
final Color base;
final Color edge;
if (cupertino) {
base = CupertinoColors.systemGrey6.resolveFrom(context);
edge = CupertinoColors.separator.resolveFrom(context);
} else {
final scheme = Theme.of(context).colorScheme;
base = scheme.surfaceContainerHigh;
edge = scheme.outlineVariant;
}
final color = fill ?? (solid ? base : base.withValues(alpha: fillOpacity));
final border = shape.copyWith(
side: BorderSide(
color: solid ? edge : edge.withValues(alpha: edge.a * 0.5),
width: 0.5,
),
);
Widget surface = DecoratedBox(
decoration: ShapeDecoration(color: color, shape: border),
child: cupertino
? child
: Material(type: MaterialType.transparency, child: child),
);
if (!solid) {
// One blur per piece, clipped to it.
surface = BackdropFilter(
filter: ImageFilter.blur(sigmaX: blurSigma, sigmaY: blurSigma),
child: surface,
);
}
return ClipPath(
clipper: ShapeBorderClipper(
shape: shape,
textDirection: Directionality.maybeOf(context),
),
child: surface,
);
}
}
+253
View File
@@ -0,0 +1,253 @@
import 'dart:math' as math;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'segmented_bar_pieces.dart';
export 'segmented_bar_pieces.dart' show SegmentedBackButton, SegmentedPill;
/// The app's top bar: separate frosted-glass pieces floating over the page's
/// content, on a scrim of the page's background that fades out downwards.
///
/// From the leading edge: a round piece holding [leading] (the platform's
/// Back, implied as the stock bars do when [automaticallyImplyLeading]), a
/// pill with the [title] and an optional one-line [subtitle] that runs
/// [onTitlePressed] when given, and a pill holding the [actions] at the
/// trailing edge. The actions take their natural width first and the title
/// shrinks; only when the title is down to [minTitleShare] of the bar does
/// the first action ellipsise.
///
/// Only the pieces take taps: a tap between them reaches the content behind.
/// It works as a [Scaffold.appBar] (with `extendBodyBehindAppBar`) and a
/// [CupertinoPageScaffold.navigationBar], which inset the content by the
/// bar's height through [MediaQuery.paddingOf], so a page keeps its
/// scaffold's structure; [SegmentedBarOverlay] does the same anywhere else.
/// It handles the top safe area itself.
class SegmentedBar extends StatelessWidget
implements ObstructingPreferredSizeWidget {
const SegmentedBar({
super.key,
required this.cupertino,
required this.title,
this.leading,
this.automaticallyImplyLeading = true,
this.subtitle,
this.subtitleIcon,
this.onTitlePressed,
this.titleSemanticLabel,
this.titleKey,
this.actions = const [],
this.backgroundColor,
this.pillColor,
this.foregroundColor,
this.bottomPadding = 8,
});
/// Which control family to draw. Required because the bar's reserved
/// height differs: a Cupertino navigation bar never scales its text, so it
/// reserves exactly its height, while a Material app bar grows with text.
final bool cupertino;
/// The title; empty shows no title pill unless there is a [subtitle].
final String title;
/// The leading control, normally a [SegmentedBackButton].
final Widget? leading;
/// Whether to show the platform's Back (or close, on a full-screen dialog)
/// when [leading] is null and the route can go back.
final bool automaticallyImplyLeading;
final String? subtitle;
final IconData? subtitleIcon;
/// Makes the title pill a button (an information page, a picker).
final VoidCallback? onTitlePressed;
final String? titleSemanticLabel;
/// The key of the title's button (or its content when not pressable).
final Key? titleKey;
final List<Widget> actions;
/// The bar's backdrop; defaults to the enclosing scaffold's background.
final Color? backgroundColor;
/// The pieces' fill; defaults to the host's raised surface role.
final Color? pillColor;
/// The title's color, for a bar over a dark backdrop.
final Color? foregroundColor;
/// Space between the pieces and the content below.
final double bottomPadding;
static const double _topPadding = 8;
/// The share of the bar the title keeps before the actions shrink.
static const double minTitleShare = 0.25;
/// Bar text grows with the reader's setting up to this factor, like the
/// stock app bar caps its title, so the bar never takes over the page.
static const double maxTextScale = 2;
@override
Size get preferredSize {
final piece = segmentedPieceHeight(cupertino);
if (cupertino) {
// CupertinoPageScaffold disables text scaling for its navigation bar
// and reserves exactly this height for it.
return Size.fromHeight(_topPadding + piece + bottomPadding);
}
// A Material scaffold only caps its app bar at this height and lets it
// size to its content: room for a title and subtitle at the largest
// text scale.
return Size.fromHeight(_topPadding + piece * maxTextScale + bottomPadding);
}
@override
// Content scrolls behind the bar; the scaffold insets it by the bar's
// height through the media padding instead.
bool shouldFullyObstruct(BuildContext context) => false;
Color _background(BuildContext context) {
if (backgroundColor case final color?) return color;
if (cupertino) {
return context
.dependOnInheritedWidgetOfExactType<
CupertinoPageScaffoldBackgroundColor
>()
?.color ??
CupertinoTheme.of(context).scaffoldBackgroundColor;
}
return Scaffold.maybeOf(context)?.widget.backgroundColor ??
Theme.of(context).scaffoldBackgroundColor;
}
@override
Widget build(BuildContext context) {
final leading =
this.leading ??
(automaticallyImplyLeading
? impliedSegmentedLeading(context, cupertino)
: null);
final showTitle = title.isNotEmpty || (subtitle ?? '').isNotEmpty;
final Widget bar = LayoutBuilder(
builder: (context, constraints) => Row(
children: [
if (leading != null) ...[
SegmentedPill(
cupertino: cupertino,
fill: pillColor,
child: leading,
),
const SizedBox(width: 8),
],
Expanded(
// Only as tall as the title: a Material scaffold offers its app
// bar more height than it needs.
child: Align(
alignment: AlignmentDirectional.centerStart,
heightFactor: 1,
child: showTitle
? SegmentedTitlePill(
cupertino: cupertino,
title: title,
subtitle: subtitle,
subtitleIcon: subtitleIcon,
onPressed: onTitlePressed,
semanticLabel: titleSemanticLabel,
buttonKey: titleKey,
foreground: foregroundColor,
fill: pillColor,
)
: const SizedBox.shrink(),
),
),
if (actions.isNotEmpty) ...[
const SizedBox(width: 8),
// The actions keep to the trailing edge at their natural width,
// so a model name shows whole while the title has room; only past
// the title's minimum share does the first action ellipsise.
ConstrainedBox(
constraints: BoxConstraints(
maxWidth: math.max(
0,
constraints.maxWidth -
(leading == null
? 0
: segmentedPieceHeight(cupertino) + 8) -
8 -
constraints.maxWidth * minTitleShare,
),
),
child: SegmentedPill(
cupertino: cupertino,
fill: pillColor,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(child: actions.first),
...actions.skip(1),
],
),
),
),
],
],
),
);
final background = _background(context);
final inset = MediaQuery.paddingOf(context).top;
return Stack(
children: [
// The scrim: the page's background, opaque at the edge and fading out
// towards the content, so content scrolled behind the pieces still
// reads. It takes no taps; only the pieces do.
Positioned.fill(
child: IgnorePointer(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SizedBox(
height: inset,
child: ColoredBox(color: background),
),
Expanded(
child: DecoratedBox(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
background,
background.withValues(alpha: background.a * 0.85),
background.withValues(alpha: 0),
],
stops: const [0, 0.45, 1],
),
),
),
),
],
),
),
),
SafeArea(
bottom: false,
child: Padding(
padding: EdgeInsetsDirectional.fromSTEB(
12,
_topPadding,
12,
bottomPadding,
),
child: MediaQuery.withClampedTextScaling(
maxScaleFactor: maxTextScale,
child: bar,
),
),
),
],
);
}
}
@@ -0,0 +1,92 @@
import 'package:flutter/widgets.dart';
/// Floats [bar] over the top of [child], the way a scaffold with
/// `extendBodyBehindAppBar` does, for a bar that is not a scaffold's.
///
/// [child] fills the whole area and scrolls behind the bar; it learns the
/// bar's measured height (status-bar inset included) as its
/// [MediaQuery.paddingOf] top, so a list can pad its first item below the bar
/// at rest. Taps the bar does not take reach [child].
class SegmentedBarOverlay extends StatelessWidget {
const SegmentedBarOverlay({
super.key,
required this.bar,
required this.child,
});
final Widget bar;
final Widget child;
@override
Widget build(BuildContext context) => CustomMultiChildLayout(
delegate: _OverlayLayout(),
children: [
// Painted first, so the bar draws (and takes taps) above it.
LayoutId(
id: _Slot.content,
child: LayoutBuilder(
builder: (context, constraints) {
final barHeight = constraints is _ContentConstraints
? constraints.barHeight
: 0.0;
final media = MediaQuery.of(context);
return MediaQuery(
data: media.copyWith(
padding: media.padding.copyWith(top: barHeight),
),
child: child,
);
},
),
),
LayoutId(id: _Slot.bar, child: bar),
],
);
}
enum _Slot { content, bar }
/// The content's tight constraints, carrying the bar's height so the content
/// rebuilds its inset when the bar grows or shrinks.
class _ContentConstraints extends BoxConstraints {
_ContentConstraints(Size size, {required this.barHeight})
: super(
minWidth: size.width,
maxWidth: size.width,
minHeight: size.height,
maxHeight: size.height,
);
final double barHeight;
@override
bool operator ==(Object other) =>
other is _ContentConstraints &&
super == other &&
other.barHeight == barHeight;
@override
int get hashCode => Object.hash(super.hashCode, barHeight);
}
class _OverlayLayout extends MultiChildLayoutDelegate {
@override
void performLayout(Size size) {
final bar = layoutChild(
_Slot.bar,
BoxConstraints(
maxWidth: size.width,
minWidth: size.width,
).copyWith(maxHeight: size.height),
);
positionChild(_Slot.bar, Offset.zero);
layoutChild(
_Slot.content,
_ContentConstraints(size, barHeight: bar.height),
);
positionChild(_Slot.content, Offset.zero);
}
@override
bool shouldRelayout(_OverlayLayout oldDelegate) => false;
}
@@ -0,0 +1,203 @@
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);
}
}