Merge: tabs as separate round glass buttons
This commit is contained in:
@@ -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),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user