flutter - 在颤动的卡片堆栈视图中将前面的卡片动画到后面
问题描述
我有一堆卡片和滚轮。我正在尝试为前面的卡片设置动画,将其向右移动,然后以某种方式将其带回卡片的背面。我使用了一个未来函数来指定应该首先完成的部分代码。但我得到的是;它首先改变卡片的索引,动画发生。这是我的代码:
class AnimationsPractice extends StatefulWidget {
static const String id = 'test_screen';
@override
_AnimationsPracticeState createState() => _AnimationsPracticeState();
}
class _AnimationsPracticeState extends State<AnimationsPractice>
with SingleTickerProviderStateMixin {
FixedExtentScrollController _scrollController =
FixedExtentScrollController(initialItem: 0);
AnimationController _controller;
Animation<Offset> _offsetAnimation;
int selected;
List<Widget> sampleCard;
Animation<Offset> _offsetAnimation2;
bool halfWayAnimation;
@override
void initState() {
super.initState();
_controller =
AnimationController(duration: const Duration(seconds: 1), vsync: this)
..repeat();
_offsetAnimation = Tween<Offset>(
begin: Offset.zero,
end: const Offset(1.5, 0.0),
).animate(
CurvedAnimation(
parent: _controller,
curve: Interval(
0.0,
0.5,
curve: Curves.elasticIn,
),
),
);
_offsetAnimation2 = Tween<Offset>(
begin: const Offset(1.5, 0.0),
end: Offset.zero,
).animate(
CurvedAnimation(
parent: _controller,
curve: Interval(
0.5,
1.0,
curve: Curves.elasticIn,
),
),
);
halfWayAnimation = false;
_controller.stop();
sampleCard = [
Container(
height: 60,
width: 40,
color: Colors.red,
),
Transform.rotate(
angle: 10 * (pi / 180),
child: Container(
height: 60,
width: 40,
color: Colors.blueGrey,
)),
SlideTransition(
position: halfWayAnimation ? _offsetAnimation2 : _offsetAnimation,
child: Container(
height: 60,
width: 40,
color: Colors.yellow,
),
),
];
}
@override
void dispose() {
super.dispose();
_controller.dispose();
}
Future<void> _playAnimation() async {
try {
await _controller.forward().orCancel;
await siftingIndex();
await _controller.reverse().orCancel;
} on TickerCanceled {
// the animation got canceled, probably because it was disposed of
}
}
Future<void> siftingIndex() {
return Future.delayed(const Duration(microseconds: 200), () {
sampleCard.insert(0, sampleCard[sampleCard.length - 1]);
sampleCard.removeLast();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: const Size.fromHeight(180.0),
child: SafeArea(
child: TextButton(
child: Text('back to login'),
onPressed: () {
Navigator.pushNamed(context, LoginScreen.id);
},
),
),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Stack(children: sampleCard),
CustomScrollWheel(
onItemChange: (x) {
setState(() {
_playAnimation();
halfWayAnimation = true;
});
},
scrollController: _scrollController,
),
],
),
);
}
}
解决方案
推荐阅读
- mongodb - Mongodb聚合对查找结果应用排序,并添加字段索引号
- javascript - 如何为在 java 脚本节点/html 中单击的按钮添加指定的图像?(我是新手)
- r - which.max 结果在列表中
- google-cloud-platform - 如何运行 Python 脚本将数据发送到 Google PubSub
- python - 从网络链接中删除字符然后下载
- angular - 从包含Angular中的文本和标签的字符串中显示滑块中的图像
- python - 在 Python 中将输入函数参数放入字典的最佳方法是什么?
- javascript - 设置秒和毫秒的计时器
- appium - 在 Android 应用程序的情况下如何选择所有文本?webdriver 有什么可以全选的吗?
- go - 多次使用时 TCP 连接返回 'broken pipe' 错误