0

I want to change the reverseTransitionDuration of a PageRoute dynamically with state. I initially thought the only way to do this would be to create a StatefulWidget above the PageRoute's Navigator.

However, playing around with GoRouter and ValueNotifier I wonder if there is a simpler way to do this by creating a ValueNotifier directly in the pageBuilder of a ShellRoute to store the a Duration. Then to pass that value to the reverseTransitionDuration of CustomTransitionPage. And finally put an InheritedWidget to wrap the child passed to the child property of CustomTransitionPage that contains the ValueNotifier<Duration>.

This should allow this value notifier to be set with a static function that can be called anywhere further down the tree. However I am not convinced that reverseTransitionDuration property of CustomTransitionPage would actually update as it is not technically listening.

Here is some code to show what I mean. Would this work and if not is there a way of being able to do this without having to create a StatefulWidget / Inherited State above the route's navigator.

   ShellRoute(
     pageBuilder: (context, state, child){ 
        // create value notifier
        ValueNotifier<Duration> reverseTransitionDuration = 
            ValueNotifier(Duration(milliseconds: 300));

        return CustomTransitionPage<T>(
          key: state.pageKey,
          reverseTransitionDuration: reverseTransitionDuration.value, 
          // would this actually update the PageRoute reverseTransitionDuration

          child: MrInheritedWidget(
            data: reverseTransitionDuration,
            child: child,
          ),
          transitionBuilder: (context, animation, secondaryAnimation, child) => child,
        ),
      }
    );
        
        
    class MrInheritedWidget extends InheritedWidget {
      const MrInheritedWidget(
        super.key,
        required super.child,
        required super.data,
      );
        
      final ValueNotifier<Duration> data;
        
      static void setDuration(Duration duration){
        context.dependOnInheritedWidgetOfExactType<MrInheritedWidget>.data.value = duration;
      }

      bool updateShouldNotify(covariant MrInheritedWidget oldWidget){
        return oldWidget.data.value != data.value;
      }
    }
Walrus
  • 19,801
  • 35
  • 121
  • 199

0 Answers0