146 lines
4.7 KiB
Dart
146 lines
4.7 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'destination.dart';
|
|
|
|
class AdaptiveShell extends StatefulWidget {
|
|
const AdaptiveShell({
|
|
super.key,
|
|
this.destinationBodies = const {},
|
|
this.cupertino = false,
|
|
this.destination,
|
|
this.onDestinationSelected,
|
|
});
|
|
|
|
final Map<AppDestination, WidgetBuilder> destinationBodies;
|
|
final bool cupertino;
|
|
|
|
/// The destination a route owner selected. When null the shell keeps its own
|
|
/// selection, which preserves the plain-shell tests and native composition.
|
|
final AppDestination? destination;
|
|
final ValueChanged<AppDestination>? onDestinationSelected;
|
|
|
|
@override
|
|
State<AdaptiveShell> createState() => _AdaptiveShellState();
|
|
}
|
|
|
|
class _AdaptiveShellState extends State<AdaptiveShell> {
|
|
late AppDestination _selected;
|
|
final Set<AppDestination> _visited = {};
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_selected = widget.destination ?? AppDestination.pipelines;
|
|
_visited.add(_selected);
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant AdaptiveShell oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
final destination = widget.destination;
|
|
if (destination != null) {
|
|
_selected = destination;
|
|
_visited.add(destination);
|
|
}
|
|
}
|
|
|
|
void _select(AppDestination destination) {
|
|
final callback = widget.onDestinationSelected;
|
|
if (callback != null) {
|
|
callback(destination);
|
|
return;
|
|
}
|
|
setState(() {
|
|
_selected = destination;
|
|
_visited.add(destination);
|
|
});
|
|
}
|
|
|
|
List<T> _items<T>(T Function(AppDestination) build) =>
|
|
AppDestination.values.map(build).toList();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final index = AppDestination.values.indexOf(_selected);
|
|
final details = destinationDetails(_selected);
|
|
final contents = AppDestination.values.map((destination) {
|
|
final item = destinationDetails(destination);
|
|
return KeyedSubtree(
|
|
key: ValueKey(destination),
|
|
child: _visited.contains(destination)
|
|
? widget.destinationBodies[destination]?.call(context) ??
|
|
Center(child: Text('${item.label} — Not implemented'))
|
|
: const SizedBox.shrink(),
|
|
);
|
|
}).toList();
|
|
return LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
final wide = constraints.maxWidth >= 600;
|
|
if (widget.cupertino) {
|
|
return CupertinoPageScaffold(
|
|
navigationBar: CupertinoNavigationBar(middle: Text(details.label)),
|
|
child: SafeArea(
|
|
child: Column(
|
|
children: [
|
|
Expanded(child: _destinationStack(contents, index)),
|
|
CupertinoTabBar(
|
|
currentIndex: index,
|
|
onTap: (value) => _select(AppDestination.values[value]),
|
|
items: _items((destination) {
|
|
final item = destinationDetails(destination);
|
|
return BottomNavigationBarItem(
|
|
icon: Icon(item.cupertinoIcon),
|
|
label: item.label,
|
|
);
|
|
}),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text(details.label)),
|
|
body: Row(
|
|
children: [
|
|
if (wide)
|
|
NavigationRail(
|
|
selectedIndex: index,
|
|
onDestinationSelected: (value) =>
|
|
_select(AppDestination.values[value]),
|
|
labelType: NavigationRailLabelType.all,
|
|
destinations: _items((destination) {
|
|
final item = destinationDetails(destination);
|
|
return NavigationRailDestination(
|
|
icon: Icon(item.icon),
|
|
label: Text(item.label),
|
|
);
|
|
}),
|
|
),
|
|
Expanded(child: _destinationStack(contents, index)),
|
|
],
|
|
),
|
|
bottomNavigationBar: wide
|
|
? null
|
|
: NavigationBar(
|
|
selectedIndex: index,
|
|
onDestinationSelected: (value) =>
|
|
_select(AppDestination.values[value]),
|
|
destinations: _items((destination) {
|
|
final item = destinationDetails(destination);
|
|
return NavigationDestination(
|
|
icon: Icon(item.icon),
|
|
label: item.label,
|
|
);
|
|
}),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _destinationStack(List<Widget> contents, int index) =>
|
|
IndexedStack(index: index, children: contents);
|
|
}
|