cart_counter.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import 'package:flutter/material.dart';
  2. import 'package:fooddeliveryapp/model/constants.dart';
  3. import 'package:fooddeliveryapp/model/product.dart';
  4. import 'package:fooddeliveryapp/model/tableDetail.dart';
  5. import 'package:provider/provider.dart';
  6. class CartCounter extends StatefulWidget {
  7. final Product? product;
  8. const CartCounter({
  9. Key? key,
  10. required this.product,
  11. }) : super(key: key);
  12. @override
  13. _CartCounterState createState() => _CartCounterState(product);
  14. }
  15. class _CartCounterState extends State<CartCounter> {
  16. final Product? product;
  17. _CartCounterState(this.product);
  18. @override
  19. Widget build(BuildContext context) {
  20. final myTableDetail = Provider.of<TableDetail>(context);
  21. int? numOfItems = myTableDetail.getItemCount(product);
  22. return Row(
  23. children: [
  24. buildOutlineButton(
  25. icon: Icons.remove,
  26. press: () {
  27. if (numOfItems! > 0) {
  28. setState(() {
  29. if (numOfItems != null) {
  30. numOfItems = numOfItems! - 1;
  31. }
  32. myTableDetail.removeItem(product);
  33. print("[通知]减少商品" +
  34. product!.id.toString() +
  35. "NUMS:" +
  36. myTableDetail.getItemCount(product).toString());
  37. });
  38. }
  39. },
  40. ),
  41. Padding(
  42. padding: const EdgeInsets.symmetric(horizontal: kDefaultPaddin / 2),
  43. child: Text(
  44. // if our item is less then 10 then it shows 01 02 like that
  45. numOfItems.toString().padLeft(2, "0"),
  46. style: Theme.of(context).textTheme.titleLarge,
  47. ),
  48. ),
  49. buildOutlineButton(
  50. icon: Icons.add,
  51. press: () {
  52. setState(() {
  53. if (numOfItems != null) {
  54. numOfItems = numOfItems! + 1;
  55. }
  56. myTableDetail.addItem(product);
  57. print("[通知]添加商品" +
  58. product!.id.toString() +
  59. "NUMS:" +
  60. myTableDetail.getItemCount(product).toString());
  61. });
  62. }),
  63. ],
  64. );
  65. }
  66. SizedBox buildOutlineButton({IconData? icon, Function? press}) {
  67. return SizedBox(
  68. width: 40,
  69. height: 32,
  70. child: TextButton(
  71. style: ButtonStyle(
  72. padding: MaterialStateProperty.all(EdgeInsets.zero),
  73. shape: MaterialStateProperty.all(RoundedRectangleBorder(
  74. borderRadius: BorderRadius.circular(13)))),
  75. onPressed: press as void Function()?,
  76. child: Icon(icon),
  77. ),
  78. );
  79. }
  80. }