Compact floating tab bars with circular selection and equal edge gaps
The floating tab bar is now sized to its icon tabs at the bottom right on every width, the selected icon sits in a tinted circle on both hosts, and the gap from each outer circle to the pill's sides equals the gap to its top, bottom and neighbours.
This commit is contained in:
@@ -10,12 +10,15 @@ import 'glass_pill.dart';
|
||||
/// instead of becoming a full-width system bar. Used for the app's
|
||||
/// destinations and for every page's own tabs.
|
||||
///
|
||||
/// Placement follows the width the bar is given, live: below
|
||||
/// [kFloatingBarWideWidth] the pill spans it minus [kFloatingBarMargin] on
|
||||
/// each side; at or above it the pill is only as wide as its tabs and sits at
|
||||
/// the trailing (right, in left-to-right text) edge, near the thumb. Either
|
||||
/// way it keeps [kFloatingBarMargin] above the bottom safe area. Only the pill
|
||||
/// takes taps: the space beside it reaches the content behind.
|
||||
/// At any width the pill is only as wide as its icon tabs and 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. Only
|
||||
/// the pill takes taps: the space beside it reaches the content behind.
|
||||
///
|
||||
/// The selected tab's icon sits in a tinted circle. Each circle keeps the
|
||||
/// same gap from its neighbours as from the pill's top and bottom, and the
|
||||
/// outer ones the same gap from the pill's sides, so the pill's round ends
|
||||
/// are concentric with them.
|
||||
///
|
||||
/// The tabs show icons only. Each tab's label stays its accessible name (and
|
||||
/// the Material tooltip), so screen readers still announce it; the icons must
|
||||
@@ -38,24 +41,44 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
final bool cupertino;
|
||||
|
||||
/// The edge the bar floats at. At [FloatingBarEdge.top] (an open item's
|
||||
/// tabs under its pane's top bar) a wide bar sits at the leading edge, in
|
||||
/// line with the bar's title, and keeps no bottom safe area.
|
||||
/// 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<int> onTap;
|
||||
final List<FloatingDestinationItem> items;
|
||||
|
||||
/// Height of the Material bar inside the pill (the stock bar is 80; with
|
||||
/// labels hidden the icon and its indicator need less).
|
||||
/// labels hidden the icon and its highlight need less).
|
||||
static const double materialHeight = 56;
|
||||
|
||||
/// Height of the Cupertino bar inside the pill (the stock bar's 50).
|
||||
static const double cupertinoHeight = 50;
|
||||
|
||||
/// Width of one icon-only tab on a wide window: a comfortable touch target
|
||||
/// around the Material indicator (64) or the Cupertino selection capsule.
|
||||
static const double materialTabWidth = 72;
|
||||
static const double cupertinoTabWidth = 64;
|
||||
/// Padding between the icon (Material 24, Cupertino 26) and the edge of
|
||||
/// its selection circle.
|
||||
static const double _materialRing = 8;
|
||||
static const double _cupertinoRing = 6;
|
||||
|
||||
/// Diameter of the selection circle.
|
||||
static const double materialHighlightSize = 24 + 2 * _materialRing;
|
||||
static const double cupertinoHighlightSize = 26 + 2 * _cupertinoRing;
|
||||
|
||||
/// The gap between a tab's circle and the pill's top and bottom; the outer
|
||||
/// circles keep the same gap from the pill's sides, and neighbours from
|
||||
/// each other.
|
||||
static const double materialEdgeGap =
|
||||
(materialHeight - materialHighlightSize) / 2;
|
||||
static const double cupertinoEdgeGap =
|
||||
(cupertinoHeight - cupertinoHighlightSize) / 2;
|
||||
|
||||
/// Width of one tab: its circle and one edge gap (48 Material, 44
|
||||
/// Cupertino: the platforms' minimum touch targets). The pill is one more
|
||||
/// edge gap wider than its tabs.
|
||||
static const double materialTabWidth =
|
||||
materialHighlightSize + materialEdgeGap;
|
||||
static const double cupertinoTabWidth =
|
||||
cupertinoHighlightSize + cupertinoEdgeGap;
|
||||
|
||||
/// The side margin at [FloatingBarEdge.top], matching the top bar's own.
|
||||
static const double topMargin = 12;
|
||||
@@ -63,27 +86,18 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final top = edge == FloatingBarEdge.top;
|
||||
final margins = 2 * (top ? topMargin : kFloatingBarMargin);
|
||||
final bar = LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
// Wide is judged on the space the bar is given, margins included,
|
||||
// the way the shell and the pages judge their own layouts.
|
||||
final available = constraints.maxWidth;
|
||||
final wide = available + margins >= kFloatingBarWideWidth;
|
||||
final width = wide
|
||||
? math.min(available, _tabsWidth(context))
|
||||
: available;
|
||||
final width = math.max(0.0, math.min(constraints.maxWidth, _tabsWidth));
|
||||
return Align(
|
||||
// As tall as the pill, so a scaffold or overlay measures the bar
|
||||
// and not the whole area it may grow into.
|
||||
heightFactor: 1,
|
||||
alignment: !wide
|
||||
? Alignment.bottomCenter
|
||||
: top
|
||||
alignment: top
|
||||
? AlignmentDirectional.topStart
|
||||
: AlignmentDirectional.bottomEnd,
|
||||
child: SizedBox(
|
||||
width: math.max(0, width),
|
||||
width: width,
|
||||
// The insets are handled around the pill. The host bars pad
|
||||
// themselves by any media padding left over (a page's top bar,
|
||||
// another floating bar below a pane), which would swell the pill.
|
||||
@@ -95,9 +109,10 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
removeRight: true,
|
||||
child: GlassPill(
|
||||
cupertino: cupertino,
|
||||
child: cupertino
|
||||
? _cupertinoBar(context)
|
||||
: _materialBar(context),
|
||||
child: _edgeAligned(
|
||||
width,
|
||||
cupertino ? _cupertinoBar(context) : _materialBar(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -124,19 +139,48 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
double get _highlight =>
|
||||
cupertino ? cupertinoHighlightSize : materialHighlightSize;
|
||||
double get _gap => cupertino ? cupertinoEdgeGap : materialEdgeGap;
|
||||
|
||||
/// 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.
|
||||
double _tabsWidth(BuildContext context) =>
|
||||
(cupertino ? cupertinoTabWidth : materialTabWidth) * items.length;
|
||||
double get _tabsWidth => pillWidth(cupertino: cupertino, tabs: items.length);
|
||||
|
||||
/// 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;
|
||||
|
||||
/// 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
|
||||
/// that gap and narrows the gaps between neighbours instead, as long as
|
||||
/// 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,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _materialBar(BuildContext context) {
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
return NavigationBarTheme(
|
||||
data: NavigationBarThemeData(
|
||||
data: const NavigationBarThemeData(
|
||||
backgroundColor: Colors.transparent,
|
||||
elevation: 0,
|
||||
surfaceTintColor: Colors.transparent,
|
||||
indicatorColor: scheme.secondaryContainer,
|
||||
// The selected icon draws its own circle (the stock indicator is a
|
||||
// fixed 64x32); the stock one stays for its circular ink response.
|
||||
indicatorColor: Colors.transparent,
|
||||
indicatorShape: CircleBorder(),
|
||||
height: materialHeight,
|
||||
// Icons only; the label remains each tab's accessible name and its
|
||||
// tooltip.
|
||||
@@ -150,7 +194,11 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
NavigationDestination(
|
||||
key: item.key,
|
||||
icon: item.icon,
|
||||
selectedIcon: item.selectedIcon ?? item.icon,
|
||||
selectedIcon: _SelectionCircle(
|
||||
color: scheme.secondaryContainer,
|
||||
ring: _materialRing,
|
||||
child: item.selectedIcon ?? item.icon,
|
||||
),
|
||||
label: item.label,
|
||||
),
|
||||
],
|
||||
@@ -160,27 +208,27 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
|
||||
/// The stock Cupertino bar, transparent inside the pill. Each tab shows its
|
||||
/// icon only, named by its label for accessibility; the item's key covers
|
||||
/// the whole tab, and the selected tab's icon sits in a tinted capsule so
|
||||
/// the whole tab, and the selected tab's icon sits in a tinted circle so
|
||||
/// 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 icon = item.icon;
|
||||
if (selected) {
|
||||
icon = DecoratedBox(
|
||||
decoration: ShapeDecoration(
|
||||
color: active.withValues(alpha: 0.16),
|
||||
shape: const StadiumBorder(),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 2),
|
||||
child: item.selectedIcon ?? item.icon,
|
||||
),
|
||||
);
|
||||
}
|
||||
final icon = selected
|
||||
? _SelectionCircle(
|
||||
color: active.withValues(alpha: 0.16),
|
||||
ring: _cupertinoRing,
|
||||
child: item.selectedIcon ?? item.icon,
|
||||
)
|
||||
: item.icon;
|
||||
return KeyedSubtree(
|
||||
key: item.key,
|
||||
child: Semantics(label: item.label, child: icon),
|
||||
child: Semantics(
|
||||
label: item.label,
|
||||
// The host centres the icon above a 4px bottom inset; 4 above it
|
||||
// centres the icon and its circle in the whole bar, so the circle
|
||||
// is as far from the pill's top as from its bottom.
|
||||
child: Padding(padding: const EdgeInsets.only(top: 4), child: icon),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -208,6 +256,26 @@ class FloatingDestinationBar extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
/// The selected tab's icon (and any badge on it) in a tinted circle, [ring]
|
||||
/// wider than the icon on every side.
|
||||
class _SelectionCircle extends StatelessWidget {
|
||||
const _SelectionCircle({
|
||||
required this.color,
|
||||
required this.ring,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
final Color color;
|
||||
final double ring;
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => DecoratedBox(
|
||||
decoration: ShapeDecoration(color: color, shape: const CircleBorder()),
|
||||
child: Padding(padding: EdgeInsets.all(ring), child: child),
|
||||
);
|
||||
}
|
||||
|
||||
/// The platform-neutral data needed by [FloatingDestinationBar].
|
||||
class FloatingDestinationItem {
|
||||
const FloatingDestinationItem({
|
||||
|
||||
Reference in New Issue
Block a user