flutter - 在颤动中动态扩展一个组件并压缩另一个组件
问题描述
我有以下代码
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Flexible(
child: Container(
height: 32.0,
padding: EdgeInsets.symmetric(
horizontal: 12.0, vertical: 5.0),
decoration: BoxDecoration(
color: Colors.green.shade300,
borderRadius: BorderRadius.circular(7.0),
),
child: Row(
children: <Widget>[
Flexible(
child: Text(
'Save 20%',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: GoogleFonts.poppins(
fontSize: 12.0,
color: Colors.white,
fontWeight: FontWeight.w500,
letterSpacing: 0.5,
),
),
),
],
),
),
),
SizedBox(
width: 8.0,
),
ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Material(
child: InkWell(
splashColor: Colors.green.withOpacity(0.5),
onTap: () {
print('Add to cart');
if (FirebaseAuth.instance.currentUser == null) {
Navigator.pushNamed(context, '/sign_in');
return;
}
cartBloc.add(
AddToCartEvent(product.id, currentUser.uid));
},
child: Container(
width: 38.0,
height: 35.0,
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.01),
borderRadius: BorderRadius.circular(8.0),
border: Border.all(
width: 0.8,
color: Colors.black.withOpacity(0.15),
),
),
child: Icon(
FontAwesomeIcons.cartPlus,
color: Colors.black45,
size: 18.0,
),
),
),
),
),
],
),
),
这导致以下视图
我想有以下条件
When I click the cart icon for the first time
1. add to cart button should expand if quantity > 0
2. "You save 20%" component should compress to square box "20%"
像这样的东西
我非常感谢任何形式的反馈、评论、提示、解决方案指针。谢谢
解决方案
我认为在您的情况下,您可以使用 AnimatedContainer 类。
伪代码
var isClickedOnCart;
AnimatedContainer(
....
width: isClickedOnCart? 100 : 20;
...
/// YOUR CODE HERE///
)
/// ONPRESSED CART CARD
onPressed(){
if(quantity > 0){
setState(){
isClickedOnCart = !isClickedOnCart;
}
}
}
推荐阅读
- python - 使用 Selenium 选择时按钮失去作用
- python - Python textract 以错误的顺序读取分页 PDF
- svg - 如何检测一个点是否在复杂的不规则矢量形状(SVG)内?
- javascript - 如何使用反应钩子限制多个复选框输入
- javascript - JS/Jasmine:为现在根据参数执行不同操作的函数转换测试
- prometheus - 可以将 Consul 服务发现配置为使用 TLS 吗?
- typeorm - TypeORM 一对一关系:两边都找不到实体列?
- c++ - C++ 入门第 5 版。虚拟继承和ctor-init
- c# - 存储过程需要未提供的参数 id 值
- r - 以闪亮的方式编辑过滤后的反应数据框,保存用户输入以在应用程序中使用