Animate and space floating tab bars
This commit is contained in:
@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
|
||||
|
||||
import 'floating_bar_layout.dart';
|
||||
import 'glass_pill.dart';
|
||||
import '../motion/motion.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
|
||||
@@ -79,12 +80,15 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
static const double materialEdgeGap = _materialEdgeGap;
|
||||
static const double cupertinoEdgeGap = _cupertinoEdgeGap;
|
||||
|
||||
/// Width of one tab: its circle and one edge gap. The pill is one more edge
|
||||
/// gap wider than its tabs.
|
||||
/// 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 + materialEdgeGap;
|
||||
materialHighlightSize + materialNeighbourGap;
|
||||
static const double cupertinoTabWidth =
|
||||
cupertinoHighlightSize + cupertinoEdgeGap;
|
||||
cupertinoHighlightSize + cupertinoNeighbourGap;
|
||||
|
||||
/// The side margin at [FloatingBarEdge.top], matching the top bar's own.
|
||||
static const double topMargin = 12;
|
||||
@@ -148,6 +152,8 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
double get _highlight =>
|
||||
cupertino ? cupertinoHighlightSize : materialHighlightSize;
|
||||
double get _gap => cupertino ? cupertinoEdgeGap : materialEdgeGap;
|
||||
double get _neighbourGap =>
|
||||
cupertino ? cupertinoNeighbourGap : 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.
|
||||
@@ -156,8 +162,12 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
/// The width of a pill holding [tabs] tabs, when the space allows it.
|
||||
static double pillWidth({required bool cupertino, required int tabs}) =>
|
||||
cupertino
|
||||
? cupertinoTabWidth * tabs + cupertinoEdgeGap
|
||||
: materialTabWidth * tabs + materialEdgeGap;
|
||||
? cupertinoHighlightSize * tabs +
|
||||
2 * cupertinoEdgeGap +
|
||||
math.max(0, tabs - 1) * cupertinoNeighbourGap
|
||||
: materialHighlightSize * tabs +
|
||||
2 * materialEdgeGap +
|
||||
math.max(0, tabs - 1) * materialNeighbourGap;
|
||||
|
||||
/// 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
|
||||
@@ -165,14 +175,31 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
/// the circles still fit side by side.
|
||||
Widget _edgeAligned(double width, Widget bar) {
|
||||
final count = items.length;
|
||||
var inset = _gap / 2;
|
||||
if (count > 1) {
|
||||
final slot = (width - _highlight - 2 * _gap) / (count - 1);
|
||||
inset = slot >= _highlight ? (width - slot * count) / 2 : 0;
|
||||
}
|
||||
return Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: math.max(0, inset)),
|
||||
child: bar,
|
||||
if (count < 2) 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 available = math.max(0.0, width - 2 * _gap - count * _highlight);
|
||||
final neighbour = math.min(_neighbourGap, available / (count - 1));
|
||||
final slot = _highlight + math.max(0, neighbour);
|
||||
final barWidth = slot * count;
|
||||
final offset = _gap - neighbour / 2;
|
||||
return ClipRect(
|
||||
child: SizedBox(
|
||||
width: width,
|
||||
height: cupertino ? cupertinoHeight : materialHeight,
|
||||
child: OverflowBox(
|
||||
alignment: AlignmentDirectional.topStart,
|
||||
minWidth: 0,
|
||||
maxWidth: double.infinity,
|
||||
child: Transform.translate(
|
||||
offset: Offset(offset, 0),
|
||||
child: SizedBox(width: barWidth, child: bar),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -199,16 +226,19 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
selectedIndex: currentIndex,
|
||||
onDestinationSelected: onTap,
|
||||
destinations: [
|
||||
for (final item in items)
|
||||
for (var index = 0; index < items.length; index += 1)
|
||||
NavigationDestination(
|
||||
key: item.key,
|
||||
icon: item.icon,
|
||||
key: items[index].key,
|
||||
icon: _entrance(items[index].icon, index),
|
||||
selectedIcon: _SelectionCircle(
|
||||
color: scheme.secondaryContainer,
|
||||
ring: _materialRing,
|
||||
child: item.selectedIcon ?? item.icon,
|
||||
child: _entrance(
|
||||
items[index].selectedIcon ?? items[index].icon,
|
||||
index,
|
||||
),
|
||||
),
|
||||
label: item.label,
|
||||
label: items[index].label,
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -221,14 +251,18 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
/// the selection reads at a glance, not by colour alone.
|
||||
Widget _cupertinoBar(BuildContext context) {
|
||||
final active = CupertinoTheme.of(context).primaryColor;
|
||||
Widget tab(FloatingDestinationItem item, {required bool selected}) {
|
||||
Widget tab(
|
||||
FloatingDestinationItem item, {
|
||||
required bool selected,
|
||||
required int index,
|
||||
}) {
|
||||
final icon = selected
|
||||
? _SelectionCircle(
|
||||
color: active.withValues(alpha: 0.16),
|
||||
ring: _cupertinoRing,
|
||||
child: item.selectedIcon ?? item.icon,
|
||||
child: _entrance(item.selectedIcon ?? item.icon, index),
|
||||
)
|
||||
: item.icon;
|
||||
: _entrance(item.icon, index);
|
||||
return KeyedSubtree(
|
||||
key: item.key,
|
||||
child: Semantics(
|
||||
@@ -256,13 +290,94 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
items: [
|
||||
for (var index = 0; index < items.length; index += 1)
|
||||
BottomNavigationBarItem(
|
||||
icon: tab(items[index], selected: false),
|
||||
activeIcon: tab(items[index], selected: true),
|
||||
icon: tab(items[index], selected: false, index: index),
|
||||
activeIcon: tab(items[index], selected: true, index: index),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _entrance(Widget child, int index) => _TabEntrance(
|
||||
key: ValueKey<Object?>('${_tabSetIdentity}_$index'),
|
||||
index: index,
|
||||
count: items.length,
|
||||
child: child,
|
||||
);
|
||||
|
||||
String get _tabSetIdentity =>
|
||||
items.map((item) => '${item.key ?? item.label}:${item.label}').join('|');
|
||||
}
|
||||
|
||||
class _TabEntrance extends StatefulWidget {
|
||||
const _TabEntrance({
|
||||
super.key,
|
||||
required this.index,
|
||||
required this.count,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
final int index;
|
||||
final int count;
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
State<_TabEntrance> createState() => _TabEntranceState();
|
||||
}
|
||||
|
||||
class _TabEntranceState extends State<_TabEntrance>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late final AnimationController _controller;
|
||||
bool _started = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller = AnimationController(
|
||||
vsync: this,
|
||||
duration:
|
||||
Motion.standard + Motion.tabEntranceStagger * (widget.count - 1),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
if (_started) return;
|
||||
_started = true;
|
||||
final duration = Motion.of(context, Motion.standard);
|
||||
_controller.duration =
|
||||
duration + Motion.tabEntranceStagger * (widget.count - 1);
|
||||
if (duration == Duration.zero) {
|
||||
_controller.value = 1;
|
||||
} else {
|
||||
_controller.forward();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final total = _controller.duration!.inMicroseconds;
|
||||
final start = Motion.tabEntranceStagger.inMicroseconds * widget.index;
|
||||
final begin = total == 0 ? 0.0 : start / total;
|
||||
final animation = CurvedAnimation(
|
||||
parent: _controller,
|
||||
curve: Interval(begin, 1, curve: Motion.curve),
|
||||
);
|
||||
return FadeTransition(
|
||||
opacity: animation,
|
||||
child: ScaleTransition(
|
||||
scale: Tween<double>(begin: 0.88, end: 1).animate(animation),
|
||||
child: widget.child,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The selected tab's icon (and any badge on it) in a tinted circle, [ring]
|
||||
|
||||
@@ -10,6 +10,9 @@ abstract final class Motion {
|
||||
/// A section opening or closing, a bar changing its actions.
|
||||
static const Duration standard = Duration(milliseconds: 200);
|
||||
|
||||
/// Delay between neighbouring controls entering as one group.
|
||||
static const Duration tabEntranceStagger = Duration(milliseconds: 40);
|
||||
|
||||
static const Curve curve = Curves.easeOutCubic;
|
||||
|
||||
/// [duration], or zero under the system's reduce-motion setting.
|
||||
|
||||
@@ -78,17 +78,10 @@ void main() {
|
||||
expect(rect.right, 1200 - 16);
|
||||
expect(rect.bottom, 800 - 16);
|
||||
expect(rect.width, lessThan(1200 * 0.6));
|
||||
// Four icon-only tabs (Settings opens from the Pipelines bar) and one
|
||||
// more edge gap, so the outer circles keep it from the sides.
|
||||
// Four icon-only tabs, with the wider neighbour gaps derived by the bar.
|
||||
expect(
|
||||
rect.width,
|
||||
4 *
|
||||
(cupertino
|
||||
? FloatingDestinationBar.cupertinoTabWidth
|
||||
: FloatingDestinationBar.materialTabWidth) +
|
||||
(cupertino
|
||||
? FloatingDestinationBar.cupertinoEdgeGap
|
||||
: FloatingDestinationBar.materialEdgeGap),
|
||||
FloatingDestinationBar.pillWidth(cupertino: cupertino, tabs: 4),
|
||||
);
|
||||
// No rail or sidebar: the content starts at the left edge.
|
||||
expect(find.byType(NavigationRail), findsNothing);
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
@@ -121,6 +123,85 @@ void main() {
|
||||
expect(mark.height, highlightSize);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'$host: tab entrance animates once and respects reduced motion',
|
||||
(tester) async {
|
||||
tester.platformDispatcher.accessibilityFeaturesTestValue =
|
||||
const FakeAccessibilityFeatures(disableAnimations: false);
|
||||
addTearDown(() {
|
||||
tester.platformDispatcher.accessibilityFeaturesTestValue =
|
||||
const FakeAccessibilityFeatures(disableAnimations: true);
|
||||
});
|
||||
tester.view.physicalSize = const Size(390, 844);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
var selected = 0;
|
||||
await tester.pumpWidget(
|
||||
cupertino
|
||||
? CupertinoApp(
|
||||
home: CupertinoPageScaffold(
|
||||
child: MediaQuery(
|
||||
data: const MediaQueryData(disableAnimations: false),
|
||||
child: StatefulBuilder(
|
||||
builder: (context, setState) => bar(
|
||||
cupertino: cupertino,
|
||||
count: 3,
|
||||
selected: selected,
|
||||
onTap: (index) => setState(() => selected = index),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
: MaterialApp(
|
||||
home: Scaffold(
|
||||
body: MediaQuery(
|
||||
data: const MediaQueryData(disableAnimations: false),
|
||||
child: StatefulBuilder(
|
||||
builder: (context, setState) => bar(
|
||||
cupertino: cupertino,
|
||||
count: 3,
|
||||
selected: selected,
|
||||
onTap: (index) => setState(() => selected = index),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
final entrance = find.descendant(
|
||||
of: find.byType(FloatingDestinationBar),
|
||||
matching: find.byType(FadeTransition),
|
||||
);
|
||||
expect(
|
||||
tester.widget<FadeTransition>(entrance.first).opacity.value,
|
||||
inInclusiveRange(0, 1),
|
||||
);
|
||||
await tester.pump(const Duration(milliseconds: 320));
|
||||
expect(tester.widget<FadeTransition>(entrance.first).opacity.value, 1);
|
||||
await tester.tap(find.byKey(tabKey(1)));
|
||||
await tester.pump();
|
||||
|
||||
await tester.pumpWidget(
|
||||
MediaQuery(
|
||||
data: const MediaQueryData(disableAnimations: true),
|
||||
child: cupertino
|
||||
? CupertinoApp(
|
||||
home: CupertinoPageScaffold(
|
||||
child: bar(cupertino: cupertino, count: 3, selected: 0),
|
||||
),
|
||||
)
|
||||
: MaterialApp(
|
||||
home: Scaffold(
|
||||
body: bar(cupertino: cupertino, count: 3, selected: 0),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(tester.widget<FadeTransition>(entrance.first).opacity.value, 1);
|
||||
},
|
||||
);
|
||||
|
||||
for (final placement in placements) {
|
||||
testWidgets('$host ${placement.name}: compact at the bottom right; the '
|
||||
'outer circles keep the top gap from the sides and neighbours', (
|
||||
@@ -162,7 +243,7 @@ void main() {
|
||||
expect(box.height / 2, moreOrLessEquals(top + mark.width / 2));
|
||||
}
|
||||
// Unselected icons sit where their circle would: neighbours are
|
||||
// evenly spaced, one edge gap apart.
|
||||
// evenly spaced, with the larger named neighbour gap.
|
||||
for (var index = 0; index < count; index += 1) {
|
||||
centres.add(
|
||||
tester
|
||||
@@ -176,13 +257,37 @@ void main() {
|
||||
);
|
||||
}
|
||||
final last = circle(tester, count - 1);
|
||||
final gap = last.top - pill(tester).top;
|
||||
final box = pill(tester);
|
||||
final edgeGap = last.top - box.top;
|
||||
final requestedNeighbourGap = cupertino
|
||||
? FloatingDestinationBar.cupertinoNeighbourGap
|
||||
: FloatingDestinationBar.materialNeighbourGap;
|
||||
final neighbourGap = math.min(
|
||||
requestedNeighbourGap,
|
||||
(box.width -
|
||||
2 *
|
||||
(cupertino
|
||||
? FloatingDestinationBar.cupertinoEdgeGap
|
||||
: FloatingDestinationBar.materialEdgeGap) -
|
||||
count * last.width) /
|
||||
(count - 1),
|
||||
);
|
||||
for (var index = 1; index < count; index += 1) {
|
||||
expect(
|
||||
centres[index] - centres[index - 1] - last.width,
|
||||
moreOrLessEquals(gap, epsilon: 1),
|
||||
moreOrLessEquals(neighbourGap, epsilon: 1),
|
||||
);
|
||||
}
|
||||
expect(
|
||||
neighbourGap - edgeGap,
|
||||
moreOrLessEquals(
|
||||
neighbourGap -
|
||||
(cupertino
|
||||
? FloatingDestinationBar.cupertinoEdgeGap
|
||||
: FloatingDestinationBar.materialEdgeGap),
|
||||
epsilon: 1,
|
||||
),
|
||||
);
|
||||
expect(last.center.dx, moreOrLessEquals(centres.last, epsilon: 1));
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user