Flutter自定义路由过渡动画插件custom_router_transition_ah的使用
Flutter自定义路由过渡动画插件custom_router_transition_ah的使用
该插件帮助实现自定义的路由过渡动画。
使用示例
示例代码
import 'package:custom_router_transition_ah/custom_router_transition_ah.dart';
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.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({super.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进行页面跳转,并设置动画类型为fadeIn
Navigator.of(context).push(
RouteTransitions(
context: context,
child: const Page2(),
animation: AnimationType.fadeIn,
duration: const Duration(seconds: 1), // 设置动画持续时间为1秒
replacement: true,
),
);
},
child: const Text('Go to Page 2'),
),
),
);
}
}
class Page2 extends StatelessWidget {
const Page2({super.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('Page 2'),
),
);
}
}
完整示例Demo
在上面的示例中,我们展示了如何使用custom_router_transition_ah
插件来实现自定义路由过渡动画。具体步骤如下:
-
引入必要的包:
import 'package:custom_router_transition_ah/custom_router_transition_ah.dart'; import 'package:flutter/material.dart';
-
创建应用入口:
void main() => runApp(const MyApp());
-
创建主应用类:
class MyApp extends StatelessWidget { const MyApp({super.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({super.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: () { // 跳转到Page2,并设置动画效果 Navigator.of(context).push( RouteTransitions( context: context, child: const Page2(), animation: AnimationType.fadeIn, duration: const Duration(seconds: 1), replacement: true, ), ); }, child: const Text('Go to Page 2'), ), ), ); } }
-
创建第二个页面(Page2):
class Page2 extends StatelessWidget { const Page2({super.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('Page 2'), ), ); } }
更多关于Flutter自定义路由过渡动画插件custom_router_transition_ah的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter自定义路由过渡动画插件custom_router_transition_ah的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
custom_router_transition_ah
是一个用于 Flutter 的自定义路由过渡动画插件。它允许开发者自定义页面之间的过渡动画,提供更加灵活和个性化的用户体验。以下是如何使用 custom_router_transition_ah
插件的详细步骤。
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 custom_router_transition_ah
插件的依赖:
dependencies:
flutter:
sdk: flutter
custom_router_transition_ah: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 基本使用
custom_router_transition_ah
插件提供了一个 CustomPageRoute
类,可以用来定义自定义的路由过渡动画。
以下是一个简单的示例,展示如何使用 CustomPageRoute
来实现一个从下到上的页面过渡动画:
import 'package:flutter/material.dart';
import 'package:custom_router_transition_ah/custom_router_transition_ah.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Custom Router 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,
CustomPageRoute(
builder: (context) => SecondPage(),
transition: TransitionType.slideUp, // 从下到上的过渡动画
),
);
},
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'),
),
),
);
}
}
3. 自定义过渡动画
custom_router_transition_ah
提供了多种内置的过渡动画类型,例如 TransitionType.slideUp
, TransitionType.slideDown
, TransitionType.fade
, 等。你也可以通过 CustomPageRoute
的 transitionBuilder
参数来自定义过渡动画。
以下是一个自定义过渡动画的示例:
import 'package:flutter/material.dart';
import 'package:custom_router_transition_ah/custom_router_transition_ah.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Custom Router 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,
CustomPageRoute(
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
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. 其他功能
custom_router_transition_ah
还支持其他功能,例如设置过渡动画的持续时间、曲线等。你可以通过 duration
和 curve
参数来调整这些属性。
CustomPageRoute(
builder: (context) => SecondPage(),
transition: TransitionType.slideUp,
duration: Duration(milliseconds: 500), // 设置动画持续时间
curve: Curves.easeInOut, // 设置动画曲线
);