Files
pipelines/app/lib/ui/adaptive/segmented_bar.dart
T
alan 0582bfc34f 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.
2026-10-31 00:21:27 +00:00

254 lines
8.8 KiB
Dart

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,
),
),
),
],
);
}
}