Files
pipelines/app/lib/ui/adaptive/floating_tab_button.dart
T
alan 2176b396ac Tabs: the selected button grows about 20%, neighbours keep their gaps
The selected tab's whole button (62 px Material, 60 px Cupertino, from 52/50)
and its icon grow with the selection in the layout, so the other buttons move
over and every gap stays the same; the previous selection shrinks back. The
row is as tall as the selected button with the others centred on it.
2026-11-02 22:30:25 +00:00

190 lines
6.0 KiB
Dart

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),
],
),
),
);
}
}