Flutter:进步器,数量加减简单使用
封装组件
import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/cupertino.dart';
import '../index.dart';
/// 数量编辑
class QuantityWidget extends StatelessWidget {
// 数量发送改变
final Function(int quantity) onChange;
// 数量
final int quantity;
const QuantityWidget({
super.key,
required this.quantity,
required this.onChange,
});
@override
Widget build(BuildContext context) {
return <Widget>[
// 减号
<Widget>[
Icon(CupertinoIcons.minus,size: 32.w,color: const Color(0xff000000),),
].toRow(mainAxisAlignment: MainAxisAlignment.center)
.card(color: const Color(0xfff8f8f8),shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.w)))
.tight(width: 56.w,height: 56.w)
.onTap(() => onChange(quantity - 1 < 0 ? 0 : quantity - 1)),
// 数量
TextWidget.body(
"$quantity",
size: 24.sp,
weight: FontWeight.w600,
)
.center()
.tight(width: 68.w,height: 56.w),
// 加号
<Widget>[
Icon(CupertinoIcons.plus,size: 32.w,color: const Color(0xff000000),),
].toRow(mainAxisAlignment: MainAxisAlignment.center)
.card(color: const Color(0xfff8f8f8),shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.w)))
.tight(width: 56.w,height: 56.w)
.onTap(() => onChange(quantity + 1)),
].toRow(mainAxisAlignment: MainAxisAlignment.spaceBetween);
}
}
页面中使用
QuantityWidget(
quantity: controller.quantity,
onChange: (value) {
controller.onQuantityChange(value);
},
),
// 数量
int quantity = 1;
// 修改数量
void onQuantityChange(int value) {
if (value <= 0) {
value = 1;
}
quantity = value;
update(["my_cart"]);
}
原文地址:https://blog.csdn.net/qq_40745143/article/details/145277512
免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!