Flutter页面路由过渡动画插件route_transitions_pm的使用

Flutter 页面路由过渡动画插件 route_transitions_pm 的使用

自定义页面路由过渡动画。此插件可以帮助处理不同屏幕之间的过渡效果。

示例

以下是一个使用 RouteTransitions 插件的简单示例:

import 'package:flutter/material.dart';
import 'package:route_transitions_pm/route_transitions.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('首页'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.of(context).push(
              RouteTransitions(
                context: context,             // 使用当前的 BuildContext
                child: const SecondPage(),    // 跳转的目标页面
                animation: AnimationType.fadeIn,  // 选择动画类型
                duration: const Duration(milliseconds: 600), // 动画持续时间
                replacement: false,           // 使用 Navigation.push
              ),
            );
          },
          child: Text('跳转到第二页'),
        ),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('第二页'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.of(context).pop();
          },
          child: Text('返回'),
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的 Flutter 应用程序,其中包含两个页面:HomePageSecondPage。当用户点击按钮时,会从 HomePage 跳转到 SecondPage,并使用淡入动画进行过渡。返回时,使用默认的过渡动画。

代码解析

  1. 导入包

    import 'package:flutter/material.dart';
    import 'package:route_transitions_pm/route_transitions.dart';
    
  2. 主应用

    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: HomePage(),
        );
      }
    }
    
  3. 首页

    class HomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('首页'),
          ),
          body: Center(
            child: ElevatedButton(
              onPressed: () {
                Navigator.of(context).push(
                  RouteTransitions(
                    context: context,             // 当前的 BuildContext
                    child: const SecondPage(),    // 目标页面
                    animation: AnimationType.fadeIn,  // 动画类型
                    duration: const Duration(milliseconds: 600), // 动画持续时间
                    replacement: false,           // 使用 Navigation.push
                  ),
                );
              },
              child: Text('跳转到第二页'),
            ),
          ),
        );
      }
    }
    
  4. 第二页

    class SecondPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('第二页'),
          ),
          body: Center(
            child: ElevatedButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: Text('返回'),
            ),
          ),
        );
      }
    }
    

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

1 回复

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


route_transitions_pm 是一个用于在 Flutter 中实现自定义页面路由过渡动画的插件。它提供了多种内置的过渡动画效果,并允许开发者轻松自定义过渡动画。

安装插件

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

dependencies:
  flutter:
    sdk: flutter
  route_transitions_pm: ^1.0.0  # 请检查最新版本

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

使用插件

route_transitions_pm 提供了多种内置的过渡动画效果,例如 SlideTransitionScaleTransitionRotationTransitionFadeTransition 等。你可以通过 RouteTransitions 类来使用这些过渡动画。

基本使用

以下是一个简单的示例,展示如何使用 route_transitions_pm 实现页面之间的过渡动画:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Page'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.push(
              context,
              RouteTransitions(
                builder: (context) => SecondPage(),
                transitionType: TransitionType.slideLeft, // 使用内置的滑动过渡动画
              ),
            );
          },
          child: Text('Go to Second Page'),
        ),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  [@override](/user/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'),
        ),
      ),
    );
  }
}

在这个示例中,当用户点击按钮时,页面会从右侧滑入进入 SecondPage

自定义过渡动画

除了使用内置的过渡动画,你还可以自定义过渡动画。以下是一个自定义过渡动画的示例:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Page'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.push(
              context,
              RouteTransitions(
                builder: (context) => SecondPage(),
                transitionBuilder: (context, animation, secondaryAnimation, child) {
                  return ScaleTransition(
                    scale: Tween<double>(begin: 0.0, end: 1.0).animate(
                      CurvedAnimation(
                        parent: animation,
                        curve: Curves.easeInOut,
                      ),
                    ),
                    child: child,
                  );
                },
              ),
            );
          },
          child: Text('Go to Second Page'),
        ),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  [@override](/user/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'),
        ),
      ),
    );
  }
}
回到顶部