fix(ui): honor disabled hero animations

This commit is contained in:
zarzet
2026-07-22 15:54:50 +07:00
parent bd6e2b4472
commit 4d77a0b178
3 changed files with 84 additions and 9 deletions
+10 -8
View File
@@ -192,16 +192,18 @@ class SpotiFLACApp extends ConsumerWidget {
// dialogs stay centered on large/foldable devices.
builder: (context, child) {
final mediaQuery = MediaQuery.of(context);
final appContent = _OrientationFade(
child: child ?? const SizedBox.shrink(),
);
return MediaQuery(
data: mediaQuery.copyWith(displayFeatures: const []),
// Global Hero kill switch: heroes below a disabled HeroMode
// never register, so no flights run on any navigator.
child: HeroMode(
enabled: heroAnimationsEnabled,
child: _OrientationFade(
child: child ?? const SizedBox.shrink(),
),
),
// HeroMode only affects heroes below a *route* subtree. At this
// level it sits above the router's Navigator, so the controller
// never encounters it while collecting heroes. Removing the
// inherited controller disables flights on the root Navigator.
child: heroAnimationsEnabled
? appContent
: HeroControllerScope.none(child: appContent),
);
},
routerConfig: router,
+12 -1
View File
@@ -506,6 +506,9 @@ class _MainShellState extends ConsumerState<MainShell>
final showStore = ref.watch(
settingsProvider.select((s) => s.showExtensionStore),
);
final heroAnimationsEnabled = ref.watch(
settingsProvider.select((s) => s.heroAnimationsEnabled),
);
ShellNavigationService.syncState(
currentTabIndex: _currentIndex,
showRepoTab: showStore,
@@ -519,12 +522,14 @@ class _MainShellState extends ConsumerState<MainShell>
key: const ValueKey('tab-home'),
navigatorKey: _homeTabNavigatorKey,
observers: [_homePreviewStopObserver],
heroAnimationsEnabled: heroAnimationsEnabled,
child: const HomeTab(),
),
_TabNavigator(
key: const ValueKey('tab-library'),
navigatorKey: _libraryTabNavigatorKey,
observers: [_libraryPreviewStopObserver],
heroAnimationsEnabled: heroAnimationsEnabled,
child: _LibraryTabRoot(parentPageController: _pageController),
),
if (showStore)
@@ -532,6 +537,7 @@ class _MainShellState extends ConsumerState<MainShell>
key: const ValueKey('tab-repo'),
navigatorKey: _repoTabNavigatorKey,
observers: [_repoPreviewStopObserver],
heroAnimationsEnabled: heroAnimationsEnabled,
child: const RepoTab(),
),
const SettingsTab(),
@@ -750,12 +756,14 @@ class _TabNavigator extends StatefulWidget {
final GlobalKey<NavigatorState> navigatorKey;
final Widget child;
final List<NavigatorObserver> observers;
final bool heroAnimationsEnabled;
const _TabNavigator({
super.key,
required this.navigatorKey,
required this.child,
this.observers = const [],
required this.heroAnimationsEnabled,
});
@override
@@ -778,7 +786,10 @@ class _TabNavigatorState extends State<_TabNavigator> {
Widget build(BuildContext context) {
return Navigator(
key: widget.navigatorKey,
observers: [_heroController, ...widget.observers],
observers: [
if (widget.heroAnimationsEnabled) _heroController,
...widget.observers,
],
onGenerateInitialRoutes: (_, _) => [
MaterialPageRoute<void>(builder: (_) => widget.child),
],
+62
View File
@@ -0,0 +1,62 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets(
'HeroControllerScope.none reaches the MaterialApp root Navigator',
(tester) async {
final navigatorKey = GlobalKey<NavigatorState>();
await tester.pumpWidget(
MaterialApp(
navigatorKey: navigatorKey,
builder: (context, child) =>
HeroControllerScope.none(child: child ?? const SizedBox.shrink()),
home: const SizedBox.shrink(),
),
);
final navigatorContext = navigatorKey.currentContext!;
final scope = navigatorContext
.getInheritedWidgetOfExactType<HeroControllerScope>();
expect(scope, isNotNull);
expect(scope!.controller, isNull);
},
);
testWidgets('removing an explicit HeroController detaches it', (
tester,
) async {
final navigatorKey = GlobalKey<NavigatorState>();
final controller = MaterialApp.createMaterialHeroController();
var enabled = true;
late StateSetter setHostState;
await tester.pumpWidget(
MaterialApp(
home: HeroControllerScope.none(
child: StatefulBuilder(
builder: (context, setState) {
setHostState = setState;
return Navigator(
key: navigatorKey,
observers: [if (enabled) controller],
onGenerateRoute: (_) => MaterialPageRoute<void>(
builder: (_) => const SizedBox.shrink(),
),
);
},
),
),
),
);
expect(controller.navigator, same(navigatorKey.currentState));
setHostState(() => enabled = false);
await tester.pump();
expect(controller.navigator, isNull);
controller.dispose();
});
}