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 actions; } /// The bar contents each tab's open screen has published. class ShellBarRegistry extends ChangeNotifier { final Map _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(); @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 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 createState() => _ShellBarState(); } class _ShellBarState extends State { 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; } }