Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

fix: stabilize KazumiDialog snackbar lifecycle

Prediditc4f2304f3cef
diff --git a/lib/app_widget.dart b/lib/app_widget.dart
index ee576ed9..d1a63047 100644
--- a/lib/app_widget.dart
+++ b/lib/app_widget.dart
@@ -10,6 +10,7 @@ import 'package:kazumi/services/logging/logger.dart';
import 'package:window_manager/window_manager.dart';
import 'package:kazumi/bean/dialog/dialog_helper.dart';
import 'package:kazumi/bean/settings/theme_provider.dart';
+import 'package:kazumi/navigation.dart';
import 'package:kazumi/utils/constants.dart';
import 'package:kazumi/utils/device.dart';
import 'package:kazumi/utils/theme.dart';
@@ -343,6 +344,7 @@ class _AppWidgetState extends State<AppWidget>
theme: lightTheme,
darkTheme: effectiveDarkTheme,
themeMode: themeProvider.themeMode,
+ scaffoldMessengerKey: rootScaffoldMessengerKey,
routerConfig: ModularApp.routerConfigOf(context),
);
},
diff --git a/lib/bean/dialog/dialog_helper.dart b/lib/bean/dialog/dialog_helper.dart
index 9fd64976..b29034fe 100644
--- a/lib/bean/dialog/dialog_helper.dart
+++ b/lib/bean/dialog/dialog_helper.dart
@@ -1,5 +1,6 @@
import 'dart:async';
import 'package:flutter/material.dart';
+import 'package:kazumi/navigation.dart';
import 'package:kazumi/utils/constants.dart';
// A simple dialog helper class to show dialogs and toasts based on flutter native implementation (replace flutter_smart_dialog)
@@ -19,7 +20,8 @@ class KazumiDialog {
VoidCallback? onDismiss,
required WidgetBuilder builder,
}) async {
- final ctx = context ?? observer.currentContext;
+ final ctx =
+ context ?? rootNavigatorKey.currentContext ?? observer.currentContext;
if (ctx != null && ctx.mounted) {
try {
final result = await showDialog<T>(
@@ -50,16 +52,17 @@ class KazumiDialog {
Function()? onActionPressed,
Duration duration = const Duration(seconds: 2),
}) {
- final ctx = context ?? observer.scaffoldContext;
- if (ctx != null && ctx.mounted) {
+ final messenger = _resolveScaffoldMessenger(context);
+ final toastContext = _resolveToastContext(context);
+ if (messenger != null && toastContext != null && toastContext.mounted) {
try {
- ScaffoldMessenger.of(ctx)
+ messenger
..removeCurrentSnackBar()
..showSnackBar(
SnackBar(
content: Text(message),
behavior: SnackBarBehavior.floating,
- width: MediaQuery.sizeOf(ctx).width >
+ width: MediaQuery.sizeOf(toastContext).width >
LayoutBreakpoint.medium['width']!
? 600
: null,
@@ -70,7 +73,7 @@ class KazumiDialog {
label: actionLabel ?? 'Dismiss',
onPressed: () {
onActionPressed?.call();
- ScaffoldMessenger.of(ctx).hideCurrentSnackBar();
+ messenger.hideCurrentSnackBar();
},
)
: null,
@@ -81,7 +84,7 @@ class KazumiDialog {
}
} else {
debugPrint(
- 'Kazumi Dialog Error: No Scaffold context available to show Toast');
+ 'Kazumi Dialog Error: No ScaffoldMessenger available to show Toast');
}
}
@@ -91,7 +94,8 @@ class KazumiDialog {
bool barrierDismissible = false,
Function()? onDismiss,
}) async {
- final ctx = context ?? observer.currentContext;
+ final ctx =
+ context ?? rootNavigatorKey.currentContext ?? observer.currentContext;
if (ctx != null && ctx.mounted) {
try {
await showDialog(
@@ -152,7 +156,10 @@ class KazumiDialog {
bool useSafeArea = false,
}) async {
// Use provided context first, then root context, then fallback to current context
- final ctx = context ?? observer.rootContext ?? observer.currentContext;
+ final ctx = context ??
+ rootNavigatorKey.currentContext ??
+ observer.rootContext ??
+ observer.currentContext;
if (ctx != null && ctx.mounted) {
try {
final result = await showModalBottomSheet<T>(
@@ -225,6 +232,29 @@ class KazumiDialog {
}
});
}
+
+ static ScaffoldMessengerState? _resolveScaffoldMessenger(
+ BuildContext? context,
+ ) {
+ if (context != null && context.mounted) {
+ final scopedMessenger = ScaffoldMessenger.maybeOf(context);
+ if (scopedMessenger != null) {
+ return scopedMessenger;
+ }
+ }
+ return rootScaffoldMessengerKey.currentState;
+ }
+
+ static BuildContext? _resolveToastContext(BuildContext? context) {
+ if (context != null && context.mounted) {
+ return context;
+ }
+ final messengerContext = rootScaffoldMessengerKey.currentContext;
+ if (messengerContext != null && messengerContext.mounted) {
+ return messengerContext;
+ }
+ return observer.scaffoldContext;
+ }
}
class _TimedSuccessDialog extends StatefulWidget {
@@ -315,6 +345,7 @@ class _TimedSuccessDialogState extends State<_TimedSuccessDialog> {
class KazumiDialogObserver extends NavigatorObserver {
/// List of active dialog routes
final List<Route<dynamic>> _kazumiDialogRoutes = [];
+ bool _snackBarClearScheduled = false;
/// The most recent context from any MaterialPageRoute or PopupRoute
BuildContext? _currentContext;
@@ -342,12 +373,6 @@ class KazumiDialogObserver extends NavigatorObserver {
@override
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didPush(route, previousRoute);
-
- /// workaround for #533
- /// we can't remove snackbar when push a new route
- /// otherwise, framework will throw an exception, and can't be caught
- /// need other way to remove snackbar here
- // _removeCurrentSnackBar(previousRoute);
if (_isKazumiDialogRoute(route)) {
_kazumiDialogRoutes.add(route);
}
@@ -359,7 +384,7 @@ class KazumiDialogObserver extends NavigatorObserver {
@override
void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didPop(route, previousRoute);
- _removeCurrentSnackBar(route);
+ _scheduleSnackBarClear();
if (_isKazumiDialogRoute(route)) {
_kazumiDialogRoutes.remove(route);
}
@@ -371,6 +396,7 @@ class KazumiDialogObserver extends NavigatorObserver {
@override
void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
+ _scheduleSnackBarClear();
if (_isKazumiDialogRoute(oldRoute!)) {
_kazumiDialogRoutes.remove(oldRoute);
}
@@ -385,6 +411,7 @@ class KazumiDialogObserver extends NavigatorObserver {
@override
void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didRemove(route, previousRoute);
+ _scheduleSnackBarClear();
if (_isKazumiDialogRoute(route)) {
_kazumiDialogRoutes.remove(route);
@@ -414,12 +441,15 @@ class KazumiDialogObserver extends NavigatorObserver {
route.settings.name == 'KazumiBottomSheet';
}
- void _removeCurrentSnackBar(Route<dynamic>? route) {
- if (route?.navigator?.context != null) {
- try {
- ScaffoldMessenger.maybeOf(route!.navigator!.context)
- ?.removeCurrentSnackBar();
- } catch (_) {}
- }
+ void _scheduleSnackBarClear() {
+ if (_snackBarClearScheduled) return;
+ _snackBarClearScheduled = true;
+ WidgetsBinding.instance.addPostFrameCallback((_) {
+ _snackBarClearScheduled = false;
+ // Route observer callbacks run while Navigator is reconciling routes.
+ // Clearing the root messenger after the frame avoids mutating UI state
+ // in the middle of that reconciliation.
+ rootScaffoldMessengerKey.currentState?.removeCurrentSnackBar();
+ });
}
}
diff --git a/lib/navigation.dart b/lib/navigation.dart
index fe32ee0c..d8b39bf0 100644
--- a/lib/navigation.dart
+++ b/lib/navigation.dart
@@ -1,3 +1,4 @@
import 'package:flutter/material.dart';
final rootNavigatorKey = GlobalKey<NavigatorState>();
+final rootScaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();