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:
@@ -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