feat(app): an open run takes over the app bar (Back, run, more menu); Search is the fifth tab; tabs keep their last screen
This commit is contained in:
+140
-101
@@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
|
||||
|
||||
import 'attention_badge.dart';
|
||||
import 'destination.dart';
|
||||
import 'shell_bar.dart';
|
||||
|
||||
class AdaptiveShell extends StatefulWidget {
|
||||
const AdaptiveShell({
|
||||
@@ -38,6 +39,13 @@ class AdaptiveShell extends StatefulWidget {
|
||||
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() {
|
||||
@@ -90,114 +98,145 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
|
||||
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(),
|
||||
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 LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final wide = constraints.maxWidth >= 600;
|
||||
if (widget.cupertino) {
|
||||
return CupertinoPageScaffold(
|
||||
navigationBar: CupertinoNavigationBar(
|
||||
middle: Text(details.label),
|
||||
trailing: widget.onSearch == null
|
||||
? null
|
||||
: CupertinoButton(
|
||||
key: const ValueKey('shell.search'),
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed: widget.onSearch,
|
||||
child: const Icon(
|
||||
CupertinoIcons.search,
|
||||
semanticLabel: 'Search',
|
||||
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,
|
||||
),
|
||||
),
|
||||
),
|
||||
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,
|
||||
automaticallyImplyLeading: false,
|
||||
middle: Text(spec?.title ?? details.label),
|
||||
trailing: spec != null && spec.actions.isNotEmpty
|
||||
? Row(
|
||||
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',
|
||||
),
|
||||
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,
|
||||
);
|
||||
}),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
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: [
|
||||
...?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,
|
||||
);
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(details.label),
|
||||
actions: [
|
||||
if (widget.onSearch != null)
|
||||
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,
|
||||
);
|
||||
}),
|
||||
),
|
||||
);
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
enum AppDestination { pipelines, runs, chat, settings }
|
||||
enum AppDestination { pipelines, runs, chat, search, settings }
|
||||
|
||||
({String label, IconData icon, IconData cupertinoIcon}) destinationDetails(
|
||||
AppDestination value,
|
||||
@@ -21,6 +21,11 @@ enum AppDestination { pipelines, runs, chat, settings }
|
||||
icon: Icons.chat_bubble_outline,
|
||||
cupertinoIcon: CupertinoIcons.chat_bubble_2,
|
||||
),
|
||||
AppDestination.search => (
|
||||
label: 'Search',
|
||||
icon: Icons.search,
|
||||
cupertinoIcon: CupertinoIcons.search,
|
||||
),
|
||||
AppDestination.settings => (
|
||||
label: 'Settings',
|
||||
icon: Icons.settings_outlined,
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import 'destination.dart';
|
||||
|
||||
/// What the open screen of a tab puts in the app bar: its title, a Back
|
||||
/// control and its actions. Absent, the bar shows the tab's own name.
|
||||
class ShellBarSpec {
|
||||
const ShellBarSpec({this.title, this.onBack, this.actions = const []});
|
||||
|
||||
final String? title;
|
||||
final VoidCallback? onBack;
|
||||
final List<Widget> actions;
|
||||
}
|
||||
|
||||
/// The bar contents each tab's open screen has published.
|
||||
class ShellBarRegistry extends ChangeNotifier {
|
||||
final Map<AppDestination, (Object, ShellBarSpec)> _specs = {};
|
||||
bool _disposed = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
ShellBarSpec? specFor(AppDestination destination) => _specs[destination]?.$2;
|
||||
|
||||
void _set(AppDestination destination, Object owner, ShellBarSpec spec) {
|
||||
// A screen's update can land after the shell itself has gone.
|
||||
if (_disposed) return;
|
||||
_specs[destination] = (owner, spec);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void _clear(AppDestination destination, Object owner) {
|
||||
if (_disposed) return;
|
||||
if (_specs[destination]?.$1 != owner) return;
|
||||
_specs.remove(destination);
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/// Places one tab's content under the shell's bar registry.
|
||||
class ShellBarScope extends InheritedWidget {
|
||||
const ShellBarScope({
|
||||
super.key,
|
||||
required this.registry,
|
||||
required this.destination,
|
||||
required super.child,
|
||||
});
|
||||
|
||||
final ShellBarRegistry registry;
|
||||
final AppDestination destination;
|
||||
|
||||
static ShellBarScope? maybeOf(BuildContext context) =>
|
||||
context.dependOnInheritedWidgetOfExactType<ShellBarScope>();
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(ShellBarScope oldWidget) =>
|
||||
registry != oldWidget.registry || destination != oldWidget.destination;
|
||||
}
|
||||
|
||||
/// Puts [title], [onBack] and [actions] in the app bar while [child] is shown.
|
||||
/// Outside a shell (a screen mounted on its own) it does nothing, so the
|
||||
/// screen keeps its own controls.
|
||||
class ShellBar extends StatefulWidget {
|
||||
const ShellBar({
|
||||
super.key,
|
||||
this.title,
|
||||
this.onBack,
|
||||
this.actions = const [],
|
||||
required this.child,
|
||||
});
|
||||
|
||||
final String? title;
|
||||
final VoidCallback? onBack;
|
||||
final List<Widget> actions;
|
||||
final Widget child;
|
||||
|
||||
/// Whether a shell bar will show these controls for the subtree at
|
||||
/// [context].
|
||||
static bool available(BuildContext context) =>
|
||||
ShellBarScope.maybeOf(context) != null;
|
||||
|
||||
@override
|
||||
State<ShellBar> createState() => _ShellBarState();
|
||||
}
|
||||
|
||||
class _ShellBarState extends State<ShellBar> {
|
||||
final Object _owner = Object();
|
||||
ShellBarScope? _scope;
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
_scope = ShellBarScope.maybeOf(context);
|
||||
}
|
||||
|
||||
void _publish() {
|
||||
final scope = _scope;
|
||||
if (scope == null) return;
|
||||
final spec = ShellBarSpec(
|
||||
title: widget.title,
|
||||
onBack: widget.onBack,
|
||||
actions: widget.actions,
|
||||
);
|
||||
// The bar is above this screen; update it after this frame's build.
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) scope.registry._set(scope.destination, _owner, spec);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
final scope = _scope;
|
||||
if (scope != null) {
|
||||
WidgetsBinding.instance.addPostFrameCallback(
|
||||
(_) => scope.registry._clear(scope.destination, _owner),
|
||||
);
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
_publish();
|
||||
return widget.child;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user