cart_counter.dart 2.4 KB

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