flutter - Flutter中跨行单元格的动画位置
问题描述
我有一个Row
小部件,它在它的一个单元格中保存一个子小部件(其他的有一个Spacer()
)。根据状态的不同,持有孩子的单元格会发生变化,从而导致孩子 Widget 的位置发生变化。我想为这个动作制作动画以使其平滑。有没有办法使用标准动画小部件(类似AnimatedPositioned
,在这种情况下不起作用)?
解决方案
如果在行内插入 Stack,则可以使用 AnimatedPositioned。否则,您可以在小部件的 X 轴上使用AnimatedBuilder
withTransform.translate
并为其设置动画。Offset
下面有一个完整的示例,您可以在DartPad上运行以查看结果。希望能帮助到你。
import 'package:flutter/material.dart';
void main(){
runApp(MaterialApp(home: MyAnimation()));
}
class MyAnimation extends StatefulWidget {
@override
_MyAnimationState createState() => _MyAnimationState();
}
class _MyAnimationState extends State<MyAnimation> with SingleTickerProviderStateMixin {
AnimationController _controller;
Animation<double> animation;
@override
void initState() {
super.initState();
_controller = AnimationController(vsync: this, duration: Duration(milliseconds: 5000));
animation = Tween(begin: 0.0, end: 300.0).animate(_controller);
_controller.forward();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Title")),
body: AnimatedBuilder(
animation: _controller,
builder: (context, child) {
return Row(
children: <Widget>[
Transform.translate(
child: Container(width: 100, height: 100, color: Colors.black),
offset: Offset(animation.value, 0),
),
Container(width: 100, height: 100, color: Colors.transparent),
Container(width: 100, height: 100, color: Colors.transparent),]
);
},
),
);
}
}
推荐阅读
- ember.js - 有没有办法将所有属性传递给 Ember 组件?
- ruby-on-rails - Rails 为关联模型生成 ave
- c++ - 自定义链表const_iterator不能遍历list的非常量实例
- reactjs - 反应:渲染()和在屏幕上看到结果之间的延迟
- ios - 如何将 4 个 UITextfields 的信息合并到一个 UILabel 中
- php - 如何在 PHP 中将对象数组定义为参数?
- c# - ASP.NET 4.6,网址被附加在地址栏中,而不是被重写
- perl - 读取 CSV 并生成字段名称为已读取的文本文件
- javascript - 如何在我的内容脚本中使用 chrome.download?
- objective-c - 在 Pod 中找不到模块“TOCropViewController”