From 425187233fade716a2bbcaa8d68ed4990b5e0428 Mon Sep 17 00:00:00 2001 From: Yesterday17 Date: Wed, 18 Sep 2024 04:35:01 +0800 Subject: [PATCH] fix: close panel on player route pop --- lib/router.dart | 2 ++ lib/ui/layout/layout.dart | 15 ++++++++++----- lib/ui/route/delegate.dart | 13 +++++++++++++ macos/Runner/AppDelegate.swift | 4 ++++ 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/router.dart b/lib/router.dart index 875eee8..f78af44 100644 --- a/lib/router.dart +++ b/lib/router.dart @@ -1,5 +1,6 @@ import 'package:annix/providers.dart'; import 'package:annix/services/theme.dart'; +import 'package:annix/ui/route/delegate.dart'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:annix/services/anniv/anniv_model.dart'; @@ -39,6 +40,7 @@ GoRouter buildRouter(Ref ref) { navigatorKey: _mainNavigatorKey, observers: [ ThemePopObserver(ref.read(themeProvider)), + PlayerRouteObserver(ref.read(routerProvider)), ], builder: (context, state, child) { return AnnixLayout(child: child); diff --git a/lib/ui/layout/layout.dart b/lib/ui/layout/layout.dart index 8d4baf2..51d8ff8 100644 --- a/lib/ui/layout/layout.dart +++ b/lib/ui/layout/layout.dart @@ -88,6 +88,11 @@ class AnnixLayout extends HookConsumerWidget { final currentIndex = pages.contains(router.location) ? pages.indexOf(router.location) : null; onDestinationSelected(final index) { + if (index >= pages.length && router.canPop()) { + router.pop(); + return; + } + context.go( pages[index], // pageBuilder: fadeThroughTransitionBuilder, @@ -214,11 +219,11 @@ class AnnixLayout extends HookConsumerWidget { ), ) .toList(), - // NavigationRailDestination( - // icon: const Icon(Icons.arrow_back), - // label: const Text('back'), - // disabled: !router.mayPop(), - // ) + const NavigationRailDestination( + icon: Icon(Icons.arrow_back), + label: Text('back'), + // disabled: !router.canPop(), + ) ], ), ), diff --git a/lib/ui/route/delegate.dart b/lib/ui/route/delegate.dart index 911e084..6e9f762 100644 --- a/lib/ui/route/delegate.dart +++ b/lib/ui/route/delegate.dart @@ -30,3 +30,16 @@ class AnnixRouterDelegate with ChangeNotifier { AnnixRouterDelegate(this.ref); } + +class PlayerRouteObserver extends NavigatorObserver { + final AnnixRouterDelegate delegate; + + PlayerRouteObserver(this.delegate); + + @override + didPop(final Route route, final Route? previousRoute) { + if (['/player'].contains(route.settings.name)) { + delegate.closePanel(); + } + } +} diff --git a/macos/Runner/AppDelegate.swift b/macos/Runner/AppDelegate.swift index 8e02df2..b3c1761 100644 --- a/macos/Runner/AppDelegate.swift +++ b/macos/Runner/AppDelegate.swift @@ -6,4 +6,8 @@ class AppDelegate: FlutterAppDelegate { override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { return true } + + override func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool { + return true + } }