6fcdbcaec2
The selected icon grows to 1.15x with the selection tint (220 ms, same curve) and shrinks back when another tab is selected, on every floating tab bar; reduced motion jumps straight to the size.
195 lines
6.2 KiB
Dart
195 lines
6.2 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),
|
|
);
|
|
// The selected icon is a little larger, growing with the selection.
|
|
glyph = Transform.scale(
|
|
scale: 1 + (Motion.tabSelectedIconScale - 1) * t,
|
|
child: glyph,
|
|
);
|
|
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),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|