gpt4 book ai didi

flutter 动画 : how to keep position of parent widget

转载 作者:IT王子 更新时间:2023-10-29 07:18:57 26 4
gpt4 key购买 nike

我在处理 Flutter 中的动画时遇到了一些问题。有3个圆圈彼此相邻。当按下其中一个时,我希望它展开和折叠。动画有效,但当它发生时整行圆圈向下移动,以保持圆圈所在行的上边距。如何确保该行保持其原始位置?

我只将动画应用到中心圆来测试它。如果代码困惑,我很抱歉,那是因为我正在测试它。这是动画和动画 Controller :

    _animationController =
AnimationController(vsync: this, duration: Duration(milliseconds: 200));
_animation = Tween(begin: 0.25, end: 0.35).animate(
CurvedAnimation(parent: _animationController, curve: Curves.elasticIn))
..addStatusListener((status) {
if (status == AnimationStatus.completed) {
_animationController.reverse();
}
});

这些是圆圈:

Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 16.0),
child: Center(
child: Text(
'Circles',
style: TextStyle(fontSize: 18),
),
),
),
Container(
margin: EdgeInsets.only(top: 8.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width * 0.25,
height: MediaQuery.of(context).size.height * 0.2,
margin: EdgeInsets.symmetric(horizontal: 8.0),
child: Center(child: Text('Circle')),
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: AppColors.primaryBlue)),
),
AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return GestureDetector(
onTap: () {
_animationController.forward();
},
child: Container(
width: MediaQuery.of(context).size.width *
_animation.value,
height: MediaQuery.of(context).size.height *
_animation.value,
margin: EdgeInsets.symmetric(horizontal: 8.0),
child: Center(child: Text('Circle')),
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: AppColors.primaryBlue)),
),
);
},
),
Container(
width: MediaQuery.of(context).size.width * 0.25,
height: MediaQuery.of(context).size.height * 0.2,
margin: EdgeInsets.symmetric(horizontal: 8.0),
child: Center(child: Text('Circle')),
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: AppColors.primaryBlue)),
)
],
),
),

您可能会注意到我对 Flutter 中的动画还很陌生,因此也非常欢迎其他反馈。谢谢!

最佳答案

输出:

enter image description here

完整代码:

Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 16.0),
child: Center(
child: Text(
'Circles',
style: TextStyle(fontSize: 18),
),
),
),
Container(
height: 200,
margin: EdgeInsets.only(top: 8.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width * 0.25,
height: MediaQuery.of(context).size.height * 0.2,
margin: EdgeInsets.symmetric(horizontal: 8.0),
child: Center(child: Text('Circle')),
decoration: BoxDecoration(shape: BoxShape.circle, border: Border.all(color: AppColors.primaryBlue)),
),
Spacer(),
AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return GestureDetector(
onTap: () {
_animationController.forward();
},
child: Container(
width: MediaQuery.of(context).size.width * _animation.value,
height: MediaQuery.of(context).size.height * _animation.value,
margin: EdgeInsets.symmetric(horizontal: 8.0),
child: Center(child: Text('Circle')),
decoration: BoxDecoration(shape: BoxShape.circle, border: Border.all(color: AppColors.primaryBlue)),
),
);
},
),
Spacer(),
Container(
width: MediaQuery.of(context).size.width * 0.25,
height: MediaQuery.of(context).size.height * 0.2,
margin: EdgeInsets.symmetric(horizontal: 8.0),
child: Center(child: Text('Circle')),
decoration: BoxDecoration(shape: BoxShape.circle, border: Border.all(color: AppColors.primaryBlue)),
)
],
),
),
],
);

关于 flutter 动画 : how to keep position of parent widget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56405744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com