4a85a12cf7
The open conversation draws its own bar of separate rounded pieces: the platform Back in a circle, a title pill with the folder that opens the chat information, and a pill with the model and mode controls. Detail pages drop only their app bar for it, keeping the body mounted.
232 lines
6.0 KiB
Dart
232 lines
6.0 KiB
Dart
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 [],
|
|
this.barless = false,
|
|
});
|
|
|
|
final String? title;
|
|
final VoidCallback? onBack;
|
|
final List<Widget> actions;
|
|
|
|
/// The content draws its own floating bar: a detail page shows no bar of
|
|
/// its own and lets the content reach the top edge.
|
|
final bool barless;
|
|
}
|
|
|
|
/// The bar contents each tab's open screen has published.
|
|
class ShellBarRegistry extends ChangeNotifier {
|
|
final Map<AppDestination, (Object, ShellBarSpec)> _specs = {};
|
|
final Map<(AppDestination, ShellBadgeKind), int> _badges = {};
|
|
|
|
/// The count a tab's screen published for [kind], 0 when none.
|
|
int badge(AppDestination destination, ShellBadgeKind kind) =>
|
|
_badges[(destination, kind)] ?? 0;
|
|
|
|
void _setBadge(AppDestination destination, ShellBadgeKind kind, int count) {
|
|
if (_disposed || badge(destination, kind) == count) return;
|
|
_badges[(destination, kind)] = count;
|
|
notifyListeners();
|
|
}
|
|
|
|
bool _disposed = false;
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
super.dispose();
|
|
}
|
|
|
|
ShellBarSpec? specFor(AppDestination destination) => _specs[destination]?.$2;
|
|
|
|
/// The screen that published [destination]'s bar; it changes when another
|
|
/// screen takes the bar over.
|
|
Object? ownerFor(AppDestination destination) => _specs[destination]?.$1;
|
|
|
|
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 [],
|
|
this.active = true,
|
|
this.barless = false,
|
|
required this.child,
|
|
});
|
|
|
|
/// See [ShellBarSpec.barless].
|
|
final bool barless;
|
|
|
|
/// False while the screen is kept mounted but hidden (a list under an open
|
|
/// detail): it then gives the bar up.
|
|
final bool active;
|
|
|
|
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;
|
|
if (!widget.active) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted) scope.registry._clear(scope.destination, _owner);
|
|
});
|
|
return;
|
|
}
|
|
final spec = ShellBarSpec(
|
|
title: widget.title,
|
|
onBack: widget.onBack,
|
|
actions: widget.actions,
|
|
barless: widget.barless,
|
|
);
|
|
// 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;
|
|
}
|
|
}
|
|
|
|
/// What a tab badge counts.
|
|
enum ShellBadgeKind {
|
|
/// Runs still executing.
|
|
running,
|
|
|
|
/// Chats with messages not yet seen.
|
|
unread,
|
|
}
|
|
|
|
/// Publishes a count for this tab's badge while [child] is mounted.
|
|
class ShellBadge extends StatefulWidget {
|
|
const ShellBadge({
|
|
super.key,
|
|
required this.kind,
|
|
required this.count,
|
|
required this.child,
|
|
});
|
|
|
|
final ShellBadgeKind kind;
|
|
final int count;
|
|
final Widget child;
|
|
|
|
@override
|
|
State<ShellBadge> createState() => _ShellBadgeState();
|
|
}
|
|
|
|
class _ShellBadgeState extends State<ShellBadge> {
|
|
ShellBarScope? _scope;
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
_scope = ShellBarScope.maybeOf(context);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
final scope = _scope;
|
|
final kind = widget.kind;
|
|
if (scope != null) {
|
|
WidgetsBinding.instance.addPostFrameCallback(
|
|
(_) => scope.registry._setBadge(scope.destination, kind, 0),
|
|
);
|
|
}
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final scope = _scope;
|
|
if (scope != null) {
|
|
final count = widget.count;
|
|
final kind = widget.kind;
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted) scope.registry._setBadge(scope.destination, kind, count);
|
|
});
|
|
}
|
|
return widget.child;
|
|
}
|
|
}
|