92 lines
2.5 KiB
Dart
92 lines
2.5 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'glass_pill.dart';
|
|
|
|
/// A compact destination switcher which floats above the bottom edge instead
|
|
/// of becoming a full-width system bar. The controls inside remain the
|
|
/// platform navigation controls, so their labels, semantics and selection
|
|
/// behaviour are not recreated by the glass treatment.
|
|
class FloatingDestinationBar extends StatelessWidget {
|
|
const FloatingDestinationBar({
|
|
super.key,
|
|
required this.cupertino,
|
|
required this.currentIndex,
|
|
required this.onTap,
|
|
required this.items,
|
|
});
|
|
|
|
final bool cupertino;
|
|
final int currentIndex;
|
|
final ValueChanged<int> onTap;
|
|
final List<FloatingDestinationItem> items;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final width = MediaQuery.sizeOf(context).width;
|
|
final bar = cupertino ? _cupertinoBar() : _materialBar(context);
|
|
return SafeArea(
|
|
top: false,
|
|
minimum: const EdgeInsets.fromLTRB(16, 0, 16, 8),
|
|
child: Align(
|
|
alignment: Alignment.bottomCenter,
|
|
child: ConstrainedBox(
|
|
constraints: BoxConstraints(maxWidth: width - 32),
|
|
child: GlassPill(cupertino: cupertino, child: bar),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
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,
|
|
),
|
|
child: NavigationBar(
|
|
selectedIndex: currentIndex,
|
|
onDestinationSelected: onTap,
|
|
destinations: [
|
|
for (final item in items)
|
|
NavigationDestination(
|
|
icon: item.icon,
|
|
selectedIcon: item.selectedIcon ?? item.icon,
|
|
label: item.label,
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _cupertinoBar() => CupertinoTabBar(
|
|
backgroundColor: CupertinoColors.transparent,
|
|
currentIndex: currentIndex,
|
|
onTap: onTap,
|
|
items: [
|
|
for (final item in items)
|
|
BottomNavigationBarItem(
|
|
icon: item.icon,
|
|
activeIcon: item.selectedIcon ?? item.icon,
|
|
label: item.label,
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
/// The platform-neutral data needed by [FloatingDestinationBar].
|
|
class FloatingDestinationItem {
|
|
const FloatingDestinationItem({
|
|
required this.icon,
|
|
required this.label,
|
|
this.selectedIcon,
|
|
});
|
|
|
|
final Widget icon;
|
|
final String label;
|
|
final Widget? selectedIcon;
|
|
}
|