|
@@ -5,34 +5,36 @@ import 'package:fooddeliveryapp/model/tableDetail.dart';
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
class CartCounter extends StatefulWidget {
|
|
|
- final Product product;
|
|
|
+ final Product? product;
|
|
|
const CartCounter({
|
|
|
- Key key,
|
|
|
- @required this.product,
|
|
|
+ Key? key,
|
|
|
+ required this.product,
|
|
|
}) : super(key: key);
|
|
|
@override
|
|
|
_CartCounterState createState() => _CartCounterState(product);
|
|
|
}
|
|
|
|
|
|
class _CartCounterState extends State<CartCounter> {
|
|
|
- final Product product;
|
|
|
+ final Product? product;
|
|
|
|
|
|
_CartCounterState(this.product);
|
|
|
@override
|
|
|
Widget build(BuildContext context) {
|
|
|
final myTableDetail = Provider.of<TableDetail>(context);
|
|
|
- int numOfItems = myTableDetail.getItemCount(product);
|
|
|
+ int? numOfItems = myTableDetail.getItemCount(product);
|
|
|
return Row(
|
|
|
children: [
|
|
|
buildOutlineButton(
|
|
|
icon: Icons.remove,
|
|
|
press: () {
|
|
|
- if (numOfItems > 0) {
|
|
|
+ if (numOfItems! > 0) {
|
|
|
setState(() {
|
|
|
- numOfItems--;
|
|
|
+ if (numOfItems != null) {
|
|
|
+ numOfItems = numOfItems! - 1;
|
|
|
+ }
|
|
|
myTableDetail.removeItem(product);
|
|
|
print("[通知]减少商品" +
|
|
|
- product.id.toString() +
|
|
|
+ product!.id.toString() +
|
|
|
"NUMS:" +
|
|
|
myTableDetail.getItemCount(product).toString());
|
|
|
});
|
|
@@ -44,17 +46,19 @@ class _CartCounterState extends State<CartCounter> {
|
|
|
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.headline6,
|
|
|
+ style: Theme.of(context).textTheme.titleLarge,
|
|
|
),
|
|
|
),
|
|
|
buildOutlineButton(
|
|
|
icon: Icons.add,
|
|
|
press: () {
|
|
|
setState(() {
|
|
|
- numOfItems++;
|
|
|
+ if (numOfItems != null) {
|
|
|
+ numOfItems = numOfItems! + 1;
|
|
|
+ }
|
|
|
myTableDetail.addItem(product);
|
|
|
print("[通知]添加商品" +
|
|
|
- product.id.toString() +
|
|
|
+ product!.id.toString() +
|
|
|
"NUMS:" +
|
|
|
myTableDetail.getItemCount(product).toString());
|
|
|
});
|
|
@@ -63,7 +67,7 @@ class _CartCounterState extends State<CartCounter> {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- SizedBox buildOutlineButton({IconData icon, Function press}) {
|
|
|
+ SizedBox buildOutlineButton({IconData? icon, Function? press}) {
|
|
|
return SizedBox(
|
|
|
width: 40,
|
|
|
height: 32,
|
|
@@ -72,7 +76,7 @@ class _CartCounterState extends State<CartCounter> {
|
|
|
padding: MaterialStateProperty.all(EdgeInsets.zero),
|
|
|
shape: MaterialStateProperty.all(RoundedRectangleBorder(
|
|
|
borderRadius: BorderRadius.circular(13)))),
|
|
|
- onPressed: press,
|
|
|
+ onPressed: press as void Function()?,
|
|
|
child: Icon(icon),
|
|
|
),
|
|
|
);
|