8

as the docs from go_router describe, it is easy to set pageBuilder-Transitions for single pages. However, I want to set the default PageTransition for all pages.

How do I set the default page transition with/for go_router in Flutter?

Single Page:


  // this is the proposed method to do it for single pages
  // how can i apply it to all pages without writing the same code?
  GoRoute(
      path: '/login',
      builder: (context, state) => const LoginScreen(),
      pageBuilder: (context, state) => CustomTransitionPage<void>(
        key: state.pageKey,
        child: const LoginScreen(),
        transitionsBuilder: (context, animation, secondaryAnimation, child) =>
            FadeTransition(opacity: animation, child: child),
      ),
    ),

Best regards

Marwin Lebensky
  • 270
  • 1
  • 2
  • 16

5 Answers5

23

The go_router package does not support this at the moment, but to reduce code duplication, you can create a helper function that would apply the custom transition for your route, like:

CustomTransitionPage buildPageWithDefaultTransition<T>({
  required BuildContext context, 
  required GoRouterState state, 
  required Widget child,
}) {
  return CustomTransitionPage<T>(
    key: state.pageKey,
    child: child,
    transitionsBuilder: (context, animation, secondaryAnimation, child) => 
      FadeTransition(opacity: animation, child: child),
  );
}

<...>

GoRoute(
  path: '/login',
  builder: (context, state) => const LoginScreen(),
  pageBuilder: (context, state) => buildPageWithDefaultTransition<void>(
    context: context, 
    state: state, 
    child: LoginScreen(),
  ),
),
Pedro Luz
  • 2,694
  • 4
  • 44
  • 54
mkobuolys
  • 4,499
  • 1
  • 11
  • 27
  • It works. Upvoted. I wonder in an invocation of `transitionBuilder`, `animation`, and `secondaryAnimation` objects come from where? – Yuriy N. Mar 27 '23 at 14:44
7

The more correct way is inheritance from CustomTransitionPage.

class WebPage extends CustomTransitionPage {
  WebPage({
    LocalKey key,
    ... // other properties taken from `MaterialPage`
    required Widget child
  }) : super(
         key: key,
         transitionBuilder: (...) {
           return FadeTransition(...);
         }
         child: child, // Here you may also wrap this child with some common designed widget
       );
}

Then

GoRoute(
  path: '/login',
  pageBuilder: (context, state) => WebPage(
    key: state.pageKey,
    child: const LoginScreen(),
  ),
),
BambinoUA
  • 6,126
  • 5
  • 35
  • 51
  • Look at the source of NoTransitionPage class in the go_router package (custom_transition_page.dart) for a full example of the implementation. – IcyIcicle Oct 22 '22 at 16:39
  • 1
    Correct answer but not runnable. Check [this answer](https://stackoverflow.com/a/74136322/3935156) for a compilable and working example of this answer. I also think `GoRoute.builder` needs to be removed. – BeniaminoBaggins Jan 02 '23 at 23:33
3

Expanding on @mkobuolys answer, the boilerplate can be reduced further, by returning function that creates page builder, i.e.:

CustomTransitionPage buildPageWithDefaultTransition<T>({
  required BuildContext context,
  required GoRouterState state,
  required Widget child,
}) {
  return CustomTransitionPage<T>(
    key: state.pageKey,
    child: child,
    transitionsBuilder: (context, animation, secondaryAnimation, child) =>
        FadeTransition(opacity: animation, child: child),
  );
}

Page<dynamic> Function(BuildContext, GoRouterState) defaultPageBuilder<T>(
        Widget child) =>
    (BuildContext context, GoRouterState state) {
      return buildPageWithDefaultTransition<T>(
        context: context,
        state: state,
        child: child,
      );
    };

and the router would look like:

final _router = GoRouter(
  routes: [
    GoRoute(
      path: '/a',
      builder: (context, state) => const PageA(),
      pageBuilder: defaultPageBuilder(const PageA()),
    ),
    GoRoute(
      path: '/b',
      builder: (context, state) => const PageB(),
      pageBuilder: defaultPageBuilder(const PageB()),
    ),
  ],
);
2

Below is a code for the transition factory (heavily based on the answer of @mkobuolys). It adds 3 more transition effects: scale, rotation, and size.

Rotation for my taste makes too many turns, size looks like there is almost no transition effect, but scale is kinda fine.

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

class RouterTransitionFactory {
 static CustomTransitionPage getTransitionPage(
  {required BuildContext context,
  required GoRouterState state,
  required Widget child,
  required String type}) {
 return CustomTransitionPage(
    key: state.pageKey,
    child: child,
    transitionsBuilder: (context, animation, secondaryAnimation, child) {
      switch (type) {
        case 'fade':
          return FadeTransition(opacity: animation, child: child);
        case 'rotation':
          return RotationTransition(turns: animation, child: child);
        case 'size':
          return SizeTransition(sizeFactor: animation, child: child);
        case 'scale':
          return ScaleTransition(scale: animation, child: child);
        default:
          return FadeTransition(opacity: animation, child: child);
      }
    });
  }
}

Should be called like this:

GoRoute(
    path: '/settings',
    builder: (context, state) => const Settings(), //StatelessWidget
    pageBuilder: (context, state) => RouterTransitionFactory.getTransitionPage(
      context: context,
      state: state,
      child: const Settings(), 
      type: 'scale', // fade|rotation|scale|size
    ),
  ),
Yuriy N.
  • 4,936
  • 2
  • 38
  • 31
1

Why not use like this its way easier than the accepted answer

GoRoute defaultTransitionGoRoute({
  required String path,
  required Widget Function(BuildContext, GoRouterState) pageBuilder,
}) {
  return GoRoute(
    path: path,
    pageBuilder: (context, state) => CustomTransitionPage<void>(
      key: state.pageKey,
      transitionDuration: const Duration(milliseconds: 300),
      child: pageBuilder(context, state),
      transitionsBuilder: (context, animation, secondaryAnimation, child) {
        return FadeTransition(
          opacity: CurveTween(curve: Curves.easeIn).animate(animation),
          child: child,
        );
      },
    ),
  );
}

then use like

GoRoute(
      path: ScreenPaths.favorites,
      builder: (context, state) => FavorateScreen(),
    ),
    defaultTransitionGoRoute(
      path: ScreenPaths.viewEmail,
      pageBuilder: (context, state) {
        final extra = state.extra;
        return ViewEmailScreen(
          email: extra as HiveEmail,
        );
      },
    ),
nhCoder
  • 451
  • 5
  • 11