Flutter 颤振中的来回动画
我试图做一个动画,使一个绿色的盒子来回移动。尝试使用onEnd和curve。所以我想让这个绿色的盒子从右往回走。它将前后移动,我还必须在setState中添加一些内容Flutter 颤振中的来回动画,flutter,Flutter,我试图做一个动画,使一个绿色的盒子来回移动。尝试使用onEnd和curve。所以我想让这个绿色的盒子从右往回走。它将前后移动,我还必须在setState中添加一些内容 import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { // This widget is the root of your application.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double _moveX = 0.0;
void _incrementCounter() {
setState(() {
_moveX++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child: AnimatedAlign(
duration: Duration(microseconds: 300),
curve: Curves.bounceIn,
alignment: Alignment(_moveX, -0.0),
child: Container(
width: 50,
height: 50,
color: Colors.green,
),
),
)
],
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
double _moveX=0.0;
void _incrementCounter(){
设置状态(){
_moveX++;
});
}
@凌驾
小部件构建(构建上下文){
//每次调用setState时都会重新运行此方法,例如,按done
//通过上面的_incrementCounter方法。
//
//对颤振框架进行了优化,以制定重新运行的构建方法
//快速,这样你就可以重建任何需要更新的东西
//而不是必须单独更改小部件的实例。
返回脚手架(
appBar:appBar(
//在这里,我们从MyHomePage对象中获取由创建的值
//使用App.build方法,并使用它设置appbar标题。
标题:文本(widget.title),
),
正文:中(
//中心是一个布局小部件。它接受一个子元素并对其进行定位
/在父母的中间。
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
容器(
孩子:动画片(
持续时间:持续时间(微秒:300),
曲线:Curves.bounceIn,
对齐:对齐(_moveX,-0.0),
子:容器(
宽度:50,
身高:50,
颜色:颜色。绿色,
),
),
)
],
),
),//此尾随逗号使生成方法的自动格式设置更方便。
);
}
}
在的onEnd
属性中,使用某种递减计数器
方法触发动画返回,该方法与\u incrementCounter
相同,但相反是递减的
void\u递减计数器(){
设置状态(){
_moveX--;
});
}
要最初触发动画,您应该在initState
中增加位置值,但必须将其注册为帧后回调,以确保挂载小部件,并在AnimatedAlign
中正确触发动画。前
@覆盖
void initState(){
super.initState();
addPostFrameCallback(_incrementCounter);
}
由于还希望重复此动画,因此需要根据上次调用的方法有条件地调用\u incrementCounter
或\u decrementCounter
。只需检查当前位置即可轻松完成此操作。前
onEnd:(){
如果(_moveX.round()==0){//round可能无法使用,具体取决于要在其中设置动画的值的范围
_递增计数器();
}
否则{
_递减计数器();
}
}
这可能更简单地使用,因为它有一个
repeat
方法,但在这种情况下使用它可能不会给您带来足够的好处。下面是一个本地通知的示例,该通知显示3秒钟,并带有来回动画
import 'package:flutter/material.dart';
import 'package:romobili_poc/app/constants/AppConstants.dart';
class LevelNotification extends StatefulWidget {
@override
_LevelNotificationState createState() => _LevelNotificationState();
}
class _LevelNotificationState extends State<LevelNotification>
with SingleTickerProviderStateMixin {
AnimationController levelNotificationController;
Animation<Offset> levelNotificationOffset;
@override
void initState() {
levelNotificationController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 500),
);
levelNotificationOffset = Tween<Offset>(
begin: Offset(0.0, -1.0),
end: Offset(0.0, 0.0),
).animate(levelNotificationController);
levelNotificationOffset.addStatusListener((status) {
print(status);
Future.delayed(
const Duration(seconds: 3),
() => levelNotificationController.reverse(),
);
});
super.initState();
}
@override
void dispose() {
levelNotificationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.topCenter,
child: SlideTransition(
position: levelNotificationOffset,
child: InkWell(
onTap: () {
levelNotificationController.forward();
},
child: Container(
color: Colors.red,
height: 100,
width: double.infinity,
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“package:romobili_poc/app/constants/AppConstants.dart”;
类级别通知扩展了StatefulWidget{
@凌驾
_LevelNotificationState createState()=>\u LevelNotificationState();
}
类_LevelNotificationState扩展状态
使用SingleTickerProviderStateMixin{
AnimationController级别NotificationController;
动画级别NotificationOffset;
@凌驾
void initState(){
levelNotificationController=动画控制器(
vsync:这个,,
持续时间:常量持续时间(毫秒:500),
);
levelNotificationOffset=两个(
开始:偏移量(0.0,-1.0),
结束:偏移量(0.0,0.0),
).设置动画(levelNotificationController);
levelNotificationOffset.addStatusListener((状态){
打印(状态);
推迟(
常数持续时间(秒:3),
()=>levelNotificationController.reverse(),
);
});
super.initState();
}
@凌驾
无效处置(){
levelNotificationController.dispose();
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回对齐(
对齐:alignment.topCenter,
子:幻灯片转换(
位置:levelNotificationOffset,
孩子:InkWell(
onTap:(){
levelNotificationController.forward();
},
子:容器(
颜色:颜色,红色,
身高:100,
宽度:double.infinity,
),
),
),
);
}
}