Flutter页面路由动画插件page_route_animator的使用

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

Flutter页面路由动画插件page_route_animator的使用

Page Route Animator Package

Flutter page route transition package, with 62 different page transitions.

Platform-Flutter Pub Package BSD-3-Clause Style-effective_dart

Examples

Example 1 Example 2 Example 3
page_route_animator_20_transitions_p1 page_route_animator_20_transitions_p2 page_route_animator_20_transitions_p3

Getting Started

pubspec.yaml文件中添加以下依赖:

dependencies:
  page_route_transition: ^1.0.4

Usage

要使用此包,您需要通过以下命令导入它:

import 'package:page_route_animator/page_route_animator.dart';

Code Example

以下是使用page_route_animator插件的一个完整示例:

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

void main(List<String> args) => runApp(const MyApp());

/// This widget is the root of your app.
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Page Route Animator Code Example',
      theme: ThemeData(primarySwatch: Colors.purple),
      home: const HomeScreen(),
      onGenerateRoute: (settings) {
        switch (settings.name) {
          case '/second-screen':
            return PageRouteAnimator(
              child: const SecondScreen(),
              routeAnimation: RouteAnimation.topToBottom,
              settings: settings,
              curve: Curves.linear,
              duration: const Duration(milliseconds: 500),
              reverseDuration: const Duration(milliseconds: 500),
            );
          default:
            return null;
        }
      },
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('HomeScreen'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: ListView(
          children: [
            ElevatedButton(
              onPressed: () {
                Navigator.pushNamed(
                  context,
                  '/second-screen',
                  arguments: 'I am going',
                );
              },
              child: getText('Top To Bottom - onGenerateRoute'),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.push(
                  context,
                  PageRouteAnimator(
                    child: const SecondScreen(),
                    routeAnimation: RouteAnimation.bottomToTop,
                    settings: const RouteSettings(arguments: 'I am going'),
                    curve: Curves.slowMiddle,
                    duration: const Duration(milliseconds: 500),
                    reverseDuration: const Duration(milliseconds: 500),
                  ),
                );
              },
              child: getText('Bottom To Top'),
            ),
            // More buttons for other animations...
          ],
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    String? argument = ModalRoute.of(context)?.settings.arguments as String?;
    return Scaffold(
      backgroundColor: Colors.yellow,
      appBar: AppBar(
        title: const Text('SecondScreen'),
      ),
      body: Center(
        child: Text(
          argument ?? 'No Data',
          style: const TextStyle(
            fontSize: 48.0,
            fontWeight: FontWeight.bold,
            color: Colors.black,
          ),
        ),
      ),
    );
  }
}

// Helper Widget
Widget getText(String text) {
  return Align(
    alignment: Alignment.centerLeft,
    child: Text(text),
  );
}

Types Of Transitions

  • topToBottom
  • bottomToTop
  • leftToRight
  • rightToLeft
  • fade
  • rotate
  • scale
  • size
  • sizeFromTop
  • sizeFromBottom
  • fadeAndRotate
  • fadeAndScale
  • rotateAndScale
  • fadeRotateAndScale
  • topLeftToBottomRight
  • topRightToBottomLeft
  • bottomLeftToTopRight
  • bottomRightToTopLeft
  • … (and many more)

Curves

了解更多关于曲线的信息,请参阅官方文档:curves.

License

BSD 3-Clause

以上是page_route_animator插件的详细使用说明和一个完整的示例代码。希望这能帮助你更好地理解和使用这个插件。如果你有任何问题或需要进一步的帮助,请随时提问!


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

1 回复

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


当然,page_route_animator 是一个用于 Flutter 的页面路由动画插件,它允许开发者轻松地为页面切换添加各种动画效果。以下是一个基本的代码示例,展示了如何在 Flutter 应用中使用 page_route_animator 来实现页面路由动画。

首先,确保你的 pubspec.yaml 文件中包含了 page_route_animator 依赖:

dependencies:
  flutter:
    sdk: flutter
  page_route_animator: ^x.y.z  # 替换为最新版本号

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

接下来,在你的 Flutter 应用中实现页面路由动画。以下是一个简单的示例,包括两个页面,并使用 PageRouteAnimator 来处理它们之间的导航和动画。

主应用文件 (main.dart)

import 'package:flutter/material.dart';
import 'package:page_route_animator/page_route_animator.dart';
import 'page1.dart';
import 'page2.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Page Route Animator Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Navigator(
        // 使用 PageRouteAnimator 包装 Navigator
        onGenerateRoute: (settings) {
          return PageRouteAnimator<dynamic>(
            pageBuilder: (context, animation, secondaryAnimation) {
              Widget page;
              switch (settings.name) {
                case '/':
                  page = Page1();
                  break;
                case '/page2':
                  page = Page2();
                  break;
                default:
                  page = Scaffold(
                    body: Center(
                      child: Text('Unknown route: ${settings.name}'),
                    ),
                  );
              }
              return page;
            },
            transitionsBuilder: (
              context,
              animation,
              secondaryAnimation,
              child,
            ) {
              // 自定义过渡动画,这里使用默认的 SlideTransition
              return SlideTransition(
                position: animation,
                child: child,
              );
            },
          );
        },
        initialRoute: '/',
      ),
    );
  }
}

页面1 (page1.dart)

import 'package:flutter/material.dart';

class Page1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Page 1'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.of(context).pushNamed('/page2');
          },
          child: Text('Go to Page 2'),
        ),
      ),
    );
  }
}

页面2 (page2.dart)

import 'package:flutter/material.dart';

class Page2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Page 2'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.of(context).pop();
          },
          child: Text('Go back to Page 1'),
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的 Flutter 应用,包含两个页面(Page1Page2)。我们使用 PageRouteAnimator 包装了 Navigator,并定义了 transitionsBuilder 来控制页面切换时的动画效果。在这个例子中,我们使用了默认的 SlideTransition,但你可以根据需要自定义动画效果。

PageRouteAnimator 提供了丰富的 API 来定制动画,比如 FadeTransitionScaleTransition 等,你可以根据需求选择合适的过渡动画。

回到顶部