flutter:页面切换时的动画
一,代码:
1,定义三个常用动画的类
FadeRoute.dart
import 'package:flutter/material.dart';
class FadeRoute extends PageRouteBuilder {
final Widget page;
FadeRoute(this.page) : super(
pageBuilder: (
context,
animation,
secondaryAnimation,
) {
return page;
},
transitionsBuilder: (
context,
animation,
secondaryAnimation,
child,
) {
return FadeTransition(
opacity: animation,
child: child,
);
},
transitionDuration: Duration(milliseconds: 800),
);
}
SlideRoute.dart
import 'package:flutter/material.dart';
class SlideRoute extends PageRouteBuilder {
final Widget page;
SlideRoute(this.page) : super(
pageBuilder: (
context,
animation,
secondaryAnimation,
) {
return page;
},
transitionsBuilder: (
context,
animation,
secondaryAnimation,
child,
) {
//左侧滑动进入退出
return SlideTransition(
position: Tween<Offset>(
begin: Offset(0.0, 0.5),
end: Offset(0.0, 0.0)
).animate(CurvedAnimation(
parent: animation,
//curve:Curves.fastOutSlowIn
curve:Curves.easeOut
)),
child: child,
);
},
transitionDuration: Duration(milliseconds: 400),
);
}
import 'package:flutter/material.dart';
class ScaleRoute extends PageRouteBuilder {
final Widget page;
ScaleRoute(this.page) : super(
pageBuilder: (
context,
animation,
secondaryAnimation,
) {
return page;
},
transitionsBuilder: (
context,
animation,
secondaryAnimation,
child,
) {
return ScaleTransition(
//alignment: Alignment.bottomCenter,
alignment: Alignment.center,
scale: Tween(
begin: 0.0,
end: 1.0,
).animate(
CurvedAnimation(
parent: animation,
curve: Curves.easeInOut,
),
),
child: child,
);
},
transitionDuration: Duration(seconds: 1),
);
}
2,切换时调用
import 'package:flutter/material.dart';
import 'package:tangpoem/pages/CopyPage.dart';
import 'package:tangpoem/pages/SystemPage.dart';
import 'package:tangpoem/pages/SaveGalleryPage.dart';
import 'package:tangpoem/common/ScaleRoute.dart';
import 'package:tangpoem/common/SlideRoute.dart';
import 'package:tangpoem/common/FadeRoute.dart';
class MyHomePage extends StatefulWidget {
//const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
//final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text("首页"),
),
body: SingleChildScrollView(
child: Center(
child: Column(
children: <Widget>[
SizedBox(height: 5),
//Text('第一个子组件'),
ElevatedButton(
child: Text("系统信息"),
onPressed: () {
Navigator.of(context).push(ScaleRoute(SystemPage(arguments:{'title': "系统信息",'id': 7})));
},
),
SizedBox(height: 5),
//Text('第一个子组件'),
ElevatedButton(
child: Text("复制粘贴"),
onPressed: () {
Navigator.of(context).push(SlideRoute(CopyPage(arguments:{'title': "复制粘贴",'id': 7})));
},
),
SizedBox(height: 5),
//Text('第一个子组件'),
ElevatedButton(
child: Text("保存到相册"),
onPressed: () {
//print("这里是首页,点击进入详情页面");
// 跳转到第二个界面
Navigator.of(context).push(FadeRoute(SaveGalleryPage(arguments:{'title': "保存到相册",'id': 7})));
},
),
],
),
),
),
// This trailing comma makes auto-formatting nicer for build methods.
);
}
}
浙公网安备 33010602011771号