Files
pipelines/app/lib/shell/shell_bar.dart
T

214 lines
5.5 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 []});
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 = {};
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;
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,
required this.child,
});
/// 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,
);
// 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;
}
}