From 2d230561aa86a33608d4ac0a8c814742af1d6f09 Mon Sep 17 00:00:00 2001 From: milad Date: Tue, 30 Jul 2024 14:08:01 +0300 Subject: [PATCH] fix a bug when returning custom roue builder --- README.md | 5 +- auto_route/example/lib/web_demo/web_main.dart | 3 +- .../lib/src/route/auto_route_config.dart | 4 +- auto_route/lib/src/route/route_type.dart | 20 ++++-- .../lib/src/router/auto_route_page.dart | 65 ++++++------------- 5 files changed, 43 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 06a745e8..734b83ff 100644 --- a/README.md +++ b/README.md @@ -1332,11 +1332,12 @@ CustomRoute( You can use your own custom route by passing a `CustomRouteBuilder` function to `CustomRoute' and implement the builder function the same way we did with the TransitionsBuilder function, the most important part here is passing the page argument to our custom route. +make sure you pass the return type to your custom route builder function. ```dart CustomRoute( page: CustomPage, - customRouteBuilder: (BuildContext context, Widget child, CustomPage page) { - return PageRouteBuilder( + customRouteBuilder: (BuildContext context, Widget child, AutoRoutePage page) { + return PageRouteBuilder( fullscreenDialog: page.fullscreenDialog, // this is important settings: page, diff --git a/auto_route/example/lib/web_demo/web_main.dart b/auto_route/example/lib/web_demo/web_main.dart index 9fdead16..562db97f 100644 --- a/auto_route/example/lib/web_demo/web_main.dart +++ b/auto_route/example/lib/web_demo/web_main.dart @@ -1,4 +1,5 @@ import 'package:example/mobile/router/router.dart'; +import 'package:example/web_demo/router/web_router.dart'; import 'package:flutter/material.dart'; //ignore_for_file: public_member_api_docs @@ -21,7 +22,7 @@ class AppState extends State { setState(() {}); }); - late final _router = AppRouter(); + late final _router = WebAppRouter( authService); @override Widget build(BuildContext context) { return MaterialApp.router( diff --git a/auto_route/lib/src/route/auto_route_config.dart b/auto_route/lib/src/route/auto_route_config.dart index 8c05298f..52179d1e 100644 --- a/auto_route/lib/src/route/auto_route_config.dart +++ b/auto_route/lib/src/route/auto_route_config.dart @@ -386,7 +386,7 @@ class CustomRoute extends AutoRoute { super.initial, super.allowSnapshotting = true, RouteTransitionsBuilder? transitionsBuilder, - CustomRouteBuilder? customRouteBuilder, + CustomRouteBuilder? customRouteBuilder, int? durationInMilliseconds, int? reverseDurationInMilliseconds, bool opaque = true, @@ -395,7 +395,7 @@ class CustomRoute extends AutoRoute { super.restorationId, Color? barrierColor, }) : super._( - type: CustomRouteType( + type: RouteType.custom( transitionsBuilder: transitionsBuilder, customRouteBuilder: customRouteBuilder, durationInMilliseconds: durationInMilliseconds, diff --git a/auto_route/lib/src/route/route_type.dart b/auto_route/lib/src/route/route_type.dart index f307028b..834651a2 100644 --- a/auto_route/lib/src/route/route_type.dart +++ b/auto_route/lib/src/route/route_type.dart @@ -3,7 +3,7 @@ import 'package:flutter/material.dart'; /// Signature for custom router builder used by /// [CustomRouteType] -typedef CustomRouteBuilder = Route Function( +typedef CustomRouteBuilder = Route Function( BuildContext context, Widget child, AutoRoutePage page, @@ -73,7 +73,7 @@ class AdaptiveRouteType extends RouteType { } /// Generates a route with user-defined transitions -class CustomRouteType extends RouteType { +class CustomRouteType extends RouteType { /// this builder function is passed to the transition builder /// function in [PageRouteBuilder] /// @@ -92,13 +92,23 @@ class CustomRouteType extends RouteType { /// /// I couldn't type this function from here but it should match /// typedef [CustomRouteBuilder] = Route Function(BuildContext context, CustomPage page); - /// you should only reference the function when passing it so - /// the generator can import it into the generated file /// /// this builder function accepts a BuildContext and a CustomPage /// that has all the other properties assigned to it /// so using them then is totally up to you. - final CustomRouteBuilder? customRouteBuilder; + /// + /// Make sure you pass the Return Type to the Route function + /// ex: + /// CustomRoute( + /// path: '/user/:userID', + /// page: UserRoute.page, + /// customRouteBuilder: (context, child, page) { + /// return PageRouteBuilder( + /// settings: page, + /// pageBuilder: (context, _, __) => child, + /// ); + /// }, + final CustomRouteBuilder? customRouteBuilder; /// route transition duration in milliseconds /// is passed to [PageRouteBuilder] diff --git a/auto_route/lib/src/router/auto_route_page.dart b/auto_route/lib/src/router/auto_route_page.dart index 0a1dce28..71a83529 100644 --- a/auto_route/lib/src/router/auto_route_page.dart +++ b/auto_route/lib/src/router/auto_route_page.dart @@ -38,9 +38,7 @@ class AutoRoutePage extends Page { /// The pop completer that's used in navigation actions /// e.g [StackRouter.push] /// it completes when the built route is popped - Future get popped => routeData.router.ignorePopCompleters - ? SynchronousFuture(null) - : _popCompleter.future; + Future get popped => routeData.router.ignorePopCompleters ? SynchronousFuture(null) : _popCompleter.future; /// The widget passed to the route Widget get child => _child; @@ -49,9 +47,7 @@ class AutoRoutePage extends Page { AutoRoutePage({ required this.routeData, required Widget child, - }) : _child = child is AutoRouteWrapper - ? WrappedRoute(child: child as AutoRouteWrapper) - : child, + }) : _child = child is AutoRouteWrapper ? WrappedRoute(child: child as AutoRouteWrapper) : child, super( restorationId: routeData.restorationId, name: routeData.name, @@ -87,14 +83,13 @@ class AutoRoutePage extends Page { } else if (type is CustomRouteType) { final result = buildPage(context); if (type.customRouteBuilder != null) { - return type.customRouteBuilder!(context, result, this) as Route; + return type.customRouteBuilder!.call(context, result, this); } return _CustomPageBasedPageRouteBuilder(page: this, routeType: type); } else if (type is AdaptiveRouteType) { if (kIsWeb) { return _NoAnimationPageRouteBuilder(page: this); - } else if ([TargetPlatform.macOS, TargetPlatform.iOS] - .contains(defaultTargetPlatform)) { + } else if ([TargetPlatform.macOS, TargetPlatform.iOS].contains(defaultTargetPlatform)) { return _PageBasedCupertinoPageRoute(page: this, title: title); } } @@ -110,8 +105,7 @@ class AutoRoutePage extends Page { } } -class _PageBasedMaterialPageRoute extends PageRoute - with MaterialRouteTransitionMixin { +class _PageBasedMaterialPageRoute extends PageRoute with MaterialRouteTransitionMixin { _PageBasedMaterialPageRoute({ required AutoRoutePage page, }) : super(settings: page); @@ -151,23 +145,17 @@ class _PageBasedMaterialPageRoute extends PageRoute String get debugLabel => '${super.debugLabel}(${_page.name})'; @override - bool canTransitionTo(TransitionRoute nextRoute) => - _canTransitionTo(nextRoute); + bool canTransitionTo(TransitionRoute nextRoute) => _canTransitionTo(nextRoute); } bool _canTransitionTo(TransitionRoute nextRoute) { - return (nextRoute is _CustomPageBasedPageRouteBuilder && - !nextRoute.fullscreenDialog || - nextRoute is MaterialRouteTransitionMixin && - !nextRoute.fullscreenDialog) || - (nextRoute is _NoAnimationPageRouteTransitionMixin && - !nextRoute.fullscreenDialog) || - (nextRoute is CupertinoRouteTransitionMixin && - !nextRoute.fullscreenDialog); + return (nextRoute is _CustomPageBasedPageRouteBuilder && !nextRoute.fullscreenDialog || + nextRoute is MaterialRouteTransitionMixin && !nextRoute.fullscreenDialog) || + (nextRoute is _NoAnimationPageRouteTransitionMixin && !nextRoute.fullscreenDialog) || + (nextRoute is CupertinoRouteTransitionMixin && !nextRoute.fullscreenDialog); } -class _CustomPageBasedPageRouteBuilder extends PageRoute - with _CustomPageRouteTransitionMixin { +class _CustomPageBasedPageRouteBuilder extends PageRoute with _CustomPageRouteTransitionMixin { _CustomPageBasedPageRouteBuilder({ required AutoRoutePage page, required this.routeType, @@ -192,12 +180,10 @@ class _CustomPageBasedPageRouteBuilder extends PageRoute String get debugLabel => '${super.debugLabel}(${_page.name})'; @override - bool canTransitionTo(TransitionRoute nextRoute) => - _canTransitionTo(nextRoute); + bool canTransitionTo(TransitionRoute nextRoute) => _canTransitionTo(nextRoute); } -class _NoAnimationPageRouteBuilder extends PageRoute - with _NoAnimationPageRouteTransitionMixin { +class _NoAnimationPageRouteBuilder extends PageRoute with _NoAnimationPageRouteTransitionMixin { _NoAnimationPageRouteBuilder({ required AutoRoutePage page, }) : super(settings: page); @@ -221,8 +207,7 @@ class _NoAnimationPageRouteBuilder extends PageRoute Duration get transitionDuration => Duration.zero; @override - bool canTransitionTo(TransitionRoute nextRoute) => - _canTransitionTo(nextRoute); + bool canTransitionTo(TransitionRoute nextRoute) => _canTransitionTo(nextRoute); } mixin _NoAnimationPageRouteTransitionMixin on PageRoute { @@ -244,8 +229,7 @@ mixin _NoAnimationPageRouteTransitionMixin on PageRoute { bool get opaque => _page.opaque; @override - bool canTransitionTo(TransitionRoute nextRoute) => - _canTransitionTo(nextRoute); + bool canTransitionTo(TransitionRoute nextRoute) => _canTransitionTo(nextRoute); @override Widget buildPage( @@ -293,8 +277,7 @@ mixin _CustomPageRouteTransitionMixin on PageRoute { bool get opaque => routeType.opaque; @override - bool canTransitionTo(TransitionRoute nextRoute) => - _canTransitionTo(nextRoute); + bool canTransitionTo(TransitionRoute nextRoute) => _canTransitionTo(nextRoute); @override Widget buildPage( @@ -310,26 +293,20 @@ mixin _CustomPageRouteTransitionMixin on PageRoute { } Widget _defaultTransitionsBuilder( - BuildContext context, - Animation animation, - Animation secondaryAnimation, - Widget child) { + BuildContext context, Animation animation, Animation secondaryAnimation, Widget child) { return child; } @override - Widget buildTransitions(BuildContext context, Animation animation, - Animation secondaryAnimation, Widget child) { - final transitionsBuilder = - routeType.transitionsBuilder ?? _defaultTransitionsBuilder; + Widget buildTransitions( + BuildContext context, Animation animation, Animation secondaryAnimation, Widget child) { + final transitionsBuilder = routeType.transitionsBuilder ?? _defaultTransitionsBuilder; return transitionsBuilder(context, animation, secondaryAnimation, child); } } class _PageBasedCupertinoPageRoute extends PageRoute - with - CupertinoRouteTransitionMixin, - CupertinoRouteTransitionOverrideMixin { + with CupertinoRouteTransitionMixin, CupertinoRouteTransitionOverrideMixin { _PageBasedCupertinoPageRoute({ required AutoRoutePage page, this.title,