Flutter自定义路由过渡动画插件custom_route_transition_jg的使用

Flutter自定义路由过渡动画插件custom_route_transition_jg的使用

路由过渡

此插件帮助实现路由之间的过渡动画。

示例代码

示例:main.dart

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

void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      initialRoute: 'page1',
      routes: {
        'page1': (context) => const Page1(),
        'page2': (context) => const Page2(),
      },
    );
  }
}

class Page1 extends StatelessWidget {
  const Page1({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Page 1'),
        backgroundColor: Colors.transparent,
      ),
      backgroundColor: Colors.blue,
      body: Center(
        child: MaterialButton(
          child: const Text('Go to page 2'),
          onPressed: () {
            // 使用自定义的路由过渡动画
            Navigator.of(context).push(
              RouteTransitions(
                context: context, 
                child: const Page2(),
                animation: AnimationType.fadeIn,
                duration: const Duration(milliseconds: 300),
                replacement: true
              )
            );
          }
        )
      ),
    );
  }
}

class Page2 extends StatelessWidget {
  const Page2({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Page 2'),
        backgroundColor: Colors.transparent,
      ),
      backgroundColor: Colors.blueGrey,
      body: const Center(
        child: Text('Page2'),
      ),
    );
  }
}

说明

  1. 导入库

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

    导入必要的包以支持 Flutter 和 custom_route_transition_jg 插件。

  2. 创建主应用类

    class MainApp extends StatelessWidget {
      const MainApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          initialRoute: 'page1',
          routes: {
            'page1': (context) => const Page1(),
            'page2': (context) => const Page2(),
          },
        );
      }
    }
    

    创建一个基本的 Flutter 应用,并设置初始路由和页面。

  3. 创建第一页

    class Page1 extends StatelessWidget {
      const Page1({super.key});
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Page 1'),
            backgroundColor: Colors.transparent,
          ),
          backgroundColor: Colors.blue,
          body: Center(
            child: MaterialButton(
              child: const Text('Go to page 2'),
              onPressed: () {
                // 使用自定义的路由过渡动画
                Navigator.of(context).push(
                  RouteTransitions(
                    context: context, 
                    child: const Page2(),
                    animation: AnimationType.fadeIn,
                    duration: const Duration(milliseconds: 300),
                    replacement: true
                  )
                );
              }
            )
          ),
        );
      }
    }
    

    在第一页中添加一个按钮,点击后触发自定义的路由过渡动画,从第一页跳转到第二页。

  4. 创建第二页

    class Page2 extends StatelessWidget {
      const Page2({super.key});
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Page 2'),
            backgroundColor: Colors.transparent,
          ),
          backgroundColor: Colors.blueGrey,
          body: const Center(
            child: Text('Page2'),
          ),
        );
      }
    }
    

更多关于Flutter自定义路由过渡动画插件custom_route_transition_jg的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter自定义路由过渡动画插件custom_route_transition_jg的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


custom_route_transition_jg 是一个 Flutter 插件,用于自定义路由过渡动画。通过这个插件,你可以轻松地为 Flutter 应用程序中的页面切换添加自定义的过渡效果。以下是使用 custom_route_transition_jg 插件的步骤和示例代码。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 custom_route_transition_jg 插件的依赖。

dependencies:
  flutter:
    sdk: flutter
  custom_route_transition_jg: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来获取依赖。

2. 使用插件

在你的 Flutter 项目中,你可以使用 CustomRouteTransition 来定义自定义的路由过渡动画。

基本用法

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Custom Route Transition Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Page'),
      ),
      body: Center(
        child: ElevatedButton(
          child: Text('Go to Next Page'),
          onPressed: () {
            Navigator.push(
              context,
              CustomRouteTransition(
                builder: (context) => NextPage(),
                transitionType: TransitionType.slideRight, // 自定义过渡动画类型
                duration: Duration(milliseconds: 500), // 过渡动画的持续时间
              ),
            );
          },
        ),
      ),
    );
  }
}

class NextPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Next Page'),
      ),
      body: Center(
        child: ElevatedButton(
          child: Text('Go Back'),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
    );
  }
}

自定义过渡动画类型

CustomRouteTransition 提供了多种内置的过渡动画类型,你可以通过 transitionType 参数来指定。以下是一些常用的过渡动画类型:

  • TransitionType.slideRight: 从右向左滑动
  • TransitionType.slideLeft: 从左向右滑动
  • TransitionType.slideTop: 从上向下滑动
  • TransitionType.slideBottom: 从下向上滑动
  • TransitionType.fade: 淡入淡出
  • TransitionType.scale: 缩放效果

你也可以通过 CustomRouteTransitioncustomTransition 参数来自定义过渡动画。

3. 自定义过渡动画

如果你想创建自己的过渡动画,可以使用 PageRouteBuilder 并结合 CustomRouteTransitioncustomTransition 参数。

Navigator.push(
  context,
  CustomRouteTransition(
    builder: (context) => NextPage(),
    customTransition: (context, animation, secondaryAnimation, child) {
      return ScaleTransition(
        scale: Tween<double>(begin: 0.0, end: 1.0).animate(
          CurvedAnimation(
            parent: animation,
            curve: Curves.easeInOut,
          ),
        ),
        child: child,
      );
    },
    duration: Duration(milliseconds: 500),
  ),
);
回到顶部