From aba4f19963112e3647e142b09cad52c4ef9e157f Mon Sep 17 00:00:00 2001 From: fujidaiti Date: Sun, 5 May 2024 14:22:54 +0900 Subject: [PATCH 1/4] Immediately dispatch NoTransition if transition animation is completed --- package/lib/src/internal/transition_observer.dart | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/package/lib/src/internal/transition_observer.dart b/package/lib/src/internal/transition_observer.dart index 58914b9b..6f26cd6e 100644 --- a/package/lib/src/internal/transition_observer.dart +++ b/package/lib/src/internal/transition_observer.dart @@ -72,9 +72,13 @@ mixin TransitionAwareStateMixin void didPush(ModalRoute route, ModalRoute? previousRoute) { final currentState = _currentTransition; - if (previousRoute == null) { + if (previousRoute == null || route.animation!.isCompleted) { + // There is only one roue in the history stack, or multiple routes + // are pushed at the same time without transition animation. _setCurrentTransition(NoTransition(currentRoute: route)); } else if (route.isCurrent && currentState is NoTransition) { + // A new route is pushed on top of the stack with transition animation. + // Then, notify the listeners of the beginning of the transition. _setCurrentTransition(ForwardTransition( originRoute: currentState.currentRoute, destinationRoute: route, @@ -90,6 +94,7 @@ mixin TransitionAwareStateMixin } } + // Notify the listeners again when the transition is completed. route.animation!.addStatusListener(transitionStatusListener); } } From 45f8e69f31d0eca39020ebb9bcd0715436c658ab Mon Sep 17 00:00:00 2001 From: fujidaiti Date: Sun, 5 May 2024 14:35:34 +0900 Subject: [PATCH 2/4] Refactor --- .../lib/src/internal/transition_observer.dart | 45 +++++++++---------- .../lib/src/navigation/navigation_sheet.dart | 4 +- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/package/lib/src/internal/transition_observer.dart b/package/lib/src/internal/transition_observer.dart index 6f26cd6e..b8838903 100644 --- a/package/lib/src/internal/transition_observer.dart +++ b/package/lib/src/internal/transition_observer.dart @@ -1,6 +1,5 @@ import 'package:flutter/widgets.dart'; -@optionalTypeArgs class TransitionObserver extends NavigatorObserver { final Set _listeners = {}; @@ -58,59 +57,59 @@ mixin TransitionAwareWidgetMixin on StatefulWidget { mixin TransitionAwareStateMixin on State { - Transition? _currentTransition; + Transition? _lastReportedTransition; + Transition? get currentTransition => _lastReportedTransition; - void _setCurrentTransition(Transition? transition) { - if (_currentTransition != transition) { - _currentTransition = transition; - didChangeTransitionState(_currentTransition); + void _notify(Transition? transition) { + if (_lastReportedTransition != transition) { + _lastReportedTransition = transition; + didChangeTransitionState(transition); } } void didChangeTransitionState(Transition? transition); void didPush(ModalRoute route, ModalRoute? previousRoute) { - final currentState = _currentTransition; + final currentState = currentTransition; if (previousRoute == null || route.animation!.isCompleted) { // There is only one roue in the history stack, or multiple routes // are pushed at the same time without transition animation. - _setCurrentTransition(NoTransition(currentRoute: route)); + _notify(NoTransition(currentRoute: route)); } else if (route.isCurrent && currentState is NoTransition) { // A new route is pushed on top of the stack with transition animation. - // Then, notify the listeners of the beginning of the transition. - _setCurrentTransition(ForwardTransition( + // Then, notify the listener of the beginning of the transition. + _notify(ForwardTransition( originRoute: currentState.currentRoute, destinationRoute: route, animation: route.animation!, )); + // Notify the listener again when the transition is completed. void transitionStatusListener(AnimationStatus status) { if (status == AnimationStatus.completed && !route.offstage) { route.animation!.removeStatusListener(transitionStatusListener); - if (_currentTransition is ForwardTransition) { - _setCurrentTransition(NoTransition(currentRoute: route)); + if (currentTransition is ForwardTransition) { + _notify(NoTransition(currentRoute: route)); } } } - - // Notify the listeners again when the transition is completed. route.animation!.addStatusListener(transitionStatusListener); } } void didPop(ModalRoute route, ModalRoute? previousRoute) { if (previousRoute == null) { - _setCurrentTransition(null); + _notify(null); } else { - _setCurrentTransition(BackwardTransition( + _notify(BackwardTransition( originRoute: route, destinationRoute: previousRoute, animation: route.animation!.drive(Tween(begin: 1, end: 0)), )); route.completed.whenComplete(() { - if (_currentTransition is BackwardTransition) { - _setCurrentTransition(NoTransition(currentRoute: previousRoute)); + if (currentTransition is BackwardTransition) { + _notify(NoTransition(currentRoute: previousRoute)); } }); } @@ -120,7 +119,7 @@ mixin TransitionAwareStateMixin ModalRoute route, ModalRoute? previousRoute, ) { - _setCurrentTransition(UserGestureTransition( + _notify(UserGestureTransition( currentRoute: route, previousRoute: previousRoute!, animation: route.animation!.drive(Tween(begin: 1, end: 0)), @@ -128,8 +127,8 @@ mixin TransitionAwareStateMixin } void didStopUserGesture() { - if (_currentTransition case final UserGestureTransition state) { - _setCurrentTransition(NoTransition( + if (currentTransition case final UserGestureTransition state) { + _notify(NoTransition( currentRoute: state.currentRoute, )); } @@ -147,14 +146,14 @@ mixin TransitionAwareStateMixin if (widget.transitionObserver != oldWidget.transitionObserver) { oldWidget.transitionObserver.unmount(this); widget.transitionObserver.mount(this); - _setCurrentTransition(null); + _notify(null); } } @override void dispose() { widget.transitionObserver.unmount(this); - _setCurrentTransition(null); + _notify(null); super.dispose(); } } diff --git a/package/lib/src/navigation/navigation_sheet.dart b/package/lib/src/navigation/navigation_sheet.dart index 67abe75f..ca71cbe3 100644 --- a/package/lib/src/navigation/navigation_sheet.dart +++ b/package/lib/src/navigation/navigation_sheet.dart @@ -40,7 +40,6 @@ class NavigationSheet extends StatefulWidget with TransitionAwareWidgetMixin { class _NavigationSheetState extends State with TransitionAwareStateMixin, SheetExtentDelegate { final GlobalKey _scopeKey = GlobalKey(); - Transition? _currentTransition; @override void didChangeTransitionState(Transition? transition) { @@ -87,12 +86,11 @@ class _NavigationSheetState extends State }; _scopeKey.currentState?.extent.beginActivity(sheetActivity); - _currentTransition = transition; } @override SheetActivity createIdleActivity() { - return switch (_currentTransition) { + return switch (currentTransition) { NoTransition(:final NavigationSheetRoute currentRoute) => _ProxySheetActivity(entry: currentRoute), _ => IdleSheetActivity(), From 061f422e894971c5d33d431ecd7413f7c9d826ca Mon Sep 17 00:00:00 2001 From: fujidaiti Date: Sun, 5 May 2024 14:41:28 +0900 Subject: [PATCH 3/4] Bump to 0.5.2 --- package/CHANGELOG.md | 4 ++++ package/pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/package/CHANGELOG.md b/package/CHANGELOG.md index af5e83fd..2aa8122e 100644 --- a/package/CHANGELOG.md +++ b/package/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.5.2 May 5, 2024 + +- Fix a crash during the first build of `NavigationSheet` with a path that contains multiple routes such as `/a/b/c` (#109) + ## 0.5.1 May 4, 2024 - Re-export `NavigationSheetRoute` that is unintentionally omitted in v0.5.0 (#110) diff --git a/package/pubspec.yaml b/package/pubspec.yaml index 98d4575d..1148e37e 100644 --- a/package/pubspec.yaml +++ b/package/pubspec.yaml @@ -1,6 +1,6 @@ name: smooth_sheets description: Sheet widgets with smooth motion and great flexibility. Also supports nested navigation in both imperative and declarative ways. -version: 0.5.1 +version: 0.5.2 repository: https://github.com/fujidaiti/smooth_sheets environment: From bb7819cce7464a5e02e9d939568f4a281fa34e4c Mon Sep 17 00:00:00 2001 From: fujidaiti Date: Sun, 5 May 2024 14:44:32 +0900 Subject: [PATCH 4/4] Format --- package/lib/src/internal/transition_observer.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/package/lib/src/internal/transition_observer.dart b/package/lib/src/internal/transition_observer.dart index b8838903..620d1a7c 100644 --- a/package/lib/src/internal/transition_observer.dart +++ b/package/lib/src/internal/transition_observer.dart @@ -94,6 +94,7 @@ mixin TransitionAwareStateMixin } } } + route.animation!.addStatusListener(transitionStatusListener); } }