garage_auth, garage_entitlements, garage_iap and garage_ui, moved out of Garage-Services and Metro-Map-Maker into one public repo. MIT, one readme, docs under docs/. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013F4NWNvYcdeSgqbWMT1VQ7
68 lines
1.7 KiB
Dart
68 lines
1.7 KiB
Dart
import "package:flutter/foundation.dart";
|
|
import "package:flutter/widgets.dart";
|
|
|
|
class AppMenuNotifier extends ChangeNotifier {
|
|
List<PlatformMenuItem> _menus = const [];
|
|
|
|
List<PlatformMenuItem> get menus => _menus;
|
|
|
|
void update(List<PlatformMenuItem> menus) {
|
|
_menus = menus;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
/// Keeps one native platform menu host above an application's provider tree.
|
|
class PlatformMenuHost extends StatefulWidget {
|
|
const PlatformMenuHost({
|
|
super.key,
|
|
required this.notifier,
|
|
required this.child,
|
|
});
|
|
|
|
final AppMenuNotifier notifier;
|
|
final Widget child;
|
|
|
|
@override
|
|
State<PlatformMenuHost> createState() => _PlatformMenuHostState();
|
|
}
|
|
|
|
class _PlatformMenuHostState extends State<PlatformMenuHost> {
|
|
List<PlatformMenuItem> _menus = const [];
|
|
|
|
static bool get _supported =>
|
|
!kIsWeb && defaultTargetPlatform == TargetPlatform.macOS;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_menus = widget.notifier.menus;
|
|
widget.notifier.addListener(_onMenusChanged);
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(PlatformMenuHost oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (widget.notifier == oldWidget.notifier) return;
|
|
oldWidget.notifier.removeListener(_onMenusChanged);
|
|
widget.notifier.addListener(_onMenusChanged);
|
|
_menus = widget.notifier.menus;
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
widget.notifier.removeListener(_onMenusChanged);
|
|
super.dispose();
|
|
}
|
|
|
|
void _onMenusChanged() {
|
|
if (mounted) setState(() => _menus = widget.notifier.menus);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (!_supported) return widget.child;
|
|
return PlatformMenuBar(menus: _menus, child: widget.child);
|
|
}
|
|
}
|