Flutter自定义路由过渡动画插件custom_route_transition_daec的使用
Flutter自定义路由过渡动画插件custom_route_transition_daec的使用
本插件实现了淡入(FadeIn)等过渡效果来在多个页面之间导航。
示例代码
import 'package:flutter/material.dart';
import 'package:custom_route_transition_daec/custom_route_transition_daec.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(
centerTitle: true,
title: const Text('Page1'),
backgroundColor: Colors.transparent,
),
backgroundColor: Colors.blue,
body: Center(
child: MaterialButton(
child: const Text('Go to page 2'),
color: Colors.white,
onPressed: () {
// 使用RouteTransitions进行页面跳转
Navigator.of(context).push(
RouteTransitions(
context: context,
child: const Page2(),
animation: AnimationType.fadeIn,
duration: const Duration(seconds: 5),
replacement: true,
),
);
},
),
),
);
}
}
class Page2 extends StatelessWidget {
const Page2({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: const Text('Page2'),
backgroundColor: Colors.transparent,
),
backgroundColor: Colors.blueGrey,
body: const Center(
child: Text('Pagina2'),
),
);
}
}
说明
-
导入包:
import 'package:custom_route_transition_daec/custom_route_transition_daec.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(), }, ); } }
-
定义Page1页面:
class Page1 extends StatelessWidget { const Page1({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( centerTitle: true, title: const Text('Page1'), backgroundColor: Colors.transparent, ), backgroundColor: Colors.blue, body: Center( child: MaterialButton( child: const Text('Go to page 2'), color: Colors.white, onPressed: () { // 跳转到Page2并设置过渡动画 Navigator.of(context).push( RouteTransitions( context: context, child: const Page2(), animation: AnimationType.fadeIn, duration: const Duration(seconds: 5), replacement: true, ), ); }, ), ), ); } }
-
定义Page2页面:
class Page2 extends StatelessWidget { const Page2({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( centerTitle: true, title: const Text('Page2'), backgroundColor: Colors.transparent, ), backgroundColor: Colors.blueGrey, body: const Center( child: Text('Pagina2'), ), ); } }
更多关于Flutter自定义路由过渡动画插件custom_route_transition_daec的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter自定义路由过渡动画插件custom_route_transition_daec的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
custom_route_transition_daec
是一个 Flutter 插件,用于自定义路由过渡动画。通过这个插件,你可以为 Flutter 应用中的页面跳转添加自定义的过渡效果,而不是使用默认的 Material 或 Cupertino 过渡动画。
使用步骤
-
添加依赖: 首先,你需要在
pubspec.yaml
文件中添加custom_route_transition_daec
插件的依赖。dependencies: flutter: sdk: flutter custom_route_transition_daec: ^1.0.0 # 请使用最新版本
然后运行
flutter pub get
来安装依赖。 -
导入插件: 在你的 Dart 文件中导入插件。
import 'package:custom_route_transition_daec/custom_route_transition_daec.dart';
-
使用自定义路由过渡动画: 你可以使用
CustomRouteTransition
来定义自定义的路由过渡动画,并将其应用于Navigator.push
或Navigator.pushReplacement
方法中。以下是一个简单的示例,展示如何使用
custom_route_transition_daec
插件:import 'package:flutter/material.dart'; import 'package:custom_route_transition_daec/custom_route_transition_daec.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Custom Route Transition Demo', 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( builder: (context) => SecondPage(), transitionType: TransitionType.slideLeft, 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'), ), ), ); } }
在上面的示例中,我们使用了
CustomRouteTransition
来定义从HomePage
到SecondPage
的过渡动画。transitionType
参数指定了过渡动画的类型(例如,slideLeft
、slideRight
、fade
等),duration
参数指定了动画的持续时间。 -
自定义过渡动画: 如果需要更复杂的过渡动画,你可以自定义
PageRouteBuilder
并实现自己的过渡效果。CustomRouteTransition
内部使用了PageRouteBuilder
,因此你可以通过传递自定义的transitionsBuilder
来创建独特的过渡效果。Navigator.push( context, CustomRouteTransition( builder: (context) => SecondPage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: animation, child: child, ); }, duration: Duration(milliseconds: 500), ), );