6ab8551348
# Conflicts: # app/lib/application/templates_destination.dart # app/lib/features/templates/screen.dart # app/lib/features/templates/ui/templates_frame.dart
253 lines
8.5 KiB
Dart
253 lines
8.5 KiB
Dart
import 'dart:math' as math;
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'floating_bar_layout.dart';
|
|
import 'glass_pill.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
|
|
/// 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.
|
|
///
|
|
/// The controls inside remain the platform navigation controls (Material
|
|
/// [NavigationBar], Cupertino [CupertinoTabBar]), so tab semantics (selected
|
|
/// state, "tab n of m") come from the host. The Cupertino path builds no
|
|
/// Material widget.
|
|
class FloatingDestinationBar extends StatelessWidget {
|
|
const FloatingDestinationBar({
|
|
super.key,
|
|
required this.cupertino,
|
|
required this.currentIndex,
|
|
required this.onTap,
|
|
required this.items,
|
|
this.edge = FloatingBarEdge.bottom,
|
|
});
|
|
|
|
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.
|
|
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).
|
|
static const double materialHeight = 64;
|
|
|
|
/// Height of the Cupertino bar inside the pill (the stock bar is 50).
|
|
static const double cupertinoHeight = 54;
|
|
|
|
/// The side margin at [FloatingBarEdge.top], matching the top bar's own.
|
|
static const double topMargin = 12;
|
|
|
|
@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;
|
|
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
|
|
? AlignmentDirectional.topStart
|
|
: AlignmentDirectional.bottomEnd,
|
|
child: SizedBox(
|
|
width: math.max(0, 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.
|
|
child: MediaQuery.removePadding(
|
|
context: context,
|
|
removeTop: true,
|
|
removeBottom: true,
|
|
removeLeft: true,
|
|
removeRight: true,
|
|
child: GlassPill(
|
|
cupertino: cupertino,
|
|
child: cupertino
|
|
? _cupertinoBar(context)
|
|
: _materialBar(context),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
if (top) {
|
|
// Under a top bar: no safe area to keep clear of, only the gap to the
|
|
// content below.
|
|
return Padding(
|
|
padding: const EdgeInsets.fromLTRB(topMargin, 0, topMargin, 8),
|
|
child: bar,
|
|
);
|
|
}
|
|
return SafeArea(
|
|
top: false,
|
|
minimum: const EdgeInsets.fromLTRB(
|
|
kFloatingBarMargin,
|
|
0,
|
|
kFloatingBarMargin,
|
|
kFloatingBarMargin,
|
|
),
|
|
child: bar,
|
|
);
|
|
}
|
|
|
|
/// The width the tabs need side by side: every tab as wide as the widest
|
|
/// label (as the host lays them out), never narrower than a comfortable
|
|
/// touch target.
|
|
double _tabsWidth(BuildContext context) {
|
|
final TextStyle? style;
|
|
final TextScaler scaler;
|
|
final double minimum;
|
|
if (cupertino) {
|
|
style = CupertinoTheme.of(context).textTheme.tabLabelTextStyle;
|
|
scaler = TextScaler.noScaling;
|
|
minimum = 76;
|
|
} else {
|
|
final theme = Theme.of(context);
|
|
style =
|
|
theme.navigationBarTheme.labelTextStyle?.resolve(const {
|
|
WidgetState.selected,
|
|
}) ??
|
|
theme.textTheme.labelMedium;
|
|
// NavigationBar clamps its labels at 1.3x.
|
|
scaler = MediaQuery.textScalerOf(context).clamp(maxScaleFactor: 1.3);
|
|
minimum = 88;
|
|
}
|
|
var widest = 0.0;
|
|
for (final item in items) {
|
|
final painter = TextPainter(
|
|
text: TextSpan(text: item.label, style: style),
|
|
textScaler: scaler,
|
|
textDirection: Directionality.of(context),
|
|
maxLines: 1,
|
|
)..layout();
|
|
widest = math.max(widest, painter.width);
|
|
painter.dispose();
|
|
}
|
|
return math.max(minimum, widest + 24) * items.length;
|
|
}
|
|
|
|
Widget _materialBar(BuildContext context) {
|
|
final scheme = Theme.of(context).colorScheme;
|
|
return NavigationBarTheme(
|
|
data: NavigationBarThemeData(
|
|
backgroundColor: Colors.transparent,
|
|
elevation: 0,
|
|
surfaceTintColor: Colors.transparent,
|
|
indicatorColor: scheme.secondaryContainer,
|
|
height: materialHeight,
|
|
labelBehavior: NavigationDestinationLabelBehavior.alwaysShow,
|
|
),
|
|
child: NavigationBar(
|
|
selectedIndex: currentIndex,
|
|
onDestinationSelected: onTap,
|
|
destinations: [
|
|
for (final item in items)
|
|
NavigationDestination(
|
|
key: item.key,
|
|
icon: item.icon,
|
|
selectedIcon: item.selectedIcon ?? item.icon,
|
|
label: item.label,
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
/// The stock Cupertino bar, transparent inside the pill. Each tab's label is
|
|
/// drawn with its icon (in the bar's own label style and colours) so the
|
|
/// item's key covers the whole tab, and the selected tab's icon sits in a
|
|
/// tinted capsule 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,
|
|
),
|
|
);
|
|
}
|
|
return KeyedSubtree(
|
|
key: item.key,
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
icon,
|
|
const SizedBox(height: 2),
|
|
Text(item.label, maxLines: 1, softWrap: false),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
// The tab bar does not scale its text on iOS either; its height is fixed.
|
|
return MediaQuery.withNoTextScaling(
|
|
child: CupertinoTabBar(
|
|
backgroundColor: CupertinoColors.transparent,
|
|
border: null,
|
|
// Secondary-label translucency over glass falls below text contrast
|
|
// requirements. The host label role remains readable in both themes.
|
|
inactiveColor: CupertinoColors.label.resolveFrom(context),
|
|
iconSize: 24,
|
|
height: cupertinoHeight,
|
|
currentIndex: currentIndex,
|
|
onTap: onTap,
|
|
items: [
|
|
for (var index = 0; index < items.length; index += 1)
|
|
BottomNavigationBarItem(
|
|
icon: tab(items[index], selected: false),
|
|
activeIcon: tab(items[index], selected: true),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// The platform-neutral data needed by [FloatingDestinationBar].
|
|
class FloatingDestinationItem {
|
|
const FloatingDestinationItem({
|
|
required this.icon,
|
|
required this.label,
|
|
this.selectedIcon,
|
|
this.key,
|
|
});
|
|
|
|
final Widget icon;
|
|
final String label;
|
|
final Widget? selectedIcon;
|
|
|
|
/// Identifies the whole tab (icon and label), for tests and automation.
|
|
final Key? key;
|
|
}
|