import 'package:flutter/material.dart'; import 'package:fooddeliveryapp/model/constants.dart'; import 'package:fooddeliveryapp/model/product.dart'; import 'package:fooddeliveryapp/model/tableDetail.dart'; import 'package:provider/provider.dart'; class CartCounter extends StatefulWidget { final Product? product; const CartCounter({ Key? key, required this.product, }) : super(key: key); @override _CartCounterState createState() => _CartCounterState(product); } class _CartCounterState extends State { final Product? product; _CartCounterState(this.product); @override Widget build(BuildContext context) { final myTableDetail = Provider.of(context); int? numOfItems = myTableDetail.getItemCount(product); return Row( children: [ buildOutlineButton( icon: Icons.remove, press: () { if (numOfItems! > 0) { setState(() { if (numOfItems != null) { numOfItems = numOfItems! - 1; } myTableDetail.removeItem(product); print("[通知]减少商品" + product!.id.toString() + "NUMS:" + myTableDetail.getItemCount(product).toString()); }); } }, ), Padding( padding: const EdgeInsets.symmetric(horizontal: kDefaultPaddin / 2), child: Text( // if our item is less then 10 then it shows 01 02 like that numOfItems.toString().padLeft(2, "0"), style: Theme.of(context).textTheme.titleLarge, ), ), buildOutlineButton( icon: Icons.add, press: () { setState(() { if (numOfItems != null) { numOfItems = numOfItems! + 1; } myTableDetail.addItem(product); print("[通知]添加商品" + product!.id.toString() + "NUMS:" + myTableDetail.getItemCount(product).toString()); }); }), ], ); } SizedBox buildOutlineButton({IconData? icon, Function? press}) { return SizedBox( width: 40, height: 32, child: TextButton( style: ButtonStyle( padding: MaterialStateProperty.all(EdgeInsets.zero), shape: MaterialStateProperty.all(RoundedRectangleBorder( borderRadius: BorderRadius.circular(13)))), onPressed: press as void Function()?, child: Icon(icon), ), ); } }