334 lines
12 KiB
Dart
334 lines
12 KiB
Dart
import '../ui/motion/motion.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'attention_badge.dart';
|
|
import 'destination.dart';
|
|
import 'shell_bar.dart';
|
|
|
|
class AdaptiveShell extends StatefulWidget {
|
|
const AdaptiveShell({
|
|
super.key,
|
|
this.destinationBodies = const {},
|
|
this.cupertino = false,
|
|
this.destination,
|
|
this.onDestinationSelected,
|
|
this.onSearch,
|
|
this.attentionCount = 0,
|
|
});
|
|
|
|
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;
|
|
|
|
/// Opens the transient Search page. Null hides the action, so the plain
|
|
/// shell tests and compositions without routing keep their tab set only.
|
|
final VoidCallback? onSearch;
|
|
|
|
/// Runs needing the operator today, mirrored on the Runs tab in every mount
|
|
/// so it stays visible from any destination. Zero shows no badge.
|
|
final int attentionCount;
|
|
|
|
@override
|
|
State<AdaptiveShell> createState() => _AdaptiveShellState();
|
|
}
|
|
|
|
class _AdaptiveShellState extends State<AdaptiveShell> {
|
|
late AppDestination _selected;
|
|
final Set<AppDestination> _visited = {};
|
|
final ShellBarRegistry _bar = ShellBarRegistry();
|
|
|
|
@override
|
|
void dispose() {
|
|
_bar.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@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();
|
|
|
|
/// The destination's icon; only Runs carries the attention count.
|
|
Widget _icon(AppDestination destination, IconData data) {
|
|
Widget icon = Icon(data);
|
|
if (destination == AppDestination.chat) {
|
|
return ActivityBadge(
|
|
key: const ValueKey('shell.badge.chat.unread'),
|
|
count: _bar.badge(destination, ShellBadgeKind.unread),
|
|
label: 'unread chats',
|
|
cupertino: widget.cupertino,
|
|
child: icon,
|
|
);
|
|
}
|
|
if (destination != AppDestination.runs) return icon;
|
|
// Runs in progress at the bottom, runs that need you at the top.
|
|
icon = ActivityBadge(
|
|
key: const ValueKey('shell.badge.runs.running'),
|
|
count: _bar.badge(destination, ShellBadgeKind.running),
|
|
label: 'runs in progress',
|
|
cupertino: widget.cupertino,
|
|
bottom: true,
|
|
child: icon,
|
|
);
|
|
return AttentionDestinationIcon(
|
|
count: widget.attentionCount,
|
|
cupertino: widget.cupertino,
|
|
child: icon,
|
|
);
|
|
}
|
|
|
|
@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: ShellBarScope(
|
|
registry: _bar,
|
|
destination: destination,
|
|
child: _visited.contains(destination)
|
|
? widget.destinationBodies[destination]?.call(context) ??
|
|
Center(child: Text('${item.label} — Not implemented'))
|
|
: const SizedBox.shrink(),
|
|
),
|
|
);
|
|
}).toList();
|
|
return ListenableBuilder(
|
|
listenable: _bar,
|
|
builder: (context, _) => LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
final wide = constraints.maxWidth >= 600;
|
|
if (widget.cupertino) {
|
|
final spec = _bar.specFor(_selected);
|
|
return CupertinoPageScaffold(
|
|
navigationBar: CupertinoNavigationBar(
|
|
// The open screen's own bar: its Back, title and actions.
|
|
leading: spec?.onBack == null
|
|
? null
|
|
: CupertinoNavigationBarBackButton(
|
|
key: const ValueKey('shell.back'),
|
|
onPressed: spec!.onBack,
|
|
),
|
|
automaticallyImplyLeading: false,
|
|
middle: Text(spec?.title ?? details.label),
|
|
trailing: spec != null && spec.actions.isNotEmpty
|
|
? MotionSwitcher(
|
|
child: Row(
|
|
key: ObjectKey(_bar.ownerFor(_selected)),
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: spec.actions,
|
|
),
|
|
)
|
|
: widget.onSearch == null
|
|
? null
|
|
: CupertinoButton(
|
|
key: const ValueKey('shell.search'),
|
|
padding: EdgeInsets.zero,
|
|
onPressed: widget.onSearch,
|
|
child: const Icon(
|
|
CupertinoIcons.search,
|
|
semanticLabel: 'Search',
|
|
),
|
|
),
|
|
),
|
|
child: SafeArea(
|
|
child: wide
|
|
// The web keeps a destination rail on a wide window. A native
|
|
// Cupertino sidebar list is the same idea with the host's own
|
|
// controls; the tab bar stays the narrow-window navigation.
|
|
? Row(
|
|
children: [
|
|
_CupertinoSidebar(
|
|
selected: _selected,
|
|
onSelected: _select,
|
|
attentionCount: widget.attentionCount,
|
|
),
|
|
Expanded(child: _destinationStack(contents, index)),
|
|
],
|
|
)
|
|
: 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(destination, item.cupertinoIcon),
|
|
label: item.label,
|
|
);
|
|
}),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
final spec = _bar.specFor(_selected);
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
// The open screen's own bar: its Back, title and actions.
|
|
automaticallyImplyLeading: false,
|
|
leading: spec?.onBack == null
|
|
? null
|
|
: BackButton(
|
|
key: const ValueKey('shell.back'),
|
|
onPressed: spec!.onBack,
|
|
),
|
|
title: Text(spec?.title ?? details.label),
|
|
actions: [
|
|
if (spec != null && spec.actions.isNotEmpty)
|
|
// A screen taking the bar over cross-fades its actions in.
|
|
MotionSwitcher(
|
|
child: Row(
|
|
key: ObjectKey(_bar.ownerFor(_selected)),
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: spec.actions,
|
|
),
|
|
),
|
|
if (widget.onSearch != null && (spec?.actions.isEmpty ?? true))
|
|
IconButton(
|
|
key: const ValueKey('shell.search'),
|
|
onPressed: widget.onSearch,
|
|
tooltip: 'Search',
|
|
icon: const Icon(Icons.search),
|
|
),
|
|
],
|
|
),
|
|
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(destination, 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(destination, item.icon),
|
|
label: item.label,
|
|
);
|
|
}),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _destinationStack(List<Widget> contents, int index) =>
|
|
IndexedStack(index: index, children: contents);
|
|
}
|
|
|
|
/// The wide-window Cupertino destination list: the web's 200px rail with the
|
|
/// host's own grouped list controls.
|
|
class _CupertinoSidebar extends StatelessWidget {
|
|
const _CupertinoSidebar({
|
|
required this.selected,
|
|
required this.onSelected,
|
|
this.attentionCount = 0,
|
|
});
|
|
|
|
final AppDestination selected;
|
|
final ValueChanged<AppDestination> onSelected;
|
|
final int attentionCount;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = CupertinoTheme.of(context);
|
|
return Container(
|
|
width: 200,
|
|
decoration: BoxDecoration(
|
|
color: CupertinoColors.secondarySystemGroupedBackground.resolveFrom(
|
|
context,
|
|
),
|
|
border: Border(
|
|
right: BorderSide(
|
|
color: CupertinoColors.separator.resolveFrom(context),
|
|
),
|
|
),
|
|
),
|
|
child: ListView(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
children: [
|
|
for (final destination in AppDestination.values)
|
|
CupertinoListTile(
|
|
key: ValueKey('shell.destination.${destination.name}'),
|
|
leading: destination == AppDestination.runs
|
|
? AttentionDestinationIcon(
|
|
count: attentionCount,
|
|
cupertino: true,
|
|
child: Icon(
|
|
destinationDetails(destination).cupertinoIcon,
|
|
),
|
|
)
|
|
: Icon(destinationDetails(destination).cupertinoIcon),
|
|
title: Text(destinationDetails(destination).label),
|
|
trailing: destination == selected
|
|
? Icon(
|
|
CupertinoIcons.checkmark_alt,
|
|
color: theme.primaryColor,
|
|
)
|
|
: null,
|
|
onTap: () => onSelected(destination),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|