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

发布于 1周前 作者 nodeper 来自 Flutter

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

Route Transition(路由过渡)

此插件帮助实现页面之间的路由过渡效果。

示例代码

example/main.dart

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      debugShowCheckedModeBanner: false,
      initialRoute: 'page1', // 初始页面为 page1
      routes: {
        'page1': (_) => Page1(), // 定义路由映射
        'page2': (_) => Page2(),
      },
    );
  }
}

class Page1 extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Page 1"), // 页面标题
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      backgroundColor: Colors.blue, // 页面背景颜色
      body: Center(
        child: MaterialButton(
          child: Text("Go to page 2"), // 按钮文本
          color: Colors.white, // 按钮颜色
          onPressed: () {
            // 使用 RouteTransitions 实现自定义路由过渡
            Navigator.push(
              context,
              RouteTransitions(
                context: context,
                child: Page2(), // 目标页面
                animation: AnimationType.fadeIn, // 动画类型
                duration: Duration(milliseconds: 100), // 动画时长
                replacement: true, // 是否替换当前页面
              ),
            );
          },
        ),
      ),
    );
  }
}

class Page2 extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Page 2"), // 页面标题
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      backgroundColor: Colors.blueGrey, // 页面背景颜色
      body: Center(
        child: Text('Page2'), // 页面中心内容
      ),
    );
  }
}

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

1 回复

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


custom_route_transitions_prueba_ebc 是一个用于 Flutter 的自定义路由过渡动画插件。它可以帮助你在导航到新页面时实现自定义的过渡动画效果。以下是如何使用这个插件的基本步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加插件的依赖。打开 pubspec.yaml 文件并在 dependencies 部分添加以下内容:

dependencies:
  flutter:
    sdk: flutter
  custom_route_transitions_prueba_ebc: ^1.0.0  # 请确保使用最新版本

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

2. 导入插件

在你的 Dart 文件中导入插件:

import 'package:custom_route_transitions_prueba_ebc/custom_route_transitions_prueba_ebc.dart';

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

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

import 'package:flutter/material.dart';
import 'package:custom_route_transitions_prueba_ebc/custom_route_transitions_prueba_ebc.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.fade, // 选择过渡动画类型
                duration: Duration(seconds: 1), // 设置动画持续时间
              ),
            );
          },
          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.slide: 滑动效果
  • TransitionType.scale: 缩放效果
  • TransitionType.rotation: 旋转效果
  • TransitionType.custom: 自定义效果 (你可以通过 transitionBuilder 参数来自定义过渡动画)

5. 自定义过渡动画

如果你选择 TransitionType.custom,你可以通过 transitionBuilder 参数来自定义过渡动画。以下是一个示例:

Navigator.push(
  context,
  CustomRouteTransition(
    child: SecondPage(),
    transitionType: TransitionType.custom,
    transitionBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
      return ScaleTransition(
        scale: animation,
        child: child,
      );
    },
    duration: Duration(seconds: 1),
  ),
);
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!