Merge: tabs as separate round glass buttons

This commit is contained in:
2026-11-02 18:44:27 +00:00
24 changed files with 916 additions and 1003 deletions
+106 -300
View File
@@ -1,37 +1,32 @@
import 'dart:math' as math;
import 'dart:ui' show SemanticsRole;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'floating_bar_layout.dart';
import 'floating_tab_button.dart';
import 'floating_tab_motion.dart';
import 'glass_pill.dart';
/// 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.
/// A tab bar which floats above the bottom edge as a row of separate round
/// frosted [GlassPill] buttons, one per tab, instead of a full-width system
/// bar. Used for the app's destinations and for every page's own tabs.
///
/// At any width the pill is only as wide as its icon tabs and sits at the
/// trailing (right, in left-to-right text) edge, near the thumb, keeping
/// [kFloatingBarMargin] from the sides and above the bottom safe area. Only
/// the pill takes taps: the space beside it reaches the content behind.
/// The row is only as wide as its buttons and sits at the trailing (right,
/// in left-to-right text) edge, near the thumb, keeping [kFloatingBarMargin]
/// from the sides and above the bottom safe area. Only the buttons take
/// taps: the gaps between and beside them reach the content behind. A row
/// squeezed below its natural width narrows its gaps, then scales down.
///
/// One tinted circle sits behind the selected tab's icon and glides to a
/// newly selected tab. At rest it keeps the same gap from its neighbours' slots
/// as from the pill's top and bottom, and at the outer tabs the same gap from
/// the pill's sides, so the pill's round ends are concentric with it.
/// The selected button is filled with the host's selection tint, which
/// cross-fades when the selection moves, and its icon bounces briefly. Each
/// button pops in on its own, staggered, when the bar appears or its set of
/// tabs changes, never when only the selection does.
///
/// The tabs pop in when the bar appears or its set of tabs changes, never
/// when only the selection does.
///
/// The tabs show icons only. Each tab's label stays its accessible name (and
/// the Material tooltip), so screen readers still announce it; the icons must
/// be distinct within a bar.
///
/// 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.
/// The buttons show icons only. Each tab's label stays its accessible name
/// (and the Material tooltip), announced as a selectable tab with "tab n of
/// m" the way the platform navigation controls announce theirs (see
/// [FloatingTabButton]); the icons must be distinct within a bar. The
/// Cupertino path builds no Material widget.
class FloatingDestinationBar extends StatefulWidget {
const FloatingDestinationBar({
super.key,
@@ -52,64 +47,34 @@ class FloatingDestinationBar extends StatefulWidget {
final ValueChanged<int> onTap;
final List<FloatingDestinationItem> items;
/// The icon sizes used by the two host navigation controls.
/// The icon size inside each button, as in the host navigation controls.
static const double materialIconSize = 28;
static const double cupertinoIconSize = 30;
/// Padding between each icon and the edge of its selection circle.
static const double _materialRing = 8;
static const double _cupertinoRing = 6;
/// The diameter of each tab button, which is also the bar's height.
static const double materialButtonSize = 52;
static const double cupertinoButtonSize = 50;
/// The gap between a circle and the pill's nearest edge or neighbour.
/// Keeping these as the source constants makes the bar grow with the icons.
static const double _materialEdgeGap = 8;
static const double _cupertinoEdgeGap = 6;
/// Diameter of the selection circle.
static const double materialHighlightSize =
materialIconSize + 2 * _materialRing;
static const double cupertinoHighlightSize =
cupertinoIconSize + 2 * _cupertinoRing;
/// Height of the host bar, derived from its circle and the equal edge gaps.
static const double materialHeight =
materialHighlightSize + 2 * _materialEdgeGap;
static const double cupertinoHeight =
cupertinoHighlightSize + 2 * _cupertinoEdgeGap;
/// The gap between a tab's circle and the pill's top and bottom; the outer
/// circles keep the same gap from the pill's sides, and neighbours from
/// each other.
static const double materialEdgeGap = _materialEdgeGap;
static const double cupertinoEdgeGap = _cupertinoEdgeGap;
/// The additional breathing room between neighbouring selection circles.
static const double materialNeighbourGap = 15;
static const double cupertinoNeighbourGap = 13;
/// Width of one equal native tab slot at the requested neighbour spacing.
static const double materialTabWidth =
materialHighlightSize + materialNeighbourGap;
static const double cupertinoTabWidth =
cupertinoHighlightSize + cupertinoNeighbourGap;
/// The gap between neighbouring buttons.
static const double materialButtonGap = 12;
static const double cupertinoButtonGap = 10;
/// The side margin at [FloatingBarEdge.top], matching the top bar's own.
static const double topMargin = 12;
/// The selection highlight inside each bar, for tests.
static const Key highlightKey = ValueKey(
'floating-destination-bar.highlight',
);
/// The row of buttons inside each bar, for tests and measurement.
static const Key rowKey = ValueKey('floating-destination-bar.row');
/// The width of a pill holding [tabs] tabs, when the space allows it.
static double pillWidth({required bool cupertino, required int tabs}) =>
cupertino
? cupertinoHighlightSize * tabs +
2 * cupertinoEdgeGap +
math.max(0, tabs - 1) * cupertinoNeighbourGap
: materialHighlightSize * tabs +
2 * materialEdgeGap +
math.max(0, tabs - 1) * materialNeighbourGap;
static double buttonSize({required bool cupertino}) =>
cupertino ? cupertinoButtonSize : materialButtonSize;
static double buttonGap({required bool cupertino}) =>
cupertino ? cupertinoButtonGap : materialButtonGap;
/// The width of a row of [tabs] buttons, when the space allows it.
static double rowWidth({required bool cupertino, required int tabs}) =>
buttonSize(cupertino: cupertino) * tabs +
buttonGap(cupertino: cupertino) * math.max(0, tabs - 1);
@override
State<FloatingDestinationBar> createState() => _FloatingDestinationBarState();
@@ -118,6 +83,7 @@ class FloatingDestinationBar extends StatefulWidget {
class _FloatingDestinationBarState extends State<FloatingDestinationBar>
with TickerProviderStateMixin {
late final FloatingTabMotion _motion = FloatingTabMotion(this);
final _backdrop = BackdropKey();
bool _entered = false;
bool get cupertino => widget.cupertino;
@@ -136,8 +102,7 @@ class _FloatingDestinationBarState extends State<FloatingDestinationBar>
void didUpdateWidget(FloatingDestinationBar oldWidget) {
super.didUpdateWidget(oldWidget);
if (_tabSetIdentity(oldWidget.items) != _tabSetIdentity(items)) {
// New tabs (a run's page tabs appearing): they pop in, and the
// highlight starts on the selected one.
// New tabs (a run's page tabs appearing): they pop in.
_motion.enter(context, count: items.length, selected: currentIndex);
} else if (oldWidget.currentIndex != currentIndex) {
_motion.select(context, currentIndex);
@@ -153,44 +118,21 @@ class _FloatingDestinationBarState extends State<FloatingDestinationBar>
static String _tabSetIdentity(List<FloatingDestinationItem> items) =>
items.map((item) => '${item.key ?? item.label}:${item.label}').join('|');
double get _size => FloatingDestinationBar.buttonSize(cupertino: cupertino);
@override
Widget build(BuildContext context) {
final top = widget.edge == FloatingBarEdge.top;
final bar = LayoutBuilder(
builder: (context, constraints) {
final width = math.max(0.0, math.min(constraints.maxWidth, _tabsWidth));
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: top
? AlignmentDirectional.topStart
: AlignmentDirectional.bottomEnd,
child: SizedBox(
width: width,
// The insets are handled around the pill. The host bars pad
// themselves by any media padding left over (a page's top bar,
// another floating bar below a pane), which would swell the pill.
child: MediaQuery.removePadding(
context: context,
removeTop: true,
removeBottom: true,
removeLeft: true,
removeRight: true,
child: GlassPill(
cupertino: cupertino,
child: _edgeAligned(
context,
width,
cupertino
? _cupertinoBar(context, _slot(width))
: _materialBar(context),
),
),
),
),
);
},
builder: (context, constraints) => Align(
// As tall as the row, so a scaffold or overlay measures the bar and
// not the whole area it may grow into.
heightFactor: 1,
alignment: top
? AlignmentDirectional.topStart
: AlignmentDirectional.bottomEnd,
child: _row(context, constraints.maxWidth),
),
);
if (top) {
// Under a top bar: no safe area to keep clear of, only the gap to the
@@ -217,207 +159,71 @@ class _FloatingDestinationBarState extends State<FloatingDestinationBar>
);
}
double get _highlight => cupertino
? FloatingDestinationBar.cupertinoHighlightSize
: FloatingDestinationBar.materialHighlightSize;
double get _gap => cupertino
? FloatingDestinationBar.cupertinoEdgeGap
: FloatingDestinationBar.materialEdgeGap;
double get _neighbourGap => cupertino
? FloatingDestinationBar.cupertinoNeighbourGap
: FloatingDestinationBar.materialNeighbourGap;
/// The width the icon-only tabs need side by side. Icons do not scale with
/// text, so the pill keeps its size at any text scale.
double get _tabsWidth => FloatingDestinationBar.pillWidth(
cupertino: cupertino,
tabs: items.length,
);
/// The gap between neighbouring circles in a pill [width] wide: the named
/// one, or less in a pill squeezed below its tabs' width.
double _neighbour(double width) {
/// The gap between neighbouring buttons in [width]: the named one, or less
/// in a row squeezed below its natural width.
double _gap(double width) {
final gap = FloatingDestinationBar.buttonGap(cupertino: cupertino);
final count = items.length;
if (count < 2) return _neighbourGap;
final available = math.max(0.0, width - 2 * _gap - count * _highlight);
return math.min(_neighbourGap, available / (count - 1));
if (count < 2 || !width.isFinite) return gap;
return (width - count * _size).clamp(0.0, gap * (count - 1)) / (count - 1);
}
/// The width of each equal tab slot, which is also the distance between
/// neighbouring circles' centres.
double _slot(double width) => _highlight + math.max(0, _neighbour(width));
bool get _hasSelection => currentIndex >= 0 && currentIndex < items.length;
/// Insets the host bar's equal tab slots so the outer circles sit the edge
/// gap from the pill's sides. A pill squeezed below its tabs' width keeps
/// that gap and narrows the gaps between neighbours instead, as long as
/// the circles still fit side by side. The Material highlight is laid over
/// the first slot, behind the host bar.
Widget _edgeAligned(BuildContext context, double width, Widget bar) {
final count = items.length;
if (count == 0) return bar;
// The native bars use equal-width slots. Let their slot centers be the
// desired circle centers, and translate the slots by half the difference
// between the outer and neighbour gaps. The pill clips the harmless
// overflow of the native slot backgrounds, while the circles remain
// exactly edge-aligned.
final slot = _slot(width);
final offset = _gap - _neighbour(width) / 2;
return ClipRect(
child: SizedBox(
width: width,
height: cupertino
? FloatingDestinationBar.cupertinoHeight
: FloatingDestinationBar.materialHeight,
child: OverflowBox(
alignment: AlignmentDirectional.topStart,
minWidth: 0,
maxWidth: double.infinity,
child: Transform.translate(
offset: Offset(offset, 0),
child: SizedBox(
width: slot * count,
child: Stack(
fit: StackFit.expand,
children: [
if (!cupertino && _hasSelection)
PositionedDirectional(
start: 0,
top: 0,
bottom: 0,
width: slot,
child: _highlightFor(context, slot),
),
bar,
],
),
),
),
),
),
);
}
/// The bar's one selection highlight, laid out over tab 0.
Widget _highlightFor(BuildContext context, double slot) =>
FloatingTabHighlight(
key: FloatingDestinationBar.highlightKey,
motion: _motion,
slot: slot,
size: _highlight,
color: cupertino
? CupertinoTheme.of(context).primaryColor.withValues(alpha: 0.16)
: Theme.of(context).colorScheme.secondaryContainer,
);
Widget _materialBar(BuildContext context) {
return NavigationBarTheme(
data: const NavigationBarThemeData(
backgroundColor: Colors.transparent,
elevation: 0,
surfaceTintColor: Colors.transparent,
// The bar draws one gliding highlight behind the icons (the stock
// indicator is a fixed 64x32 that jumps); the stock one stays for
// its circular ink response.
indicatorColor: Colors.transparent,
indicatorShape: CircleBorder(),
height: FloatingDestinationBar.materialHeight,
iconTheme: WidgetStatePropertyAll(
IconThemeData(size: FloatingDestinationBar.materialIconSize),
),
// Icons only; the label remains each tab's accessible name and its
// tooltip.
labelBehavior: NavigationDestinationLabelBehavior.alwaysHide,
),
child: NavigationBar(
selectedIndex: currentIndex,
onDestinationSelected: widget.onTap,
destinations: [
for (var index = 0; index < items.length; index += 1)
NavigationDestination(
key: items[index].key,
icon: _icon(items[index].icon, index),
selectedIcon: _icon(
items[index].selectedIcon ?? items[index].icon,
index,
),
label: items[index].label,
),
Widget _row(BuildContext context, double width) {
final gap = _gap(width);
Widget row = Row(
key: FloatingDestinationBar.rowKey,
mainAxisSize: MainAxisSize.min,
children: [
for (var index = 0; index < items.length; index += 1) ...[
if (index > 0) SizedBox(width: gap),
_button(context, index),
],
),
],
);
}
/// The stock Cupertino bar, transparent inside the pill. Each tab shows its
/// icon only, named by its label for accessibility; the item's key covers
/// the whole tab, and the highlight behind the selected icon makes the
/// selection read at a glance, not by colour alone.
///
/// The host blurs whatever is behind its translucent background, so the
/// highlight is painted from inside tab 0, under the icons but above the
/// blur.
Widget _cupertinoBar(BuildContext context, double slot) {
Widget tab(
FloatingDestinationItem item, {
required bool selected,
required int index,
}) {
final icon = _icon(
selected ? item.selectedIcon ?? item.icon : item.icon,
index,
);
return KeyedSubtree(
key: item.key,
child: Semantics(
label: item.label,
// The host centres the icon above a 4px bottom inset; 4 above it
// centres the icon and its highlight in the whole bar, so the
// highlight is as far from the pill's top as from its bottom.
child: Padding(
padding: const EdgeInsets.only(top: 4),
child: index == 0 && _hasSelection
? Stack(
clipBehavior: Clip.none,
alignment: Alignment.center,
children: [
Positioned.fill(child: _highlightFor(context, slot)),
icon,
],
)
: icon,
),
),
// Narrower than the buttons themselves: the whole row shrinks.
if (width.isFinite && width < items.length * _size) {
row = SizedBox(
width: math.max(0.0, width),
height: _size,
child: FittedBox(fit: BoxFit.scaleDown, child: row),
);
}
// One tab bar, its buttons the tabs. The buttons share one read of the
// content behind them for their blurs where the engine supports it.
row = Semantics(
role: SemanticsRole.tabBar,
container: true,
explicitChildNodes: true,
child: BackdropGroup(backdropKey: _backdrop, child: row),
);
// The bar does not scale with text (its icons do not either); a badge
// count keeps its size.
return cupertino ? MediaQuery.withNoTextScaling(child: row) : row;
}
// 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,
// Secondary-label translucency over glass falls below text contrast
// requirements. The host label role remains readable in both themes.
inactiveColor: CupertinoColors.label.resolveFrom(context),
iconSize: FloatingDestinationBar.cupertinoIconSize,
height: FloatingDestinationBar.cupertinoHeight,
currentIndex: currentIndex,
onTap: widget.onTap,
items: [
for (var index = 0; index < items.length; index += 1)
BottomNavigationBarItem(
icon: tab(items[index], selected: false, index: index),
activeIcon: tab(items[index], selected: true, index: index),
),
],
Widget _button(BuildContext context, int index) {
final item = items[index];
final selected = index == currentIndex;
return FloatingTabEntrance(
motion: _motion,
index: index,
child: FloatingTabButton(
key: item.key,
cupertino: cupertino,
motion: _motion,
index: index,
count: items.length,
selected: selected,
size: _size,
iconSize: cupertino
? FloatingDestinationBar.cupertinoIconSize
: FloatingDestinationBar.materialIconSize,
label: item.label,
icon: selected ? item.selectedIcon ?? item.icon : item.icon,
onTap: () => widget.onTap(index),
),
);
}
/// Tab [index]'s icon, animated by the bar: the host swaps a tab's icon
/// subtree on selection, so the icon itself holds no animation state.
Widget _icon(Widget child, int index) =>
FloatingTabEntrance(motion: _motion, index: index, child: child);
}
/// The platform-neutral data needed by [FloatingDestinationBar].
@@ -0,0 +1,189 @@
import 'dart:ui' show SemanticsRole;
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import '../motion/motion.dart';
import 'floating_tab_motion.dart';
import 'glass_pill.dart';
import 'segmented_bar_motion.dart';
/// One tab of a floating tab bar: a round [GlassPill] of [size] with the
/// tab's icon in the middle.
///
/// The selected tab fills its circle with the host's selection tint
/// (Material `secondaryContainer`, Cupertino active colour at
/// [cupertinoTintOpacity]) and draws its icon in the selected colour; the
/// tint and colour cross-fade when the selection moves. A press sinks the
/// button ([SegmentedPress]), the Material one with its circular ink.
///
/// It is announced like the host navigation controls' tabs: a tab (and a
/// button) named by [label], with its selected state and "tab n of m"
/// (in the Material label, or the Cupertino hint). Material shows [label]
/// as a tooltip. The icon is painted above the glass, unclipped, so a badge
/// on its corner can reach past the circle.
class FloatingTabButton extends StatelessWidget {
const FloatingTabButton({
super.key,
required this.cupertino,
required this.motion,
required this.index,
required this.count,
required this.selected,
required this.size,
required this.iconSize,
required this.label,
required this.icon,
required this.onTap,
});
final bool cupertino;
final FloatingTabMotion motion;
final int index;
final int count;
final bool selected;
final double size;
final double iconSize;
final String label;
/// The icon for the current state (the item's selected icon when selected).
final Widget icon;
final VoidCallback onTap;
/// The Cupertino selection fill: the active colour at this opacity.
static const double cupertinoTintOpacity = 0.18;
/// The selection fill inside each button (an [Ink] on Material, a
/// [DecoratedBox] on Cupertino), for tests.
static const Key fillKey = ValueKey('floating-tab-button.fill');
@override
Widget build(BuildContext context) {
final colors = _colors(context);
final duration = Motion.of(context, Motion.tabSelectDuration);
final button = TweenAnimationBuilder<double>(
tween: Tween(end: selected ? 1 : 0),
duration: duration,
curve: Motion.curve,
builder: (context, t, _) => _button(context, colors, t),
);
return MergeSemantics(
child: Semantics(
role: SemanticsRole.tab,
selected: selected,
button: true,
label: cupertino || kIsWeb ? label : '$label\n${_tabLabel(context)}',
hint: cupertino ? _tabLabel(context) : null,
// Switching tabs does not unfocus a text field (the composer).
child: TextFieldTapRegion(
child: cupertino
? button
: Tooltip(
message: label,
excludeFromSemantics: true,
child: button,
),
),
),
);
}
String _tabLabel(BuildContext context) => cupertino
? CupertinoLocalizations.of(
context,
).tabSemanticsLabel(tabIndex: index + 1, tabCount: count)
: MaterialLocalizations.of(
context,
).tabLabel(tabIndex: index + 1, tabCount: count);
({Color tint, Color selectedIcon, Color icon}) _colors(BuildContext context) {
if (cupertino) {
final active = CupertinoTheme.of(context).primaryColor;
return (
tint: active.withValues(alpha: cupertinoTintOpacity),
selectedIcon: active,
// Secondary-label translucency over glass falls below contrast.
icon: CupertinoColors.label.resolveFrom(context),
);
}
final scheme = Theme.of(context).colorScheme;
return (
tint: scheme.secondaryContainer,
selectedIcon: scheme.onSecondaryContainer,
icon: scheme.onSurfaceVariant,
);
}
/// The button at selection progress [t] (0 unselected, 1 selected).
Widget _button(
BuildContext context,
({Color tint, Color selectedIcon, Color icon}) colors,
double t,
) {
final decoration = ShapeDecoration(
color: colors.tint.withValues(alpha: colors.tint.a * t),
shape: const CircleBorder(),
);
final Widget fill = cupertino
? DecoratedBox(
key: fillKey,
decoration: decoration,
child: MouseRegion(
cursor: kIsWeb ? SystemMouseCursors.click : MouseCursor.defer,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: onTap,
),
),
)
// Painted on the glass's Material, under the ink, so the ripple
// shows over the selected tint too.
: Ink(
key: fillKey,
decoration: decoration,
child: InkResponse(
onTap: onTap,
customBorder: const CircleBorder(),
containedInkWell: true,
highlightShape: BoxShape.circle,
),
);
Widget glyph = IconTheme.merge(
data: IconThemeData(
size: iconSize,
color: Color.lerp(colors.icon, colors.selectedIcon, t),
),
child: FloatingTabBounce(motion: motion, index: index, child: icon),
);
final reveal = GlassReveal.maybeOf(context);
if (reveal != null) {
// The icon sits above the glass, so it fades with it here.
glyph = FadeTransition(
opacity: reveal,
alwaysIncludeSemantics: true,
child: glyph,
);
}
return SegmentedPress(
cupertino: cupertino,
child: SizedBox.square(
dimension: size,
child: Stack(
clipBehavior: Clip.none,
alignment: Alignment.center,
children: [
Positioned.fill(
child: GlassPill(
cupertino: cupertino,
shape: const CircleBorder(),
child: fill,
),
),
IgnorePointer(child: glyph),
],
),
),
);
}
}
+48 -119
View File
@@ -3,22 +3,17 @@ import 'dart:math' as math;
import 'package:flutter/widgets.dart';
import '../motion/motion.dart';
import 'glass_pill.dart';
/// The animations of one floating tab bar: its tabs popping in, the
/// selection highlight gliding between tabs and the newly selected icon's
/// bounce.
/// The animations of one floating tab bar: its tab buttons popping in one
/// after another, and the newly selected icon's bounce.
///
/// They live with the bar, not with each tab's icon, because the host
/// navigation controls rebuild a tab's icon subtree whenever it is selected
/// or deselected: state kept in the icons would restart with every tap.
/// They live with the bar, not with each button, so a button rebuilt with a
/// new selection (or a new badge) never restarts its entrance: only the bar
/// decides when its tabs enter.
class FloatingTabMotion {
FloatingTabMotion(TickerProvider vsync)
: entrance = AnimationController(vsync: vsync),
slide = AnimationController(
vsync: vsync,
duration: Motion.tabHighlightDuration,
value: 1,
),
bounce = AnimationController(
vsync: vsync,
duration: Motion.tabBounceDuration,
@@ -27,17 +22,15 @@ class FloatingTabMotion {
/// Drives every tab's entrance; tab n plays its pop in its own window.
final AnimationController entrance;
final AnimationController slide;
final AnimationController bounce;
int _count = 0;
final List<({Animation<double> pop, Animation<double> opacity})> _entrances =
[];
double _from = 0;
int _to = 0;
int _selected = 0;
/// Plays the entrance of a bar of [count] tabs, from the start, and places
/// the highlight on [selected] without a move.
/// Plays the entrance of a bar of [count] tabs, from the start, with
/// [selected] chosen and nothing bouncing.
void enter(
BuildContext context, {
required int count,
@@ -53,41 +46,25 @@ class FloatingTabMotion {
entrance.forward(from: 0);
}
_splitEntrance();
_from = selected.toDouble();
_to = selected;
slide.value = 1;
_selected = selected;
bounce.value = 1;
}
/// Glides the highlight from where it is now to [selected] and bounces
/// that tab's icon. Under reduced motion the highlight jumps.
/// Bounces the newly [selected] tab's icon; nothing moves under reduced
/// motion.
void select(BuildContext context, int selected) {
if (selected == _to) return;
_from = position;
_to = selected;
if (Motion.of(context, Motion.tabHighlightDuration) == Duration.zero) {
slide.value = 1;
if (selected == _selected) return;
_selected = selected;
if (Motion.of(context, Motion.tabBounceDuration) == Duration.zero) {
bounce.value = 1;
return;
}
slide.forward(from: 0);
bounce.forward(from: 0);
}
double get _travel => Motion.curve.transform(slide.value);
/// The highlight's position in tab slots: 1.5 is halfway from tab 1 to 2.
double get position => _from + (_to - _from) * _travel;
/// The highlight's width over its height: 1 at rest, at most
/// [Motion.tabHighlightStretch] halfway through a move.
double get stretch => _from == _to
? 1
: 1 + (Motion.tabHighlightStretch - 1) * math.sin(math.pi * _travel);
/// The scale of tab [index]'s icon: only the newly selected one bounces.
double scaleOf(int index) {
if (index != _to || bounce.isCompleted) return 1;
if (index != _selected || bounce.isCompleted) return 1;
final t = bounce.value;
final rise = t < 0.5 ? t * 2 : (1 - t) * 2;
return 1 + (Motion.tabBounceScale - 1) * Curves.easeOut.transform(rise);
@@ -138,114 +115,66 @@ class FloatingTabMotion {
void dispose() {
_disposeEntrances();
entrance.dispose();
slide.dispose();
bounce.dispose();
}
}
/// Tab [index]'s icon (or the highlight behind it) popping in with the bar,
/// and bouncing when it becomes selected.
/// Tab [index]'s whole button popping in with the bar: it grows from
/// [Motion.tabEntranceScaleBegin] with a gentle overshoot, rising a little,
/// while its glass fades in through a [GlassReveal] (so the blur fades with
/// the fill instead of snapping on at the end).
class FloatingTabEntrance extends StatelessWidget {
const FloatingTabEntrance({
super.key,
required this.motion,
required this.index,
required this.child,
this.bounces = true,
});
final FloatingTabMotion motion;
final int index;
final bool bounces;
final Widget child;
@override
Widget build(BuildContext context) {
final entrance = motion.entranceOf(index);
return FadeTransition(
opacity: entrance.opacity,
// Tabs (and their badge counts) stay announced while they fade in.
alwaysIncludeSemantics: true,
child: AnimatedBuilder(
animation: Listenable.merge([entrance.pop, motion.bounce]),
child: child,
builder: (context, child) {
final pop = entrance.pop.value;
final scale =
Motion.tabEntranceScaleBegin +
(1 - Motion.tabEntranceScaleBegin) * pop;
return Transform.translate(
offset: Offset(0, (1 - pop) * Motion.tabEntranceSlideDistance),
child: Transform.scale(
scale: scale * (bounces ? motion.scaleOf(index) : 1),
child: child,
),
);
},
),
return AnimatedBuilder(
animation: entrance.pop,
// The reveal keeps the tab (and its badge count) announced while it
// fades in.
child: GlassReveal(opacity: entrance.opacity, child: child),
builder: (context, child) {
final pop = entrance.pop.value;
final scale =
Motion.tabEntranceScaleBegin +
(1 - Motion.tabEntranceScaleBegin) * pop;
return Transform.translate(
offset: Offset(0, (1 - pop) * Motion.tabEntranceSlideDistance),
child: Transform.scale(scale: scale, child: child),
);
},
);
}
}
/// The one selection highlight of a bar, behind its icons: a circle of
/// [size] centred on the selected tab, gliding (and stretching along the way)
/// to a newly selected one. It takes no taps and says nothing; the host's
/// tabs carry the selected state.
///
/// It is laid out over tab 0 (its slot, or its icon) and paints itself
/// [slot] widths along the reading direction per tab, unclipped.
class FloatingTabHighlight extends StatelessWidget {
const FloatingTabHighlight({
/// Tab [index]'s icon, bouncing briefly when it becomes selected.
class FloatingTabBounce extends StatelessWidget {
const FloatingTabBounce({
super.key,
required this.motion,
required this.slot,
required this.size,
required this.color,
required this.index,
required this.child,
});
final FloatingTabMotion motion;
/// The distance between neighbouring tabs' centres.
final double slot;
final double size;
final Color color;
final int index;
final Widget child;
@override
Widget build(BuildContext context) {
final direction = Directionality.of(context) == TextDirection.rtl ? -1 : 1;
return IgnorePointer(
child: ExcludeSemantics(
child: AnimatedBuilder(
animation: motion.slide,
builder: (context, _) {
final width = size * motion.stretch;
return OverflowBox(
minWidth: 0,
minHeight: 0,
maxWidth: double.infinity,
maxHeight: double.infinity,
child: Transform.translate(
offset: Offset(direction * slot * motion.position, 0),
child: FloatingTabEntrance(
motion: motion,
index: motion.position.round(),
bounces: false,
child: SizedBox(
width: width,
height: size,
child: DecoratedBox(
decoration: ShapeDecoration(
color: color,
shape: const StadiumBorder(),
),
),
),
),
),
);
},
),
),
);
}
Widget build(BuildContext context) => AnimatedBuilder(
animation: motion.bounce,
child: child,
builder: (context, child) =>
Transform.scale(scale: motion.scaleOf(index), child: child),
);
}
+2 -6
View File
@@ -34,12 +34,8 @@ abstract final class Motion {
/// The fraction of a tab's entrance spent fading in.
static const double tabEntranceOpacityPortion = 0.6;
/// How long a tab bar's selection highlight takes to glide to a new tab.
static const Duration tabHighlightDuration = Duration(milliseconds: 250);
/// The highlight's width, relative to its height, halfway through a move;
/// it is a circle again at rest.
static const double tabHighlightStretch = 1.25;
/// How long a tab button's selection tint takes to cross-fade.
static const Duration tabSelectDuration = Duration(milliseconds: 220);
/// The newly selected tab icon's brief bounce.
static const Duration tabBounceDuration = Duration(milliseconds: 180);
+39
View File
@@ -0,0 +1,39 @@
# Floating tab bars: one round glass button per tab
`FloatingDestinationBar` (the shell's destinations and every page's tabs) was
one glass pill holding the host `NavigationBar` / `CupertinoTabBar`, with a
single highlight circle gliding between tabs. Each tab is now its own round
`GlassPill` button (`FloatingTabButton`), in a row at the same place
(bottom right, `kFloatingBarMargin`, safe area; page tabs `kStackedBarGap`
above the shell row).
- Size: 52 px Material, 50 px Cupertino (the old circle was 44/42); icons
28/30 as before; gaps 12/10. The bar's height is the button size.
`FloatingDestinationBar.rowWidth`, `buttonSize`, `buttonGap` and `rowKey`
replace `pillWidth`, the highlight/edge-gap constants and `highlightKey`.
A row narrower than its natural width shrinks its gaps, then scales down.
- Selection: the selected button fills with `secondaryContainer` (Material) or
the active colour at 18% (Cupertino) and draws its icon in
`onSecondaryContainer` / the active colour; the fill and icon colour
cross-fade (`Motion.tabSelectDuration`), and the new icon bounces as before.
The gliding highlight was dropped: between separate buttons it would cross
the gaps over the content.
- Entrance: each whole button pops in (0.6 -> 1, ease-out-back, 70 ms
stagger), its glass fading through `GlassReveal`; only when the bar first
builds or its tab set changes.
- Press: `SegmentedPress`, as in the top bar; Material keeps its circular ink.
- Semantics: the row is a `SemanticsRole.tabBar`; each button a merged
`SemanticsRole.tab` button with its selected state, named by its label, and
"Tab n of m" in the label (Material, off the web, as `NavigationBar`) or the
hint (Cupertino, as `CupertinoTabBar`). Material shows the label as a
tooltip. `item.key` is on each button.
- Badges: the icon is painted above the glass, outside its circular clip, so
a badge on the icon's corner is not cut off.
- Blur: the buttons of a bar share one `BackdropGroup`, so on Impeller they
read the content behind them once (see `perf-glass-chrome.md`; CanvasKit
ignores backdrop keys and blurs each button).
Evidence: `/tmp/tab-buttons/` (start-*, flow-* at 560x900 and 880x1000,
Material and Cupertino; light-* shots). The kit's `film` step after `open`
recorded a cropped frame (560x347, 756x469); re-applying the viewport size
before filming (as the films here did) gives full-size frames.
@@ -127,7 +127,7 @@ void main() {
);
expect(navigator.canPop(), isTrue);
expect(findNativeBack(), findsOneWidget);
expect(find.byType(NavigationBar), findsNothing);
expect(find.byType(FloatingDestinationBar), findsNothing);
await tester.pageBack();
await tester.pumpAndSettle();
@@ -4,7 +4,6 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/pipelines/widgets/pipeline_tabs.dart';
import 'package:pipelines_app/features/pipelines/widgets/pipeline_detail_styles.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
void main() {
testWidgets('code-like values still use native host typography', (
@@ -61,17 +60,14 @@ void main() {
),
);
await tester.pumpAndSettle();
// The host's own tab bar inside the shared glass pill.
expect(
find.byType(cupertino ? CupertinoTabBar : NavigationBar),
findsOneWidget,
);
final pill = tester.getRect(find.byType(GlassPill));
// One floating bar of separate round tab buttons.
expect(find.byType(FloatingDestinationBar), findsOneWidget);
final pill = tester.getRect(find.byKey(FloatingDestinationBar.rowKey));
// Sized to its three tabs at the right edge, on a phone as when wide.
expect(pill.right, width - 16);
expect(
pill.width,
FloatingDestinationBar.pillWidth(
FloatingDestinationBar.rowWidth(
cupertino: cupertino,
tabs: PipelineDetailTab.values.length,
),
@@ -9,6 +9,7 @@ import 'package:pipelines_app/features/pipelines/pipeline_repository.dart';
import 'package:pipelines_app/features/pipelines/pipelines_controller.dart';
import 'package:pipelines_app/features/pipelines/screen.dart';
import 'package:pipelines_app/features/pipelines/widgets/pipeline_detail_panel.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
import 'fixtures.dart';
import 'test_helpers.dart';
@@ -139,7 +140,7 @@ void main() {
expect(
find.descendant(
of: find.byKey(const Key('pipeline-detail-tabs')),
matching: find.byType(CupertinoTabBar),
matching: find.byType(FloatingDestinationBar),
),
findsOneWidget,
);
@@ -6,7 +6,6 @@ import 'package:pipelines_app/features/graph/widgets/run_graph_view.dart';
import 'package:pipelines_app/features/runs/ui/run_detail_tab.dart';
import 'package:pipelines_app/features/runs/ui/run_detail_tab_strip.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
import 'run_detail_graph_support.dart';
@@ -63,7 +62,10 @@ void main() {
// The tabs are the floating glass bar: on this 900px detail it is sized
// to its tabs at the right edge, above the bottom.
final pill = tester.getRect(
find.descendant(of: strip, matching: find.byType(GlassPill)),
find.descendant(
of: strip,
matching: find.byKey(FloatingDestinationBar.rowKey),
),
);
expect(pill.right, 900 - 16);
expect(pill.bottom, 1000 - 16);
@@ -89,13 +91,16 @@ void main() {
await pumpRunDetail(tester, platform, size: const Size(390, 844));
final strip = find.byKey(const Key('run-detail-tabs'));
final pill = tester.getRect(
find.descendant(of: strip, matching: find.byType(GlassPill)),
find.descendant(
of: strip,
matching: find.byKey(FloatingDestinationBar.rowKey),
),
);
final body = find.byKey(const Key('run-detail-tab-body-summary'));
// Sized to its tabs at the right, floating above the bottom.
expect(
pill.width,
FloatingDestinationBar.pillWidth(
FloatingDestinationBar.rowWidth(
cupertino: platform != RunDetailTestPlatform.material,
tabs: renderedRunDetailTabs.length,
),
@@ -125,7 +130,10 @@ void main() {
await pumpRunDetail(tester, platform, size: const Size(1000, 844));
final widePill = tester.getRect(
find.descendant(of: strip, matching: find.byType(GlassPill)),
find.descendant(
of: strip,
matching: find.byKey(FloatingDestinationBar.rowKey),
),
);
expect(widePill.right, 1000 - 16);
expect(widePill.bottom, 844 - 16);
@@ -6,7 +6,7 @@ import 'package:pipelines_app/features/graph/widgets/stage_definition_view.dart'
import 'package:pipelines_app/features/runs/ui/run_detail_visit_facts.dart';
import 'package:pipelines_app/features/runs/ui/run_detail_visit_sections.dart';
import 'package:pipelines_app/features/runs/ui/run_detail_visit_session_state.dart';
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
import 'run_detail_graph_support.dart';
import 'run_detail_visit_support.dart';
@@ -241,7 +241,7 @@ void main() {
final pill = tester.getRect(
find.descendant(
of: find.byType(RunVisitSectionStrip),
matching: find.byType(GlassPill),
matching: find.byKey(FloatingDestinationBar.rowKey),
),
);
expect(pill.right, size.width - 16);
+3 -2
View File
@@ -2,6 +2,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/shell/adaptive_shell.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
/// The Runs tab mirrors the attention count in every mount of both hosts:
/// the floating tab bar at full width on a phone and right-aligned on a wide
@@ -35,7 +36,7 @@ void main() {
) async {
final semantics = tester.ensureSemantics();
await pumpShell(tester, width: width, cupertino: false, count: 3);
expect(find.byType(NavigationBar), findsOneWidget);
expect(find.byType(FloatingDestinationBar), findsOneWidget);
expect(find.byType(NavigationRail), findsNothing);
final badge = find.byType(Badge);
expect(badge, findsOneWidget);
@@ -74,7 +75,7 @@ void main() {
) async {
final semantics = tester.ensureSemantics();
await pumpShell(tester, width: width, cupertino: true, count: 3);
expect(find.byType(CupertinoTabBar), findsOneWidget);
expect(find.byType(FloatingDestinationBar), findsOneWidget);
expect(find.byType(Badge), findsNothing, reason: 'no Material badge');
expect(find.text('3'), findsOneWidget);
expect(find.bySemanticsLabel(needingAttention), findsOneWidget);
+5 -6
View File
@@ -4,14 +4,13 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/shell/adaptive_shell.dart';
import 'package:pipelines_app/shell/destination.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
/// The shell's one floating glass tab bar: full width on a phone, sized to its
/// tabs at the right edge on a wide window, with content behind it.
void main() {
Finder pill() => find.descendant(
of: find.byType(FloatingDestinationBar),
matching: find.byType(GlassPill),
matching: find.byKey(FloatingDestinationBar.rowKey),
);
Future<void> setSize(WidgetTester tester, Size size) async {
@@ -56,7 +55,7 @@ void main() {
final rect = tester.getRect(pill());
expect(
rect.width,
FloatingDestinationBar.pillWidth(cupertino: cupertino, tabs: 4),
FloatingDestinationBar.rowWidth(cupertino: cupertino, tabs: 4),
);
expect(rect.right, 390 - 16);
expect(rect.bottom, 844 - 16);
@@ -81,7 +80,7 @@ void main() {
// Four icon-only tabs, with the wider neighbour gaps derived by the bar.
expect(
rect.width,
FloatingDestinationBar.pillWidth(cupertino: cupertino, tabs: 4),
FloatingDestinationBar.rowWidth(cupertino: cupertino, tabs: 4),
);
// No rail or sidebar: the content starts at the left edge.
expect(find.byType(NavigationRail), findsNothing);
@@ -135,7 +134,7 @@ void main() {
await tester.pump();
expect(
tester.getRect(pill()).width,
FloatingDestinationBar.pillWidth(cupertino: cupertino, tabs: 4),
FloatingDestinationBar.rowWidth(cupertino: cupertino, tabs: 4),
);
expect(tester.getRect(pill()).right, 390 - 16);
expect(tester.state(find.byType(StatefulBuilder)), same(state));
@@ -199,7 +198,7 @@ void main() {
// Sized to its four tabs at the right, as on any width.
expect(
tester.getRect(pill()).width,
FloatingDestinationBar.pillWidth(cupertino: cupertino, tabs: 4),
FloatingDestinationBar.rowWidth(cupertino: cupertino, tabs: 4),
);
expect(tester.getRect(pill()).right, 320 - 16);
}
+3 -3
View File
@@ -161,14 +161,14 @@ void main() {
find
.ancestor(
of: find.byKey(const ValueKey('shell.destination.runs')).first,
matching: find.byType(GlassPill),
matching: find.byKey(FloatingDestinationBar.rowKey),
)
.first,
);
final itemTabs = tester.getRect(
find.descendant(
of: find.byKey(const Key('item-tabs')),
matching: find.byType(GlassPill),
matching: find.byKey(FloatingDestinationBar.rowKey),
),
);
final body = tester.getRect(
@@ -214,7 +214,7 @@ void main() {
final tabs = tester.getRect(
find.descendant(
of: find.byKey(const Key('item-tabs')),
matching: find.byType(GlassPill),
matching: find.byKey(FloatingDestinationBar.rowKey),
),
);
expect(tabs.bottom, greaterThan(844 - 40));
+10 -8
View File
@@ -5,7 +5,6 @@ import 'package:pipelines_app/shell/adaptive_shell.dart';
import 'package:pipelines_app/shell/destination.dart';
import 'package:pipelines_app/shell/shell_bar.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
Widget _app({required bool cupertino, required bool open, VoidCallback? back}) {
final shell = AdaptiveShell(
@@ -88,12 +87,12 @@ void main() {
final destinationPill = find.descendant(
of: find.byType(FloatingDestinationBar),
matching: find.byType(GlassPill),
matching: find.byKey(FloatingDestinationBar.rowKey),
);
// Sized to its four tabs at the bottom right.
expect(
tester.getSize(destinationPill).width,
FloatingDestinationBar.pillWidth(cupertino: cupertino, tabs: 4),
FloatingDestinationBar.rowWidth(cupertino: cupertino, tabs: 4),
);
expect(tester.getRect(destinationPill).right, 390 - 16);
await tester.tap(find.byKey(const ValueKey('shell.destination.chat')));
@@ -154,7 +153,7 @@ void main() {
),
);
await tester.pumpWidget(shell());
expect(find.byType(NavigationBar), findsOneWidget);
expect(find.byType(FloatingDestinationBar), findsOneWidget);
final navigator = tester.state<NavigatorState>(find.byType(Navigator));
navigator.push(
MaterialPageRoute<void>(
@@ -162,13 +161,16 @@ void main() {
),
);
await tester.pumpAndSettle();
expect(find.byType(NavigationBar), findsNothing);
expect(find.byType(NavigationBar, skipOffstage: false), findsOneWidget);
expect(find.byType(FloatingDestinationBar), findsNothing);
expect(
find.byType(FloatingDestinationBar, skipOffstage: false),
findsOneWidget,
);
await navigator.maybePop();
await tester.pump(const Duration(milliseconds: 16));
expect(find.byType(NavigationBar), findsOneWidget);
expect(find.byType(FloatingDestinationBar), findsOneWidget);
await tester.pumpAndSettle();
expect(find.byType(NavigationBar), findsOneWidget);
expect(find.byType(FloatingDestinationBar), findsOneWidget);
},
);
}
@@ -6,7 +6,6 @@ import 'package:pipelines_app/shell/destination.dart';
import 'package:pipelines_app/shell/list_detail.dart';
import 'package:pipelines_app/ui/adaptive/floating_bar_layout.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
/// While a tab shows its list beside an open item, the shell's destination
/// bar keeps its place at the bottom right (near the thumb). The item's own
@@ -101,7 +100,7 @@ void main() {
find
.ancestor(
of: find.byKey(const ValueKey('shell.destination.chat')).first,
matching: find.byType(GlassPill),
matching: find.byKey(FloatingDestinationBar.rowKey),
)
.first,
);
@@ -160,7 +159,7 @@ void main() {
find
.descendant(
of: find.byKey(const Key('detail-tabs')),
matching: find.byType(GlassPill),
matching: find.byKey(FloatingDestinationBar.rowKey),
)
.first,
);
@@ -200,7 +199,7 @@ void main() {
final pill = destinationPill(tester);
expect(
pill.width,
FloatingDestinationBar.pillWidth(cupertino: cupertino, tabs: 4),
FloatingDestinationBar.rowWidth(cupertino: cupertino, tabs: 4),
);
expect(pill.right, closeTo(390 - kFloatingBarMargin, 0.5));
});
+6 -3
View File
@@ -10,7 +10,7 @@ import 'package:pipelines_app/shell/destination.dart';
import 'package:pipelines_app/shell/detail_page.dart';
import 'package:pipelines_app/shell/list_detail.dart';
import 'package:pipelines_app/ui/adaptive/floating_bar_layout.dart';
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
import '../features/runs/ui/run_detail_graph_support.dart';
import '../features/runs/ui/run_detail_visit_support.dart';
@@ -21,14 +21,17 @@ import '../features/runs/ui/run_detail_visit_support.dart';
/// the detail pane instead of covering the shell.
void main() {
Rect pillOf(WidgetTester tester, Finder owner) => tester.getRect(
find.descendant(of: owner, matching: find.byType(GlassPill)).first,
find.descendant(
of: owner,
matching: find.byKey(FloatingDestinationBar.rowKey),
),
);
Rect shellPill(WidgetTester tester) => tester.getRect(
find
.ancestor(
of: find.byKey(const ValueKey('shell.destination.runs')).first,
matching: find.byType(GlassPill),
matching: find.byKey(FloatingDestinationBar.rowKey),
)
.first,
);
+7 -2
View File
@@ -171,14 +171,19 @@ void main() {
final body = tester.getRect(
find.byKey(const ValueKey('list-detail-body')),
);
final tabs = pillsOf(tester, find.byKey(const Key('item-tabs'))).single;
final tabs = tester.getRect(
find.descendant(
of: find.byKey(const Key('item-tabs')),
matching: find.byKey(FloatingDestinationBar.rowKey),
),
);
expect(tabs.left, greaterThanOrEqualTo(body.left));
expect(tabs.right, lessThanOrEqualTo(body.right));
final shellTabs = tester.getRect(
find
.ancestor(
of: find.byKey(const ValueKey('shell.destination.runs')).first,
matching: find.byType(GlassPill),
matching: find.byKey(FloatingDestinationBar.rowKey),
)
.first,
);
@@ -1,368 +0,0 @@
import 'dart:math' as math;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
/// The floating tab bar is sized to its tabs at the bottom right on every
/// width, and its selection circle keeps one gap everywhere: the first and
/// last circles sit as far from the pill's sides as from its top and bottom,
/// and neighbouring circles are that far apart too, on both hosts.
void main() {
const icons = [
Icons.account_tree_outlined,
Icons.play_circle_outline,
Icons.chat_bubble_outline,
Icons.search,
Icons.settings_outlined,
];
Key tabKey(int index) => ValueKey('tab-$index');
Widget bar({
required bool cupertino,
required int count,
required int selected,
ValueChanged<int>? onTap,
}) => FloatingDestinationBar(
cupertino: cupertino,
currentIndex: selected,
onTap: onTap ?? (_) {},
items: [
for (var index = 0; index < count; index += 1)
FloatingDestinationItem(
key: tabKey(index),
icon: Icon(icons[index]),
label: 'Tab $index',
),
],
);
Future<void> pump(
WidgetTester tester, {
required bool cupertino,
required Size size,
required Widget child,
double textScale = 1,
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final body = MediaQuery(
data: MediaQueryData(
size: size,
textScaler: TextScaler.linear(textScale),
),
child: child,
);
await tester.pumpWidget(
cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: body))
: MaterialApp(home: Scaffold(body: body)),
);
await tester.pumpAndSettle();
}
/// The bar's one selection highlight, at rest on tab [index].
Rect circle(WidgetTester tester, int index) {
final rect = tester.getRect(
find.descendant(
of: find.byKey(FloatingDestinationBar.highlightKey),
matching: find.byType(DecoratedBox),
),
);
final icon = tester.getCenter(
find.descendant(
of: find.byKey(tabKey(index)),
matching: find.byIcon(icons[index]),
),
);
expect(rect.center.dx, moreOrLessEquals(icon.dx, epsilon: 0.5));
expect(rect.center.dy, moreOrLessEquals(icon.dy, epsilon: 0.5));
return rect;
}
Rect pill(WidgetTester tester) => tester.getRect(
find.descendant(
of: find.byType(FloatingDestinationBar),
matching: find.byType(GlassPill),
),
);
final placements = [
(name: 'wide 1200, 4 tabs', size: const Size(1200, 800), count: 4),
(name: 'phone 390, 3 tabs', size: const Size(390, 844), count: 3),
(name: 'phone 390, 4 tabs', size: const Size(390, 844), count: 4),
(name: 'phone 560, 5 tabs', size: const Size(560, 844), count: 5),
(name: 'phone 320, 5 tabs', size: const Size(320, 640), count: 5),
];
for (final cupertino in [false, true]) {
final host = cupertino ? 'Cupertino' : 'Material';
testWidgets('$host: icon and selection circle sizes grow together', (
tester,
) async {
await pump(
tester,
cupertino: cupertino,
size: const Size(390, 844),
child: Align(
alignment: Alignment.bottomCenter,
child: bar(cupertino: cupertino, count: 2, selected: 0),
),
);
final icon = tester.getRect(find.byIcon(icons.first));
final mark = circle(tester, 0);
final iconSize = cupertino
? FloatingDestinationBar.cupertinoIconSize
: FloatingDestinationBar.materialIconSize;
final highlightSize = cupertino
? FloatingDestinationBar.cupertinoHighlightSize
: FloatingDestinationBar.materialHighlightSize;
expect(icon.width, iconSize);
expect(icon.height, iconSize);
expect(mark.width, highlightSize);
expect(mark.height, highlightSize);
});
testWidgets(
'$host: tab entrance animates once and respects reduced motion',
(tester) async {
tester.platformDispatcher.accessibilityFeaturesTestValue =
const FakeAccessibilityFeatures(disableAnimations: false);
addTearDown(() {
tester.platformDispatcher.accessibilityFeaturesTestValue =
const FakeAccessibilityFeatures(disableAnimations: true);
});
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
var selected = 0;
await tester.pumpWidget(
cupertino
? CupertinoApp(
home: CupertinoPageScaffold(
child: MediaQuery(
data: const MediaQueryData(disableAnimations: false),
child: StatefulBuilder(
builder: (context, setState) => bar(
cupertino: cupertino,
count: 3,
selected: selected,
onTap: (index) => setState(() => selected = index),
),
),
),
),
)
: MaterialApp(
home: Scaffold(
body: MediaQuery(
data: const MediaQueryData(disableAnimations: false),
child: StatefulBuilder(
builder: (context, setState) => bar(
cupertino: cupertino,
count: 3,
selected: selected,
onTap: (index) => setState(() => selected = index),
),
),
),
),
),
);
Finder entranceOf(int index) => find
.descendant(
of: find.byKey(tabKey(index)),
matching: find.byWidgetPredicate(
(widget) =>
widget is FadeTransition && widget.child is AnimatedBuilder,
),
)
.last;
final entrance = entranceOf(0);
double firstOpacity() =>
tester.widget<FadeTransition>(entranceOf(0)).opacity.value;
double lastOpacity() =>
tester.widget<FadeTransition>(entranceOf(2)).opacity.value;
expect(firstOpacity(), 0);
expect(lastOpacity(), 0);
await tester.pump(const Duration(milliseconds: 130));
expect(firstOpacity(), greaterThan(0.9));
expect(lastOpacity(), lessThan(0.2));
await tester.pump(const Duration(milliseconds: 270));
expect(firstOpacity(), 1);
expect(lastOpacity(), 1);
await tester.tap(find.byKey(tabKey(1)));
await tester.pump();
await tester.pumpWidget(
MediaQuery(
data: const MediaQueryData(disableAnimations: true),
child: cupertino
? CupertinoApp(
home: CupertinoPageScaffold(
child: bar(cupertino: cupertino, count: 3, selected: 0),
),
)
: MaterialApp(
home: Scaffold(
body: bar(cupertino: cupertino, count: 3, selected: 0),
),
),
),
);
expect(tester.widget<FadeTransition>(entrance.first).opacity.value, 1);
},
);
for (final placement in placements) {
testWidgets('$host ${placement.name}: compact at the bottom right; the '
'outer circles keep the top gap from the sides and neighbours', (
tester,
) async {
final count = placement.count;
final size = placement.size;
final centres = <double>[];
for (final selected in [0, count - 1]) {
await pump(
tester,
cupertino: cupertino,
size: size,
child: Align(
alignment: Alignment.bottomCenter,
child: bar(
cupertino: cupertino,
count: count,
selected: selected,
),
),
);
final box = pill(tester);
expect(box.right, size.width - 16);
expect(box.bottom, size.height - 16);
final mark = circle(tester, selected);
expect(mark.width, mark.height);
final top = mark.top - box.top;
expect(box.bottom - mark.bottom, moreOrLessEquals(top, epsilon: 1));
final side = selected == 0
? mark.left - box.left
: box.right - mark.right;
expect(
side,
moreOrLessEquals(top, epsilon: 1),
reason: 'side gap $side vs top gap $top',
);
// The pill's round end is concentric with the outer circle.
expect(box.height / 2, moreOrLessEquals(top + mark.width / 2));
}
// Unselected icons sit where their circle would: neighbours are
// evenly spaced, with the larger named neighbour gap.
for (var index = 0; index < count; index += 1) {
centres.add(
tester
.getCenter(
find.descendant(
of: find.byKey(tabKey(index)),
matching: find.byIcon(icons[index]),
),
)
.dx,
);
}
final last = circle(tester, count - 1);
final box = pill(tester);
final edgeGap = last.top - box.top;
final requestedNeighbourGap = cupertino
? FloatingDestinationBar.cupertinoNeighbourGap
: FloatingDestinationBar.materialNeighbourGap;
final neighbourGap = math.min(
requestedNeighbourGap,
(box.width -
2 *
(cupertino
? FloatingDestinationBar.cupertinoEdgeGap
: FloatingDestinationBar.materialEdgeGap) -
count * last.width) /
(count - 1),
);
for (var index = 1; index < count; index += 1) {
expect(
centres[index] - centres[index - 1] - last.width,
moreOrLessEquals(neighbourGap, epsilon: 1),
);
}
expect(
neighbourGap - edgeGap,
moreOrLessEquals(
neighbourGap -
(cupertino
? FloatingDestinationBar.cupertinoEdgeGap
: FloatingDestinationBar.materialEdgeGap),
epsilon: 1,
),
);
expect(last.center.dx, moreOrLessEquals(centres.last, epsilon: 1));
});
}
testWidgets('$host phone: taps beside the compact bar reach the content, '
'taps on it the tabs', (tester) async {
final tapped = <int>[];
var content = 0;
await pump(
tester,
cupertino: cupertino,
size: const Size(390, 844),
child: Stack(
children: [
Positioned.fill(
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => content++,
),
),
Align(
alignment: Alignment.bottomCenter,
child: bar(
cupertino: cupertino,
count: 3,
selected: 1,
onTap: tapped.add,
),
),
],
),
);
final box = pill(tester);
expect(box.width, lessThan(390 / 2));
await tester.tapAt(Offset(box.left - 40, box.center.dy));
await tester.tapAt(box.centerLeft + const Offset(12, 0));
await tester.tapAt(box.centerRight - const Offset(12, 0));
await tester.pump();
expect(content, 1);
expect(tapped, [0, 2]);
});
testWidgets('$host: 5 tabs at 320 with 2x text do not overflow', (
tester,
) async {
await pump(
tester,
cupertino: cupertino,
size: const Size(320, 640),
textScale: 2,
child: Align(
alignment: Alignment.bottomCenter,
child: bar(cupertino: cupertino, count: 5, selected: 4),
),
);
expect(tester.takeException(), isNull);
expect(pill(tester).right, 320 - 16);
expect(pill(tester).left, greaterThanOrEqualTo(16));
});
}
}
@@ -0,0 +1,254 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
/// The floating tab bar is a row of separate round glass buttons at the
/// bottom right on every width: each tab its own circle of the named size,
/// evenly spaced, the row keeping the margins from the edges, on both hosts.
void main() {
const icons = [
Icons.account_tree_outlined,
Icons.play_circle_outline,
Icons.chat_bubble_outline,
Icons.search,
Icons.settings_outlined,
];
Key tabKey(int index) => ValueKey('tab-$index');
Widget bar({
required bool cupertino,
required int count,
required int selected,
ValueChanged<int>? onTap,
}) => FloatingDestinationBar(
cupertino: cupertino,
currentIndex: selected,
onTap: onTap ?? (_) {},
items: [
for (var index = 0; index < count; index += 1)
FloatingDestinationItem(
key: tabKey(index),
icon: Icon(icons[index]),
label: 'Tab $index',
),
],
);
Future<void> pump(
WidgetTester tester, {
required bool cupertino,
required Size size,
required Widget child,
double textScale = 1,
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final body = MediaQuery(
data: MediaQueryData(
size: size,
textScaler: TextScaler.linear(textScale),
),
child: child,
);
await tester.pumpWidget(
cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: body))
: MaterialApp(home: Scaffold(body: body)),
);
await tester.pumpAndSettle();
}
/// Tab [index]'s glass circle.
Rect button(WidgetTester tester, int index) => tester.getRect(
find.descendant(
of: find.byKey(tabKey(index)),
matching: find.byType(GlassPill),
),
);
Rect row(WidgetTester tester) =>
tester.getRect(find.byKey(FloatingDestinationBar.rowKey));
final placements = [
(name: 'wide 1200, 4 tabs', size: const Size(1200, 800), count: 4),
(name: 'phone 390, 3 tabs', size: const Size(390, 844), count: 3),
(name: 'phone 390, 4 tabs', size: const Size(390, 844), count: 4),
(name: 'phone 560, 5 tabs', size: const Size(560, 844), count: 5),
];
for (final cupertino in [false, true]) {
final host = cupertino ? 'Cupertino' : 'Material';
final diameter = FloatingDestinationBar.buttonSize(cupertino: cupertino);
final gap = FloatingDestinationBar.buttonGap(cupertino: cupertino);
final iconSize = cupertino
? FloatingDestinationBar.cupertinoIconSize
: FloatingDestinationBar.materialIconSize;
test('$host: buttons are larger than the old 44/42 circle, hit targets '
'meet the platform minimum', () {
expect(diameter, cupertino ? 50 : 52);
expect(diameter, greaterThanOrEqualTo(cupertino ? 44 : 48));
expect(gap, inInclusiveRange(10, 12));
});
testWidgets('$host: each tab is its own glass circle with the icon '
'centred in it', (tester) async {
await pump(
tester,
cupertino: cupertino,
size: const Size(390, 844),
child: Align(
alignment: Alignment.bottomCenter,
child: bar(cupertino: cupertino, count: 3, selected: 0),
),
);
expect(
find.descendant(
of: find.byType(FloatingDestinationBar),
matching: find.byType(GlassPill),
),
findsNWidgets(3),
);
for (var index = 0; index < 3; index += 1) {
final circle = button(tester, index);
expect(circle.size, Size(diameter, diameter));
expect(
tester
.widget<GlassPill>(
find.descendant(
of: find.byKey(tabKey(index)),
matching: find.byType(GlassPill),
),
)
.shape,
isA<CircleBorder>(),
);
final icon = tester.getRect(find.byIcon(icons[index]));
expect(icon.size, Size(iconSize, iconSize));
expect(icon.center, circle.center);
}
});
for (final placement in placements) {
testWidgets('$host ${placement.name}: a row at the bottom right, '
'evenly spaced', (tester) async {
final count = placement.count;
final size = placement.size;
await pump(
tester,
cupertino: cupertino,
size: size,
child: Align(
alignment: Alignment.bottomCenter,
child: bar(cupertino: cupertino, count: count, selected: 0),
),
);
final box = row(tester);
expect(box.right, size.width - 16);
expect(box.bottom, size.height - 16);
expect(box.height, diameter);
expect(
box.width,
FloatingDestinationBar.rowWidth(cupertino: cupertino, tabs: count),
);
expect(button(tester, 0).left, box.left);
expect(button(tester, count - 1).right, box.right);
for (var index = 1; index < count; index += 1) {
expect(
button(tester, index).left - button(tester, index - 1).right,
moreOrLessEquals(gap),
);
expect(button(tester, index).top, box.top);
}
});
}
testWidgets('$host: taps between and beside the buttons reach the '
'content, taps on them the tabs', (tester) async {
final tapped = <int>[];
var content = 0;
await pump(
tester,
cupertino: cupertino,
size: const Size(390, 844),
child: Stack(
children: [
Positioned.fill(
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => content++,
),
),
Align(
alignment: Alignment.bottomCenter,
child: bar(
cupertino: cupertino,
count: 3,
selected: 1,
onTap: tapped.add,
),
),
],
),
);
final box = row(tester);
expect(box.width, lessThan(390 / 2));
await tester.tapAt(Offset(box.left - 40, box.center.dy));
final between = (button(tester, 0).right + button(tester, 1).left) / 2;
await tester.tapAt(Offset(between, box.center.dy));
await tester.tapAt(button(tester, 0).center);
await tester.tapAt(button(tester, 2).center);
await tester.pumpAndSettle();
expect(content, 2);
expect(tapped, [0, 2]);
});
testWidgets('$host: 5 tabs at 320 with 2x text narrow their gaps and '
'stay within the margins', (tester) async {
await pump(
tester,
cupertino: cupertino,
size: const Size(320, 640),
textScale: 2,
child: Align(
alignment: Alignment.bottomCenter,
child: bar(cupertino: cupertino, count: 5, selected: 4),
),
);
expect(tester.takeException(), isNull);
final box = row(tester);
expect(box.right, 320 - 16);
expect(box.left, greaterThanOrEqualTo(16));
final narrowed = (288 - 5 * diameter) / 4;
for (var index = 1; index < 5; index += 1) {
expect(button(tester, index).width, diameter);
expect(
button(tester, index).left - button(tester, index - 1).right,
moreOrLessEquals(narrowed < gap ? narrowed : gap),
);
}
});
testWidgets('$host: right to left, the row sits at the bottom left and '
'reads from the right', (tester) async {
await pump(
tester,
cupertino: cupertino,
size: const Size(390, 844),
child: Directionality(
textDirection: TextDirection.rtl,
child: Align(
alignment: Alignment.bottomCenter,
child: bar(cupertino: cupertino, count: 3, selected: 0),
),
),
);
expect(row(tester).left, 16);
expect(button(tester, 0).right, row(tester).right);
});
}
}
@@ -49,7 +49,7 @@ void main() {
),
),
);
final pill = tester.getRect(find.byType(GlassPill));
final pill = tester.getRect(find.byKey(FloatingDestinationBar.rowKey));
// The list fills the area, behind the bar.
expect(tester.getRect(find.byType(ListView)).bottom, 800);
await tester.drag(find.byType(ListView), const Offset(0, -5000));
@@ -67,11 +67,11 @@ void main() {
await tester.pumpWidget(
page(list: (_) => const SizedBox.expand(), onTap: () => taps++),
);
final pill = tester.getRect(find.byType(GlassPill));
final pill = tester.getRect(find.byKey(FloatingDestinationBar.rowKey));
expect(pill.right, 1200 - kFloatingBarMargin);
await tester.tapAt(Offset(pill.left - 24, pill.center.dy));
expect(taps, 1);
await tester.tapAt(pill.center);
await tester.tapAt(tester.getCenter(find.byType(GlassPill).first));
expect(taps, 1, reason: 'the bar takes its own taps');
});
}
+162 -137
View File
@@ -4,10 +4,13 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/shell/adaptive_shell.dart';
import 'package:pipelines_app/shell/destination.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
import 'package:pipelines_app/ui/adaptive/floating_tab_button.dart';
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
/// The floating tab bar's motion: the tabs pop in only when the bar appears
/// or its set of tabs changes, and one selection highlight glides (and
/// stretches on the way) to a newly selected tab, whose icon bounces.
/// The floating tab bar's motion: each button pops in on its own, staggered,
/// only when the bar appears or its set of tabs changes; the selection tint
/// cross-fades to a newly selected button, whose icon bounces; a press sinks
/// the button and it springs back.
void main() {
const icons = [
Icons.account_tree_outlined,
@@ -30,15 +33,16 @@ void main() {
void animate(WidgetTester tester, {required bool on}) {
tester.platformDispatcher.accessibilityFeaturesTestValue =
FakeAccessibilityFeatures(disableAnimations: !on);
addTearDown(() {
tester.platformDispatcher.accessibilityFeaturesTestValue =
const FakeAccessibilityFeatures(disableAnimations: true);
});
addTearDown(tester.platformDispatcher.clearAccessibilityFeaturesTestValue);
tester.view.physicalSize = const Size(560, 900);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
}
Widget app(bool cupertino, Widget body) => cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: body))
: MaterialApp(home: Scaffold(body: body));
/// A bar of [count] tabs whose tap selects.
Future<void> pumpBar(
WidgetTester tester, {
@@ -46,74 +50,92 @@ void main() {
int count = 3,
}) async {
var selected = 0;
final body = StatefulBuilder(
builder: (context, setState) {
return Align(
alignment: Alignment.bottomCenter,
child: FloatingDestinationBar(
cupertino: cupertino,
currentIndex: selected,
onTap: (index) => setState(() => selected = index),
items: items(count),
),
);
},
);
await tester.pumpWidget(
cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: body))
: MaterialApp(home: Scaffold(body: body)),
app(
cupertino,
StatefulBuilder(
builder: (context, setState) => Align(
alignment: Alignment.bottomCenter,
child: FloatingDestinationBar(
cupertino: cupertino,
currentIndex: selected,
onTap: (index) => setState(() => selected = index),
items: items(count),
),
),
),
),
);
}
/// The opacity of [tab]'s entrance fade.
double opacityOf(WidgetTester tester, Finder tab) => tester
.widget<FadeTransition>(
find
.descendant(
of: tab,
matching: find.byWidgetPredicate(
(widget) =>
widget is FadeTransition && widget.child is AnimatedBuilder,
),
)
.last,
)
.opacity
.value;
Finder glass(Finder tab) =>
find.descendant(of: tab, matching: find.byType(GlassPill));
double iconWidth(WidgetTester tester, Finder tab, IconData icon) => tester
.getRect(find.descendant(of: tab, matching: find.byIcon(icon)))
/// The opacity of [tab]'s entrance fade (its glass reveal).
double opacityOf(WidgetTester tester, Finder tab) =>
GlassReveal.maybeOf(tester.element(glass(tab)))?.value ?? 1;
/// [tab]'s drawn width (its entrance and press scale it).
double widthOf(WidgetTester tester, Finder tab) =>
tester.getRect(glass(tab)).width;
double iconWidth(WidgetTester tester, int index) => tester
.getRect(
find.descendant(
of: find.byKey(tabKey(index)),
matching: find.byIcon(icons[index]),
),
)
.width;
Rect highlight(WidgetTester tester) => tester.getRect(
find.descendant(
of: find.byKey(FloatingDestinationBar.highlightKey),
matching: find.byType(DecoratedBox),
),
);
Offset iconCentre(WidgetTester tester, int index) => tester.getCenter(
find.descendant(
of: find.byKey(tabKey(index)),
matching: find.byIcon(icons[index]),
),
);
/// The alpha of tab [index]'s selection fill.
double fillAlpha(WidgetTester tester, int index) {
final fill = tester.widget(
find.descendant(
of: find.byKey(tabKey(index)),
matching: find.byKey(FloatingTabButton.fillKey),
),
);
final decoration = fill is Ink
? fill.decoration
: (fill as DecoratedBox).decoration;
return (decoration! as ShapeDecoration).color!.a;
}
for (final cupertino in [false, true]) {
final host = cupertino ? 'Cupertino' : 'Material';
final size = FloatingDestinationBar.buttonSize(cupertino: cupertino);
final iconSize = cupertino
? FloatingDestinationBar.cupertinoIconSize
: FloatingDestinationBar.materialIconSize;
final size = cupertino
? FloatingDestinationBar.cupertinoHighlightSize
: FloatingDestinationBar.materialHighlightSize;
testWidgets('$host: the entrance does not replay when the selection '
'changes, and plays again for a new set of tabs', (tester) async {
testWidgets('$host: each button pops in on its own, staggered, from '
'0.6 to full size', (tester) async {
animate(tester, on: true);
await pumpBar(tester, cupertino: cupertino);
for (var index = 0; index < 3; index += 1) {
final tab = find.byKey(tabKey(index));
expect(opacityOf(tester, tab), 0);
expect(widthOf(tester, tab), moreOrLessEquals(size * 0.6));
}
await tester.pump(const Duration(milliseconds: 130));
final first = widthOf(tester, find.byKey(tabKey(0)));
final last = widthOf(tester, find.byKey(tabKey(2)));
expect(first, greaterThan(last + 4), reason: 'staggered');
expect(opacityOf(tester, find.byKey(tabKey(0))), greaterThan(0.9));
expect(opacityOf(tester, find.byKey(tabKey(2))), lessThan(0.2));
await tester.pumpAndSettle();
for (var index = 0; index < 3; index += 1) {
final tab = find.byKey(tabKey(index));
expect(opacityOf(tester, tab), 1);
expect(widthOf(tester, tab), size);
}
});
testWidgets('$host: the entrance does not replay when the selection '
'changes', (tester) async {
animate(tester, on: true);
await pumpBar(tester, cupertino: cupertino);
expect(opacityOf(tester, find.byKey(tabKey(0))), 0);
await tester.pumpAndSettle();
for (final target in [1, 2, 0]) {
await tester.tap(find.byKey(tabKey(target)));
@@ -122,14 +144,18 @@ void main() {
for (var index = 0; index < 3; index += 1) {
final tab = find.byKey(tabKey(index));
expect(opacityOf(tester, tab), 1, reason: 'tab $index faded');
// Only the bounce may grow an icon; a replayed pop shrinks it.
// Only the press may shrink the tapped button, and only a
// little; a replayed pop starts at 0.6.
expect(
iconWidth(tester, tab, icons[index]),
greaterThanOrEqualTo(iconSize - 0.01),
widthOf(tester, tab),
index == target
? greaterThan(size * 0.9)
: moreOrLessEquals(size),
reason: 'tab $index popped again',
);
}
}
await tester.pumpAndSettle();
}
});
@@ -137,24 +163,24 @@ void main() {
animate(tester, on: true);
var count = 3;
late StateSetter update;
Widget build() => StatefulBuilder(
builder: (context, setState) {
update = setState;
return Align(
alignment: Alignment.bottomCenter,
child: FloatingDestinationBar(
cupertino: cupertino,
currentIndex: 0,
onTap: (_) {},
items: items(count),
),
);
},
);
await tester.pumpWidget(
cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: build()))
: MaterialApp(home: Scaffold(body: build())),
app(
cupertino,
StatefulBuilder(
builder: (context, setState) {
update = setState;
return Align(
alignment: Alignment.bottomCenter,
child: FloatingDestinationBar(
cupertino: cupertino,
currentIndex: 0,
onTap: (_) {},
items: items(count),
),
);
},
),
),
);
await tester.pumpAndSettle();
expect(opacityOf(tester, find.byKey(tabKey(2))), 1);
@@ -164,84 +190,80 @@ void main() {
expect(opacityOf(tester, find.byKey(tabKey(2))), 0);
await tester.pumpAndSettle();
expect(opacityOf(tester, find.byKey(tabKey(3))), 1);
expect(widthOf(tester, find.byKey(tabKey(3))), size);
});
testWidgets('$host: the highlight rests on the selected tab as the old '
'circle did, glides and stretches between tabs, and the new icon '
'bounces', (tester) async {
testWidgets('$host: the selection tint cross-fades to the new button, '
'whose icon takes the selected colour and bounces', (tester) async {
animate(tester, on: true);
await pumpBar(tester, cupertino: cupertino);
await tester.pumpAndSettle();
expect(
highlight(tester),
rectMoreOrLessEquals(
Rect.fromCenter(
center: iconCentre(tester, 0),
width: size,
height: size,
),
epsilon: 0.01,
),
);
expect(find.byKey(FloatingDestinationBar.highlightKey), findsOneWidget);
final tint = fillAlpha(tester, 0);
expect(tint, greaterThan(0.1));
expect(fillAlpha(tester, 1), 0);
expect(fillAlpha(tester, 2), 0);
await tester.tap(find.byKey(tabKey(2)));
await tester.pump();
await tester.pump(const Duration(milliseconds: 60));
final from = iconCentre(tester, 0).dx;
final to = iconCentre(tester, 2).dx;
final moving = highlight(tester);
expect(moving.center.dx, greaterThan(from + 1));
expect(moving.center.dx, lessThan(to - 1));
expect(moving.height, moreOrLessEquals(size));
expect(moving.width, greaterThan(size * 1.1));
expect(moving.width, lessThanOrEqualTo(size * 1.25 + 0.01));
await tester.pump(const Duration(milliseconds: 80));
expect(fillAlpha(tester, 2), inExclusiveRange(0, tint));
expect(fillAlpha(tester, 0), inExclusiveRange(0, tint));
// The icon grows within its button (which the tap's press sinks).
expect(
iconWidth(tester, find.byKey(tabKey(2)), icons[2]),
greaterThan(iconSize * 1.02),
iconWidth(tester, 2) * size / widthOf(tester, find.byKey(tabKey(2))),
greaterThan(iconSize * 1.04),
);
expect(iconWidth(tester, 1), moreOrLessEquals(iconSize));
await tester.pumpAndSettle();
expect(fillAlpha(tester, 2), tint);
expect(fillAlpha(tester, 0), 0);
expect(iconWidth(tester, 2), moreOrLessEquals(iconSize));
final context = tester.element(find.byKey(tabKey(2)));
final selectedColour = cupertino
? CupertinoTheme.of(context).primaryColor
: Theme.of(context).colorScheme.onSecondaryContainer;
expect(
highlight(tester),
rectMoreOrLessEquals(
Rect.fromCenter(
center: iconCentre(tester, 2),
width: size,
height: size,
),
epsilon: 0.01,
),
IconTheme.of(tester.element(find.byIcon(icons[2]))).color,
isSameColorAs(selectedColour),
);
expect(
iconWidth(tester, find.byKey(tabKey(2)), icons[2]),
moreOrLessEquals(iconSize),
IconTheme.of(tester.element(find.byIcon(icons[0]))).color,
isNot(isSameColorAs(selectedColour)),
);
});
testWidgets('$host: under reduced motion the highlight jumps and the '
'icons neither pop nor bounce', (tester) async {
testWidgets('$host: a press sinks the button and it springs back', (
tester,
) async {
animate(tester, on: true);
await pumpBar(tester, cupertino: cupertino);
await tester.pumpAndSettle();
final tab = find.byKey(tabKey(1));
final gesture = await tester.startGesture(tester.getCenter(tab));
await tester.pump();
await tester.pump(const Duration(milliseconds: 120));
expect(widthOf(tester, tab), lessThan(size * 0.97));
expect(widthOf(tester, find.byKey(tabKey(0))), size);
await gesture.up();
await tester.pumpAndSettle();
expect(widthOf(tester, tab), size);
});
testWidgets('$host: under reduced motion the tint jumps and the buttons '
'neither pop nor bounce', (tester) async {
animate(tester, on: false);
await pumpBar(tester, cupertino: cupertino);
await tester.pump();
expect(opacityOf(tester, find.byKey(tabKey(1))), 1);
expect(widthOf(tester, find.byKey(tabKey(1))), size);
final tint = fillAlpha(tester, 0);
await tester.tap(find.byKey(tabKey(2)));
await tester.pump();
expect(
highlight(tester),
rectMoreOrLessEquals(
Rect.fromCenter(
center: iconCentre(tester, 2),
width: size,
height: size,
),
epsilon: 0.01,
),
);
expect(
iconWidth(tester, find.byKey(tabKey(2)), icons[2]),
moreOrLessEquals(iconSize),
);
await tester.pump();
expect(fillAlpha(tester, 2), tint);
expect(fillAlpha(tester, 0), 0);
expect(iconWidth(tester, 2), moreOrLessEquals(iconSize));
});
testWidgets('$host: switching the shell tabs never replays the main '
@@ -270,14 +292,17 @@ void main() {
for (var frame = 0; frame < 12; frame += 1) {
await tester.pump(const Duration(milliseconds: 25));
for (final tab in shellTabDestinations) {
final finder = find.byKey(
ValueKey('shell.destination.${tab.name}'),
);
expect(
opacityOf(
tester,
find.byKey(ValueKey('shell.destination.${tab.name}')),
),
opacityOf(tester, finder),
1,
reason: '${tab.name} faded switching to ${next.name}',
);
if (tab != next) {
expect(widthOf(tester, finder), size, reason: tab.name);
}
}
}
await tester.pumpAndSettle();
@@ -1,3 +1,5 @@
import 'dart:ui' show SemanticsRole;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
@@ -63,32 +65,58 @@ void main() {
await tester.pumpAndSettle();
final floating = find.byType(FloatingDestinationBar);
if (cupertino) {
// No label is drawn at all.
expect(
find.descendant(of: floating, matching: find.byType(Text)),
findsNothing,
);
} else {
// NavigationBar keeps its label hidden (for semantics only).
expect(
NavigationBarTheme.of(
tester.element(find.byType(NavigationDestination).first),
).labelBehavior,
NavigationDestinationLabelBehavior.alwaysHide,
);
}
// No label is drawn at all.
expect(
find.descendant(of: floating, matching: find.byType(Text)),
findsNothing,
);
for (final (index, (key, tabLabel)) in tabs.indexed) {
final node = tester.getSemantics(find.byKey(ValueKey(key)));
final finder = find.byKey(ValueKey(key));
final node = tester.getSemantics(finder);
expect(node.label, startsWith(tabLabel), reason: key);
expect(
node,
index == 0
? isSemantics(isSelected: true)
: isNot(isSemantics(isSelected: true)),
isSemantics(
isSelected: index == 0,
hasSelectedState: true,
isButton: true,
hasTapAction: true,
),
reason: key,
);
expect(node.role, SemanticsRole.tab, reason: key);
// "Tab n of m", as the host navigation controls announce it: in the
// Material label, the Cupertino hint.
final position = 'Tab ${index + 1} of ${tabs.length}';
expect(
cupertino ? node.hint : node.label,
contains(position),
reason: key,
);
// Material names the tab in its tooltip too.
if (!cupertino) {
expect(
find.descendant(
of: finder,
matching: find.byWidgetPredicate(
(widget) => widget is Tooltip && widget.message == tabLabel,
),
),
findsOneWidget,
reason: key,
);
}
// A hit target at least the platform minimum.
expect(
tester.getSize(finder).shortestSide,
greaterThanOrEqualTo(cupertino ? 44 : 48),
);
}
// The tabs make up one tab bar.
final tabBar = tester.getSemantics(
find.byKey(FloatingDestinationBar.rowKey),
);
expect(tabBar.role, SemanticsRole.tabBar);
// Distinct glyphs within the bar.
final icons = tester
.widgetList<Icon>(
@@ -184,10 +184,10 @@ void main() {
),
),
);
final pill = tester.getRect(find.byType(GlassPill));
final pill = tester.getRect(find.byKey(FloatingDestinationBar.rowKey));
expect(pill.top, 64);
// The pill is the bar's own height: leftover media padding is not added.
expect(pill.height, FloatingDestinationBar.materialHeight);
expect(pill.height, FloatingDestinationBar.materialButtonSize);
// Wide: sized to its tabs at the leading edge, under the title.
expect(pill.left, FloatingDestinationBar.topMargin);
expect(pill.width, lessThan(400));
+5 -4
View File
@@ -4,6 +4,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/main.dart';
import 'package:pipelines_app/ui/adaptive/segmented_bar.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
void main() {
for (final width in [390.0, 1000.0]) {
@@ -25,7 +26,7 @@ void main() {
expect(find.text('Pipelines'), findsWidgets);
expect(find.text('Pipelines — Not implemented'), findsOneWidget);
// One floating tab bar at every width; no rail.
expect(find.byType(NavigationBar), findsOneWidget);
expect(find.byType(FloatingDestinationBar), findsOneWidget);
expect(find.byType(NavigationRail), findsNothing);
expect(tester.takeException(), isNull);
await tester.tap(find.byKey(const ValueKey('shell.destination.runs')));
@@ -45,7 +46,7 @@ void main() {
await tester.pumpWidget(const MyApp(cupertino: true));
expect(find.byType(SegmentedBar), findsOneWidget);
expect(find.byType(CupertinoNavigationBar), findsNothing);
expect(find.byType(CupertinoTabBar), findsOneWidget);
expect(find.byType(FloatingDestinationBar), findsOneWidget);
expect(find.text('Pipelines — Not implemented'), findsOneWidget);
});
@@ -57,7 +58,7 @@ void main() {
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(const MyApp(cupertino: true));
expect(find.byType(CupertinoTabBar), findsOneWidget);
expect(find.byType(FloatingDestinationBar), findsOneWidget);
expect(find.byType(CupertinoListTile), findsNothing);
expect(
find.byKey(const ValueKey('shell.destination.pipelines')),
@@ -76,7 +77,7 @@ void main() {
tester.view.padding = const FakeViewPadding(top: 36, bottom: 28);
addTearDown(tester.view.resetPadding);
await tester.pumpWidget(const MyApp(cupertino: true));
final tabBar = tester.getRect(find.byType(CupertinoTabBar));
final tabBar = tester.getRect(find.byType(FloatingDestinationBar));
expect(tabBar.bottom, lessThanOrEqualTo(900 - 28));
expect(tester.takeException(), isNull);
});