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

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

示例代码

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

void main() => runApp(MyApp());

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Material App',
      initialRoute: "page1",
      routes: {"page1": (_) => Page1(), "page2": (_) => Page2()},
    );
  }
}

class Page1 extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("页面1"),
        backgroundColor: Colors.transparent,
      ),
      backgroundColor: Colors.blue,
      body: Center(
        child: MaterialButton(
          child: Text("前往页面2"),
          color: Colors.white,
          onPressed: () {
            // 使用默认路由跳转
            // Navigator.push(context, MaterialPageRoute(builder: (_) => Page2()));
            // Navigator.pushNamed(context, "page2");

            // 使用自定义过渡动画
            RouteTransitions(
              context: context,
              child: Page2(),
              animation: AnimationType.fadeIn,
              duration: Duration(milliseconds: 300),
              replacement: true,
            );
          },
        ),
      ),
    );
  }
}

class Page2 extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("页面2"),
        backgroundColor: Colors.transparent,
      ),
      backgroundColor: Colors.blueGrey,
      body: Center(
        child: Text('页面2'),
      ),
    );
  }
}

使用说明

custom_route_transition_fa 插件可以帮助你在 Flutter 中实现自定义的路由过渡动画。以下是如何在你的项目中使用该插件的步骤:

步骤 1: 添加依赖

首先,在 pubspec.yaml 文件中添加 custom_route_transition_fa 依赖:

dependencies:
  flutter:
    sdk: flutter
  custom_route_transition_fa: ^1.0.0

然后运行 flutter pub get 命令来安装依赖。

步骤 2: 创建自定义过渡动画

在上面的示例代码中,我们展示了如何使用 RouteTransitions 类来创建一个简单的过渡动画。以下是关键代码片段的详细解释:

RouteTransitions(
  context: context,
  child: Page2(),
  animation: AnimationType.fadeIn,
  duration: Duration(milliseconds: 300),
  replacement: true,
);
  • context: 当前的 BuildContext
  • child: 要导航到的目标页面。
  • animation: 动画类型,例如 AnimationType.fadeIn
  • duration: 动画持续时间。
  • replacement: 是否替换当前页面(如果为 true,则使用 Navigator.pushReplacement 方法)。

步骤 3: 在页面之间导航

在上面的示例中,我们在 Page1 页面上添加了一个按钮,当点击时会触发自定义的过渡动画来跳转到 Page2 页面。

完整示例代码

下面是完整的示例代码,你可以将其复制到你的项目中并运行:

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

void main() => runApp(MyApp());

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Material App',
      initialRoute: "page1",
      routes: {"page1": (_) => Page1(), "page2": (_) => Page2()},
    );
  }
}

class Page1 extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("页面1"),
        backgroundColor: Colors.transparent,
      ),
      backgroundColor: Colors.blue,
      body: Center(
        child: MaterialButton(
          child: Text("前往页面2"),
          color: Colors.white,
          onPressed: () {
            RouteTransitions(
              context: context,
              child: Page2(),
              animation: AnimationType.fadeIn,
              duration: Duration(milliseconds: 300),
              replacement: true,
            );
          },
        ),
      ),
    );
  }
}

class Page2 extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("页面2"),
        backgroundColor: Colors.transparent,
      ),
      backgroundColor: Colors.blueGrey,
      body: Center(
        child: Text('页面2'),
      ),
    );
  }
}

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

1 回复

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


custom_route_transition_fa 是一个为 Flutter 提供自定义路由过渡动画的插件。通过这个插件,你可以轻松地为你的应用程序添加各种页面切换时的过渡效果。以下是如何使用 custom_route_transition_fa 插件的步骤:

1. 添加依赖

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

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

然后运行 flutter pub get 来安装依赖。

2. 导入插件

在你的 Dart 文件中导入 custom_route_transition_fa

import 'package:custom_route_transition_fa/custom_route_transition_fa.dart';

3. 使用自定义路由过渡动画

你可以通过 CustomRouteTransition 来定义自定义的过渡动画。以下是一个简单的示例,展示了如何在页面跳转时使用自定义过渡动画:

import 'package:flutter/material.dart';
import 'package:custom_route_transition_fa/custom_route_transition_fa.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(
          onPressed: () {
            Navigator.push(
              context,
              CustomRouteTransition(
                child: SecondPage(),
                transitionType: TransitionType.slideRight, // 选择过渡动画类型
                duration: Duration(milliseconds: 500), // 设置动画持续时间
              ),
            );
          },
          child: Text('Go to Second Page'),
        ),
      ),
    );
  }
}

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

4. 过渡动画类型

CustomRouteTransition 提供了多种内置的过渡动画类型,你可以通过 transitionType 参数来选择:

  • TransitionType.fade: 淡入淡出效果
  • TransitionType.slideRight: 从右向左滑动
  • TransitionType.slideLeft: 从左向右滑动
  • TransitionType.slideUp: 从下向上滑动
  • TransitionType.slideDown: 从上向下滑动
  • TransitionType.scale: 缩放效果
  • TransitionType.rotate: 旋转效果

你也可以通过 CustomRouteTransition.custom 来创建自定义的过渡动画。

5. 自定义过渡动画

如果你想要创建自定义的过渡动画,可以使用 CustomRouteTransition.custom 构造函数,并传递一个 PageRouteBuilder

Navigator.push(
  context,
  CustomRouteTransition.custom(
    child: SecondPage(),
    pageRouteBuilder: (context, animation, secondaryAnimation, child) {
      return FadeTransition(
        opacity: animation,
        child: child,
      );
    },
    duration: Duration(milliseconds: 500),
  ),
);
回到顶部