Float glass tab bars everywhere the app has bottom tabs
One floating frosted tab bar (host NavigationBar / CupertinoTabBar in the shared GlassPill) replaces the NavigationRail, the Cupertino sidebar and the run detail, stage page and pipeline detail tab strips. Below 600px it spans the width minus 16px margins; wider it is sized to its tabs at the right edge. Content keeps the full width, scrolls behind the bar and learns its height as bottom media padding; lists pad their end clear of it and FABs sit above it. The bar now sizes itself from its own constraints instead of the screen size, which fixes the widget_test failure where a MediaQuery without a size gave it negative width constraints.
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
/// At least this wide, a floating tab bar is sized to its tabs and sits at the
|
||||
/// trailing edge; narrower, it spans the width minus [kFloatingBarMargin].
|
||||
const double kFloatingBarWideWidth = 600;
|
||||
|
||||
/// The gap between a floating tab bar and the edges (and bottom safe area).
|
||||
const double kFloatingBarMargin = 16;
|
||||
|
||||
/// [padding] with the floating bar's clearance added at the bottom.
|
||||
///
|
||||
/// A scrollable under a floating bar (see [FloatingBarOverlay] and the shell)
|
||||
/// reads the bar's height as [MediaQuery.paddingOf] bottom; adding it to the
|
||||
/// list's own padding lets the content scroll behind the bar yet still bring
|
||||
/// its last item clear of it at rest. Lists without explicit padding already
|
||||
/// do this themselves.
|
||||
EdgeInsetsGeometry withFloatingBarClearance(
|
||||
BuildContext context,
|
||||
EdgeInsetsGeometry padding,
|
||||
) => padding.add(EdgeInsets.only(bottom: MediaQuery.paddingOf(context).bottom));
|
||||
|
||||
/// Empty space as tall as the floating bar's clearance
|
||||
/// ([MediaQuery.paddingOf] bottom), ending a scrolling column so its last item
|
||||
/// comes clear of the bar at rest.
|
||||
class FloatingBarClearance extends StatelessWidget {
|
||||
const FloatingBarClearance({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) =>
|
||||
SizedBox(height: MediaQuery.paddingOf(context).bottom);
|
||||
}
|
||||
|
||||
/// [FloatingBarClearance] as the last sliver of a [CustomScrollView].
|
||||
class SliverFloatingBarClearance extends StatelessWidget {
|
||||
const SliverFloatingBarClearance({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) =>
|
||||
const SliverToBoxAdapter(child: FloatingBarClearance());
|
||||
}
|
||||
|
||||
/// Floats [bar] over the bottom of [child], the way a scaffold with
|
||||
/// `extendBody` floats its bottom bar, 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 (bottom safe area and margins included) as its
|
||||
/// [MediaQuery.paddingOf] bottom, so a list can pad its last item clear of the
|
||||
/// bar at rest (see [withFloatingBarClearance]). Taps the bar does not take,
|
||||
/// such as beside a bar narrower than the area, reach [child].
|
||||
class FloatingBarOverlay extends StatelessWidget {
|
||||
const FloatingBarOverlay({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(
|
||||
bottom: barHeight > media.padding.bottom
|
||||
? barHeight
|
||||
: media.padding.bottom,
|
||||
),
|
||||
),
|
||||
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(
|
||||
minWidth: size.width,
|
||||
maxWidth: size.width,
|
||||
maxHeight: size.height,
|
||||
),
|
||||
);
|
||||
positionChild(_Slot.bar, Offset(0, size.height - bar.height));
|
||||
layoutChild(
|
||||
_Slot.content,
|
||||
_ContentConstraints(size, barHeight: bar.height),
|
||||
);
|
||||
positionChild(_Slot.content, Offset.zero);
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRelayout(_OverlayLayout oldDelegate) => false;
|
||||
}
|
||||
@@ -1,12 +1,26 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'floating_bar_layout.dart';
|
||||
import 'glass_pill.dart';
|
||||
|
||||
/// A compact destination switcher which floats above the bottom edge instead
|
||||
/// of becoming a full-width system bar. The controls inside remain the
|
||||
/// platform navigation controls, so their labels, semantics and selection
|
||||
/// behaviour are not recreated by the glass treatment.
|
||||
/// A tab bar which floats above the bottom edge in a frosted [GlassPill]
|
||||
/// instead of becoming a full-width system bar. Used for the app's
|
||||
/// destinations and for every page's own tabs.
|
||||
///
|
||||
/// Placement follows the width the bar is given, live: below
|
||||
/// [kFloatingBarWideWidth] the pill spans it minus [kFloatingBarMargin] on
|
||||
/// each side; at or above it the pill is only as wide as its tabs and sits at
|
||||
/// the trailing (right, in left-to-right text) edge, near the thumb. Either
|
||||
/// way it keeps [kFloatingBarMargin] above the bottom safe area. Only the pill
|
||||
/// takes taps: the space beside it reaches the content behind.
|
||||
///
|
||||
/// The controls inside remain the platform navigation controls (Material
|
||||
/// [NavigationBar], Cupertino [CupertinoTabBar]), so tab semantics (selected
|
||||
/// state, "tab n of m") come from the host. The Cupertino path builds no
|
||||
/// Material widget.
|
||||
class FloatingDestinationBar extends StatelessWidget {
|
||||
const FloatingDestinationBar({
|
||||
super.key,
|
||||
@@ -21,23 +35,90 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
final ValueChanged<int> onTap;
|
||||
final List<FloatingDestinationItem> items;
|
||||
|
||||
/// Height of the Material bar inside the pill (the stock bar is 80).
|
||||
static const double materialHeight = 64;
|
||||
|
||||
/// Height of the Cupertino bar inside the pill (the stock bar is 50).
|
||||
static const double cupertinoHeight = 54;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final width = MediaQuery.sizeOf(context).width;
|
||||
final bar = cupertino ? _cupertinoBar() : _materialBar(context);
|
||||
return SafeArea(
|
||||
top: false,
|
||||
minimum: const EdgeInsets.fromLTRB(16, 0, 16, 8),
|
||||
child: Align(
|
||||
alignment: Alignment.bottomCenter,
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(maxWidth: width - 32),
|
||||
child: GlassPill(cupertino: cupertino, child: bar),
|
||||
),
|
||||
minimum: const EdgeInsets.fromLTRB(
|
||||
kFloatingBarMargin,
|
||||
0,
|
||||
kFloatingBarMargin,
|
||||
kFloatingBarMargin,
|
||||
),
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
// Wide is judged on the space the bar is given, margins included,
|
||||
// the way the shell and the pages judge their own layouts.
|
||||
final available = constraints.maxWidth;
|
||||
final wide =
|
||||
available + 2 * kFloatingBarMargin >= kFloatingBarWideWidth;
|
||||
final width = wide
|
||||
? math.min(available, _tabsWidth(context))
|
||||
: available;
|
||||
return Align(
|
||||
// As tall as the pill, so a scaffold or overlay measures the bar
|
||||
// and not the whole area it may grow into.
|
||||
heightFactor: 1,
|
||||
alignment: wide
|
||||
? AlignmentDirectional.bottomEnd
|
||||
: Alignment.bottomCenter,
|
||||
child: SizedBox(
|
||||
width: math.max(0, width),
|
||||
child: GlassPill(
|
||||
cupertino: cupertino,
|
||||
child: cupertino
|
||||
? _cupertinoBar(context)
|
||||
: _materialBar(context),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// The width the tabs need side by side: every tab as wide as the widest
|
||||
/// label (as the host lays them out), never narrower than a comfortable
|
||||
/// touch target.
|
||||
double _tabsWidth(BuildContext context) {
|
||||
final TextStyle? style;
|
||||
final TextScaler scaler;
|
||||
final double minimum;
|
||||
if (cupertino) {
|
||||
style = CupertinoTheme.of(context).textTheme.tabLabelTextStyle;
|
||||
scaler = TextScaler.noScaling;
|
||||
minimum = 76;
|
||||
} else {
|
||||
final theme = Theme.of(context);
|
||||
style =
|
||||
theme.navigationBarTheme.labelTextStyle?.resolve(const {
|
||||
WidgetState.selected,
|
||||
}) ??
|
||||
theme.textTheme.labelMedium;
|
||||
// NavigationBar clamps its labels at 1.3x.
|
||||
scaler = MediaQuery.textScalerOf(context).clamp(maxScaleFactor: 1.3);
|
||||
minimum = 88;
|
||||
}
|
||||
var widest = 0.0;
|
||||
for (final item in items) {
|
||||
final painter = TextPainter(
|
||||
text: TextSpan(text: item.label, style: style),
|
||||
textScaler: scaler,
|
||||
textDirection: Directionality.of(context),
|
||||
maxLines: 1,
|
||||
)..layout();
|
||||
widest = math.max(widest, painter.width);
|
||||
painter.dispose();
|
||||
}
|
||||
return math.max(minimum, widest + 24) * items.length;
|
||||
}
|
||||
|
||||
Widget _materialBar(BuildContext context) {
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
return NavigationBarTheme(
|
||||
@@ -46,6 +127,8 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
elevation: 0,
|
||||
surfaceTintColor: Colors.transparent,
|
||||
indicatorColor: scheme.secondaryContainer,
|
||||
height: materialHeight,
|
||||
labelBehavior: NavigationDestinationLabelBehavior.alwaysShow,
|
||||
),
|
||||
child: NavigationBar(
|
||||
selectedIndex: currentIndex,
|
||||
@@ -53,6 +136,7 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
destinations: [
|
||||
for (final item in items)
|
||||
NavigationDestination(
|
||||
key: item.key,
|
||||
icon: item.icon,
|
||||
selectedIcon: item.selectedIcon ?? item.icon,
|
||||
label: item.label,
|
||||
@@ -62,19 +146,58 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _cupertinoBar() => CupertinoTabBar(
|
||||
backgroundColor: CupertinoColors.transparent,
|
||||
currentIndex: currentIndex,
|
||||
onTap: onTap,
|
||||
items: [
|
||||
for (final item in items)
|
||||
BottomNavigationBarItem(
|
||||
icon: item.icon,
|
||||
activeIcon: item.selectedIcon ?? item.icon,
|
||||
label: item.label,
|
||||
/// The stock Cupertino bar, transparent inside the pill. Each tab's label is
|
||||
/// drawn with its icon (in the bar's own label style and colours) so the
|
||||
/// item's key covers the whole tab, and the selected tab's icon sits in a
|
||||
/// tinted capsule so the selection reads at a glance, not by colour alone.
|
||||
Widget _cupertinoBar(BuildContext context) {
|
||||
final active = CupertinoTheme.of(context).primaryColor;
|
||||
Widget tab(FloatingDestinationItem item, {required bool selected}) {
|
||||
Widget icon = item.icon;
|
||||
if (selected) {
|
||||
icon = DecoratedBox(
|
||||
decoration: ShapeDecoration(
|
||||
color: active.withValues(alpha: 0.16),
|
||||
shape: const StadiumBorder(),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 2),
|
||||
child: item.selectedIcon ?? item.icon,
|
||||
),
|
||||
);
|
||||
}
|
||||
return KeyedSubtree(
|
||||
key: item.key,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
icon,
|
||||
const SizedBox(height: 2),
|
||||
Text(item.label, maxLines: 1, softWrap: false),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
// The tab bar does not scale its text on iOS either; its height is fixed.
|
||||
return MediaQuery.withNoTextScaling(
|
||||
child: CupertinoTabBar(
|
||||
backgroundColor: CupertinoColors.transparent,
|
||||
border: null,
|
||||
iconSize: 24,
|
||||
height: cupertinoHeight,
|
||||
currentIndex: currentIndex,
|
||||
onTap: onTap,
|
||||
items: [
|
||||
for (var index = 0; index < items.length; index += 1)
|
||||
BottomNavigationBarItem(
|
||||
icon: tab(items[index], selected: false),
|
||||
activeIcon: tab(items[index], selected: true),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The platform-neutral data needed by [FloatingDestinationBar].
|
||||
@@ -83,9 +206,13 @@ class FloatingDestinationItem {
|
||||
required this.icon,
|
||||
required this.label,
|
||||
this.selectedIcon,
|
||||
this.key,
|
||||
});
|
||||
|
||||
final Widget icon;
|
||||
final String label;
|
||||
final Widget? selectedIcon;
|
||||
|
||||
/// Identifies the whole tab (icon and label), for tests and automation.
|
||||
final Key? key;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user