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

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

路由过渡(Route Transitions)

这个插件帮助实现页面之间的过渡效果。

使用示例

/// [BuildContext] 是构建上下文
RouteTransitions(
    context: context,
    child: const Page2(),
    animation: AnimationType.fadeIn, 
    duration: const Duration(milliseconds: 300),
    replacement: true
);

完整示例代码

以下是一个完整的示例代码,展示了如何在 Flutter 应用程序中使用 custom_route_transition_elvin_loaisiga 插件来实现自定义的路由过渡动画。

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

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

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

class Page1 extends StatelessWidget {
  const Page1({Key? key}) : super(key: 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(
          color: Colors.white,
          onPressed: () {
            // 使用 RouteTransitions 实现过渡动画
            Navigator.of(context).push(
              RouteTransitions(
                context: context,
                child: const Page2(),
                animation: AnimationType.fadeIn,
                duration: const Duration(milliseconds: 300),
                replacement: true,
              ),
            );
          },
          child: const Text('Go to Page 2'),
        ),
      ),
    );
  }
}

class Page2 extends StatelessWidget {
  const Page2({Key? key}) : super(key: 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('Hello World'),
      ),
    );
  }
}

说明

  1. 导入库

    import 'package:custom_route_transition_elvin_loaisiga/custom_route_transition_elvin_loaisiga.dart';
    
  2. 主应用配置

    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'Material App',
          initialRoute: 'page1',
          routes: {
            'page1': (_) => const Page1(),
            'page2': (_) => const Page2(),
          },
        );
      }
    }
    

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

1 回复

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


custom_route_transition_elvin_loaisiga 是一个用于 Flutter 的自定义路由过渡动画插件。它允许开发者在页面导航时使用自定义的过渡动画,而不是默认的 Material 或 Cupertino 过渡动画。

安装插件

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

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

然后运行 flutter pub get 来安装插件。

使用插件

使用 custom_route_transition_elvin_loaisiga 插件非常简单。你可以在 Navigator.pushNavigator.pushReplacement 中使用 CustomRouteTransition 来定义自定义的过渡动画。

以下是一个简单的示例,展示如何使用该插件:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Custom Route Transition Demo',
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Screen'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.push(
              context,
              CustomRouteTransition(
                builder: (context) => SecondScreen(),
                transitionType: TransitionType.slideLeft, // 选择过渡动画类型
                duration: Duration(seconds: 1), // 设置动画持续时间
              ),
            );
          },
          child: Text('Go to Second Screen'),
        ),
      ),
    );
  }
}

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

支持的过渡动画类型

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

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

自定义动画持续时间

你可以通过 duration 参数来设置动画的持续时间。例如:

duration: Duration(seconds: 1),
回到顶部