import 'dart:ui' show SemanticsRole; 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 Material `secondaryContainer` /// 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 its accessible label. 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.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 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 selection [Ink] inside each button, 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( 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: kIsWeb ? label : '$label\n${_tabLabel(context)}', // Switching tabs does not unfocus a text field (the composer). child: TextFieldTapRegion( child: Tooltip( message: label, excludeFromSemantics: true, child: button, ), ), ), ); } String _tabLabel(BuildContext context) => MaterialLocalizations.of( context, ).tabLabel(tabIndex: index + 1, tabCount: count); ({Color tint, Color selectedIcon, Color icon}) _colors(BuildContext 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 = 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( child: SizedBox.square( dimension: size, child: Stack( clipBehavior: Clip.none, alignment: Alignment.center, children: [ Positioned.fill( child: GlassPill(shape: const CircleBorder(), child: fill), ), IgnorePointer(child: glyph), ], ), ), ); } }