import 'dart:math' as math; import 'dart:ui' show SemanticsRole; import 'package:flutter/widgets.dart'; import '../motion/motion.dart'; import 'floating_bar_layout.dart'; import 'floating_tab_button.dart'; import 'floating_tab_motion.dart'; import 'floating_destination_labels.dart'; /// 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. /// /// The row is only as wide as its buttons and, by default, 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. Hosts /// can request another alignment for a pane-specific layout. 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. /// /// 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. /// /// Page tabs show icons only by default; [showLabels] gives main navigation /// visible, scalable captions. 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. class FloatingDestinationBar extends StatefulWidget { const FloatingDestinationBar({ super.key, required this.currentIndex, required this.onTap, required this.items, this.edge = FloatingBarEdge.bottom, this.alignment, this.showLabels = false, }); /// Main app navigation keeps names visible; dense page tabs can stay icons. final bool showLabels; /// Where the row sits in the width it is given, at [FloatingBarEdge.bottom]: /// the trailing end by default. The shell can align its main tabs to the /// leading edge of a list pane while a list shows beside an open item. final AlignmentGeometry? alignment; /// The edge the bar floats at. At [FloatingBarEdge.top] (an open item's /// tabs under its pane's top bar) the bar sits at the leading edge, in line /// with the bar's title, and keeps no bottom safe area. final FloatingBarEdge edge; final int currentIndex; final ValueChanged onTap; final List items; /// The icon size inside each button, as in the host navigation controls. static const double materialIconSize = 28; /// The diameter of each tab button, which is also the bar's height. static const double materialButtonSize = 52; /// The gap between neighbouring buttons. static const double materialButtonGap = 12; /// The side margin at [FloatingBarEdge.top], matching the top bar's own. static const double topMargin = 12; /// The row of buttons inside each bar, for tests and measurement. static const Key rowKey = ValueKey('floating-destination-bar.row'); static double buttonSize() => materialButtonSize; static double buttonGap() => materialButtonGap; /// The selected button's size, about [Motion.tabSelectedScale] times the /// others (whole pixels, so layout stays exact). static const double materialSelectedButtonSize = 62; static double selectedButtonSize() => materialSelectedButtonSize; /// The width of a row of [tabs] buttons (one of them selected), when the /// space allows it. static double rowWidth({required int tabs}) => tabs == 0 ? 0 : buttonSize() * (tabs - 1) + selectedButtonSize() + buttonGap() * math.max(0, tabs - 1); @override State createState() => _FloatingDestinationBarState(); } class _FloatingDestinationBarState extends State with TickerProviderStateMixin { late final FloatingTabMotion _motion = FloatingTabMotion(this); final _backdrop = BackdropKey(); bool _entered = false; List get items => widget.items; int get currentIndex => widget.currentIndex; @override void didChangeDependencies() { super.didChangeDependencies(); if (_entered) return; _entered = true; _motion.enter(context, count: items.length, selected: currentIndex); } @override void didUpdateWidget(FloatingDestinationBar oldWidget) { super.didUpdateWidget(oldWidget); if (_tabSetIdentity(oldWidget.items) != _tabSetIdentity(items)) { // 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); } } @override void dispose() { _motion.dispose(); super.dispose(); } static String _tabSetIdentity(List items) => items.map((item) => '${item.key ?? item.label}:${item.label}').join('|'); double get _size => FloatingDestinationBar.buttonSize(); double get _selectedSize => FloatingDestinationBar.selectedButtonSize(); /// The buttons' natural width side by side, the selected one larger. double get _buttonsWidth => items.isEmpty ? 0 : _size * (items.length - 1) + _selectedSize; @override Widget build(BuildContext context) { final top = widget.edge == FloatingBarEdge.top; final bar = LayoutBuilder( 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 : widget.alignment ?? 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 // content below. return Padding( padding: const EdgeInsets.fromLTRB( FloatingDestinationBar.topMargin, 0, FloatingDestinationBar.topMargin, 8, ), child: bar, ); } return SafeArea( top: false, minimum: const EdgeInsets.fromLTRB( kFloatingBarMargin, 0, kFloatingBarMargin, kFloatingBarMargin, ), child: bar, ); } /// 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(); final count = items.length; if (count < 2 || !width.isFinite) return gap; return (width - _buttonsWidth).clamp(0.0, gap * (count - 1)) / (count - 1); } Widget _row(BuildContext context, double width) { if (widget.showLabels) { return FloatingDestinationLabels( key: FloatingDestinationBar.rowKey, labels: [for (final item in items) item.label], selected: currentIndex, buttonBuilder: (index) => _button(context, index), onTap: widget.onTap, ); } final gap = _gap(width); // As tall as the selected (largest) button; the others are centred on // it, and every gap stays the same while a button grows or shrinks. Widget row = Row( key: FloatingDestinationBar.rowKey, mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.center, children: [ for (var index = 0; index < items.length; index += 1) ...[ if (index > 0) SizedBox(width: gap), _button(context, index), ], ], ); // Narrower than the buttons themselves: the whole row shrinks. if (width.isFinite && width < _buttonsWidth) { row = SizedBox( width: math.max(0.0, width), height: _selectedSize, 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 row; } Widget _button(BuildContext context, int index) { final item = items[index]; final selected = index == currentIndex; final iconSize = FloatingDestinationBar.materialIconSize; // The selected button grows (and a deselected one shrinks back) in the // layout, so its neighbours move over and keep their gaps. return FloatingTabEntrance( motion: _motion, index: index, child: TweenAnimationBuilder( tween: Tween(end: selected ? _selectedSize : _size), duration: Motion.of(context, Motion.tabSelectDuration), curve: Motion.curve, builder: (context, size, _) => FloatingTabButton( key: item.key, motion: _motion, index: index, count: items.length, selected: selected, size: size, iconSize: iconSize * size / _size, label: item.label, icon: selected ? item.selectedIcon ?? item.icon : item.icon, onTap: () => widget.onTap(index), ), ), ); } } /// The platform-neutral data needed by [FloatingDestinationBar]. class FloatingDestinationItem { const FloatingDestinationItem({ required this.icon, required this.label, this.selectedIcon, this.key, }); final Widget icon; /// The tab's accessible name (and Material tooltip); not drawn. final String label; final Widget? selectedIcon; /// Identifies the whole tab, for tests and automation. final Key? key; }