page_transition

repository·master·Indexed 19 days ago

https://github.com/kalismeras61/flutter_page_transition

A Flutter package providing customizable page transitions, including Material 3 shared axis transitions and iOS-style swipe-back gestures. It offers a concise API via BuildContext extensions (such as pushTransition and pushNamedTransition) and supports integration with GoRouter and AutoRoute. The package extends PageRouteBuilder to provide various animation styles defined in the PageTransitionType enum.

Tokens
3.4K
Snippets
12
Records
13
Agent score
67%

What's inside page_transition

  1. Use Shared Axis transitions (Material 3 style)

    master

    The package supports Material Design 3 style shared axis transitions:

    • PageTransitionType.sharedAxisHorizontal
    • PageTransitionType.sharedAxisVertical
    • PageTransitionType.sharedAxisScale
    // Horizontal shared axis
    context.pushTransition(
      type: PageTransitionType.sharedAxisHorizontal,
      child: DetailScreen(),
      duration: Duration(milliseconds: 400),
      curve: Curves.easeInOut,
    );
    
    // Vertical shared axis
    context.pushTransition(
      type: PageTransitionType.sharedAxisVertical,
      child: DetailScreen(),
    );
    
    // Scale shared axis
    context.pushTransition(
      type: PageTransitionType.sharedAxisScale,
      child: DetailScreen(),
    );
  2. Integrate with AutoRoute

    master

    To use PageTransition with AutoRoute, define your routes using CustomRoute and implement the transitionsBuilder. You must call .buildTransitions() on the PageTransition instance within the builder to correctly apply the animation.

    @MaterialAutoRouter(
      replaceInRouteName: 'Page,Route',
      routes: <AutoRoute>[
        AutoRoute(
          page: HomePage,
          initial: true,
        ),
        CustomRoute(
          page: DetailsPage,
          path: '/details/:id',
          transitionsBuilder: (context, animation, secondaryAnimation, child) {
            return PageTransition(
              type: PageTransitionType.sharedAxisHorizontal,
              child: child,
            ).buildTransitions(
              context,
              animation,
              secondaryAnimation,
              child,
            );
          },
        ),
        CustomRoute(
          page: ProfilePage,
          path: '/profile',
          transitionsBuilder: (context, animation, secondaryAnimation, child) {
            return PageTransition(
              type: PageTransitionType.sharedAxisVertical,
              child: child,
            ).buildTransitions(
              context,
              animation,
              secondaryAnimation,
              child,
            );
          },
        ),
      ],
    )
    class $AppRouter {}
    
    // Navigate using AutoRoute
    context.router.push(DetailsRoute(id: 123));
  3. Use PageTransition extensions (Recommended)

    master

    The recommended way to use this package is via BuildContext extensions. These methods provide a concise API for common navigation tasks with transitions.

    Available extension methods:

    • context.pushTransition(...): Pushes a new route with a transition.
    • context.pushReplacementTransition(...): Replaces the current route with a transition.
    • context.pushAndRemoveUntilTransition(...): Pushes a route and removes previous routes based on a predicate.
    • context.pushNamedTransition(...): Navigates to a named route with a transition.

    You can provide the destination widget via child or use the childBuilder pattern for lazy construction.

    // Simple transition
    context.pushTransition(
      type: PageTransitionType.fade,
      child: DetailScreen(),
    );
    
    // Using builder pattern
    context.pushTransition(
      type: PageTransitionType.fade,
      childBuilder: (context) => DetailScreen(
        id: someId,
        title: someTitle,
      ),
    );
    
    // Push replacement
    context.pushReplacementTransition(
      type: PageTransitionType.rightToLeft,
      child: DetailScreen(),
    );
    
    // Push and remove until
    context.pushAndRemoveUntilTransition(
      type: PageTransitionType.fade,
      child: HomePage(),
      predicate: (route) => false,
    );
    
    // Named route with transition
    context.pushNamedTransition(
      routeName: '/detail',
      type: PageTransitionType.fade,
      arguments: {'id': 1},
    );
  4. Configure iOS Swipe Back gesture

    master

    You can enable iOS-style swipe back gestures by setting isIos: true in the transition configuration.

    Note: This feature only works with PageTransitionType.rightToLeft and PageTransitionType.fade transitions.

    context.pushTransition(
      type: PageTransitionType.rightToLeft,
      child: DetailScreen(),
      isIos: true,
    );
  5. Integrate with GoRouter

    master

    When using GoRouter, use the pageBuilder property of a GoRoute to return a PageTransition widget. For joined transitions (like rightToLeftJoined), you must provide childCurrent using context.currentRoute to allow the transition to animate between the existing and new route.

    final router = GoRouter(
      routes: [
        GoRoute(
          path: '/details/:id',
          pageBuilder: (context, state) {
            return PageTransition(
              type: PageTransitionType.rightToLeftJoined,
              childCurrent: context.currentRoute,
              child: DetailsPage(id: state.params['id']),
              settings: RouteSettings(name: state.location),
            );
          },
        ),
      ],
    );
  6. Customize iOS Launch Screen Assets

    master

    To change the launch screen image for the iOS version of your Flutter app, you can either replace the image files directly in the directory or use Xcode.

    Method 1: Direct File Replacement Replace the existing image files within the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory with your own assets.

    Method 2: Using Xcode (Recommended for visual management)

    1. Open your Flutter project's iOS workspace by running open ios/Runner.xcworkspace in your terminal.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog to replace the launch images.
    open ios/Runner.xcworkspace
  7. Optimize transition performance

    master

    To ensure smooth animations and high performance:

    1. Keep durations short (200-300ms).
    2. Use simple curves like Curves.easeOut.
    3. Use childBuilder for lazy widget construction to avoid heavy upfront builds.
    4. Wrap heavy widgets in a RepaintBoundary to prevent unnecessary repaints during the transition.
    context.pushTransition(
      type: PageTransitionType.fade,
      duration: Duration(milliseconds: 200),
      curve: Curves.easeOut,
      child: RepaintBoundary(
        child: HeavyWidget(),
      ),
    );
  8. Use Traditional Navigator usage

    master

    If you prefer not to use extensions, you can use the standard Navigator.push method by passing a PageTransition widget.

    Navigator.push(
      context,
      PageTransition(
        type: PageTransitionType.fade,
        child: DetailScreen(),
      ),
    );
    
    // Or using builder pattern
    Navigator.push(
      context,
      PageTransition(
        type: PageTransitionType.fade,
        childBuilder: (context) => DetailScreen(id: someId),
      ),
    );
  9. Use PageTransition for custom route animations

    master

    The PageTransition class extends PageRouteBuilder to provide various pre-defined animation types for navigating between routes in Flutter. You can provide the next page either as a direct child widget or via a childBuilder function.

    Constraints:

    • You must provide either child or childBuilder, but not both.
    • If inheritTheme is set to true, you must provide a ctx (BuildContext).
    • Certain transition types require additional parameters (e.g., scale, rotate, and size require alignment; joined or pop types require childCurrent).
    // Example using child
    PageTransition(
      type: PageTransitionType.rightToLeft,
      child: DetailPage(),
    )
    
    // Example using builder
    PageTransition(
      type: PageTransitionType.rightToLeft,
      builder: (context) => DetailPage(),
    )
  10. Configure PageTransition parameters

    master

    When instantiating PageTransition, you can customize the animation behavior using the following properties:

    PropertyTypeDescription
    typePageTransitionTypeRequired. The animation style to use.
    childWidget?The widget to transition to.
    childBuilderChildBuilder?A function to build the child widget.
    childCurrentWidget?Required for Joined or Pop transition types.
    reverseTypePageTransitionType?The transition type to use when popping the route.
    durationDurationDuration of the transition (default: 200ms).
    reverseDurationDuration?Duration of the pop transition (default: 200ms).
    curveCurveThe animation curve (default: Curves.linear).
    alignmentAlignment?Required for scale, rotate, and size types.
    inheritThemeboolIf true, uses the theme's pageTransitionsTheme. Requires ctx.
    ctxBuildContext?Required if inheritTheme is true.
    isIosboolIf true, applies matchingBuilder (Cupertino style) to certain transitions.
    fullscreenDialogboolWhether the route is a fullscreen dialog.
    opaqueboolWhether the route is opaque.
    maintainStateDatabool?Maps to maintainState in PageRouteBuilder.