Skip to content

Commit

Permalink
fix a bug when returning custom roue builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Milad-Akarie committed Jul 30, 2024
1 parent 3fbb011 commit 2d23056
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 54 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> to your custom route builder function.
```dart
CustomRoute(
page: CustomPage,
customRouteBuilder: (BuildContext context, Widget child, CustomPage<T> page) {
return PageRouteBuilder(
customRouteBuilder: <T>(BuildContext context, Widget child, AutoRoutePage<T> page) {
return PageRouteBuilder<T>(
fullscreenDialog: page.fullscreenDialog,
// this is important
settings: page,
Expand Down
3 changes: 2 additions & 1 deletion auto_route/example/lib/web_demo/web_main.dart
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,7 +22,7 @@ class AppState extends State<App> {
setState(() {});
});

late final _router = AppRouter();
late final _router = WebAppRouter( authService);
@override
Widget build(BuildContext context) {
return MaterialApp.router(
Expand Down
4 changes: 2 additions & 2 deletions auto_route/lib/src/route/auto_route_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ class CustomRoute<R> extends AutoRoute {
super.initial,
super.allowSnapshotting = true,
RouteTransitionsBuilder? transitionsBuilder,
CustomRouteBuilder<R>? customRouteBuilder,
CustomRouteBuilder? customRouteBuilder,
int? durationInMilliseconds,
int? reverseDurationInMilliseconds,
bool opaque = true,
Expand All @@ -395,7 +395,7 @@ class CustomRoute<R> extends AutoRoute {
super.restorationId,
Color? barrierColor,
}) : super._(
type: CustomRouteType<R>(
type: RouteType.custom(
transitionsBuilder: transitionsBuilder,
customRouteBuilder: customRouteBuilder,
durationInMilliseconds: durationInMilliseconds,
Expand Down
20 changes: 15 additions & 5 deletions auto_route/lib/src/route/route_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter/material.dart';

/// Signature for custom router builder used by
/// [CustomRouteType]
typedef CustomRouteBuilder<T> = Route<T> Function(
typedef CustomRouteBuilder = Route<T> Function<T>(
BuildContext context,
Widget child,
AutoRoutePage<T> page,
Expand Down Expand Up @@ -73,7 +73,7 @@ class AdaptiveRouteType extends RouteType {
}

/// Generates a route with user-defined transitions
class CustomRouteType<R> extends RouteType {
class CustomRouteType extends RouteType {
/// this builder function is passed to the transition builder
/// function in [PageRouteBuilder]
///
Expand All @@ -92,13 +92,23 @@ class CustomRouteType<R> 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<R>? customRouteBuilder;
///
/// Make sure you pass the Return Type <T> to the Route<T> function
/// ex:
/// CustomRoute(
/// path: '/user/:userID',
/// page: UserRoute.page,
/// customRouteBuilder: <T>(context, child, page) {
/// return PageRouteBuilder<T>(
/// settings: page,
/// pageBuilder: (context, _, __) => child,
/// );
/// },
final CustomRouteBuilder? customRouteBuilder;

/// route transition duration in milliseconds
/// is passed to [PageRouteBuilder]
Expand Down
65 changes: 21 additions & 44 deletions auto_route/lib/src/router/auto_route_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ class AutoRoutePage<T> extends Page<T> {
/// The pop completer that's used in navigation actions
/// e.g [StackRouter.push]
/// it completes when the built route is popped
Future<T?> get popped => routeData.router.ignorePopCompleters
? SynchronousFuture(null)
: _popCompleter.future;
Future<T?> get popped => routeData.router.ignorePopCompleters ? SynchronousFuture(null) : _popCompleter.future;

/// The widget passed to the route
Widget get child => _child;
Expand All @@ -49,9 +47,7 @@ class AutoRoutePage<T> extends Page<T> {
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,
Expand Down Expand Up @@ -87,14 +83,13 @@ class AutoRoutePage<T> extends Page<T> {
} else if (type is CustomRouteType) {
final result = buildPage(context);
if (type.customRouteBuilder != null) {
return type.customRouteBuilder!(context, result, this) as Route<T>;
return type.customRouteBuilder!.call<T>(context, result, this);
}
return _CustomPageBasedPageRouteBuilder<T>(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<T>(page: this, title: title);
}
}
Expand All @@ -110,8 +105,7 @@ class AutoRoutePage<T> extends Page<T> {
}
}

class _PageBasedMaterialPageRoute<T> extends PageRoute<T>
with MaterialRouteTransitionMixin<T> {
class _PageBasedMaterialPageRoute<T> extends PageRoute<T> with MaterialRouteTransitionMixin<T> {
_PageBasedMaterialPageRoute({
required AutoRoutePage page,
}) : super(settings: page);
Expand Down Expand Up @@ -151,23 +145,17 @@ class _PageBasedMaterialPageRoute<T> extends PageRoute<T>
String get debugLabel => '${super.debugLabel}(${_page.name})';

@override
bool canTransitionTo(TransitionRoute nextRoute) =>
_canTransitionTo(nextRoute);
bool canTransitionTo(TransitionRoute nextRoute) => _canTransitionTo(nextRoute);
}

bool _canTransitionTo(TransitionRoute<dynamic> 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<T> extends PageRoute<T>
with _CustomPageRouteTransitionMixin<T> {
class _CustomPageBasedPageRouteBuilder<T> extends PageRoute<T> with _CustomPageRouteTransitionMixin<T> {
_CustomPageBasedPageRouteBuilder({
required AutoRoutePage page,
required this.routeType,
Expand All @@ -192,12 +180,10 @@ class _CustomPageBasedPageRouteBuilder<T> extends PageRoute<T>
String get debugLabel => '${super.debugLabel}(${_page.name})';

@override
bool canTransitionTo(TransitionRoute nextRoute) =>
_canTransitionTo(nextRoute);
bool canTransitionTo(TransitionRoute nextRoute) => _canTransitionTo(nextRoute);
}

class _NoAnimationPageRouteBuilder<T> extends PageRoute<T>
with _NoAnimationPageRouteTransitionMixin<T> {
class _NoAnimationPageRouteBuilder<T> extends PageRoute<T> with _NoAnimationPageRouteTransitionMixin<T> {
_NoAnimationPageRouteBuilder({
required AutoRoutePage page,
}) : super(settings: page);
Expand All @@ -221,8 +207,7 @@ class _NoAnimationPageRouteBuilder<T> extends PageRoute<T>
Duration get transitionDuration => Duration.zero;

@override
bool canTransitionTo(TransitionRoute nextRoute) =>
_canTransitionTo(nextRoute);
bool canTransitionTo(TransitionRoute nextRoute) => _canTransitionTo(nextRoute);
}

mixin _NoAnimationPageRouteTransitionMixin<T> on PageRoute<T> {
Expand All @@ -244,8 +229,7 @@ mixin _NoAnimationPageRouteTransitionMixin<T> on PageRoute<T> {
bool get opaque => _page.opaque;

@override
bool canTransitionTo(TransitionRoute nextRoute) =>
_canTransitionTo(nextRoute);
bool canTransitionTo(TransitionRoute nextRoute) => _canTransitionTo(nextRoute);

@override
Widget buildPage(
Expand Down Expand Up @@ -293,8 +277,7 @@ mixin _CustomPageRouteTransitionMixin<T> on PageRoute<T> {
bool get opaque => routeType.opaque;

@override
bool canTransitionTo(TransitionRoute nextRoute) =>
_canTransitionTo(nextRoute);
bool canTransitionTo(TransitionRoute nextRoute) => _canTransitionTo(nextRoute);

@override
Widget buildPage(
Expand All @@ -310,26 +293,20 @@ mixin _CustomPageRouteTransitionMixin<T> on PageRoute<T> {
}

Widget _defaultTransitionsBuilder(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child) {
BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
return child;
}

@override
Widget buildTransitions(BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation, Widget child) {
final transitionsBuilder =
routeType.transitionsBuilder ?? _defaultTransitionsBuilder;
Widget buildTransitions(
BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
final transitionsBuilder = routeType.transitionsBuilder ?? _defaultTransitionsBuilder;
return transitionsBuilder(context, animation, secondaryAnimation, child);
}
}

class _PageBasedCupertinoPageRoute<T> extends PageRoute<T>
with
CupertinoRouteTransitionMixin<T>,
CupertinoRouteTransitionOverrideMixin<T> {
with CupertinoRouteTransitionMixin<T>, CupertinoRouteTransitionOverrideMixin<T> {
_PageBasedCupertinoPageRoute({
required AutoRoutePage<T> page,
this.title,
Expand Down

0 comments on commit 2d23056

Please sign in to comment.