cart_counter.dart 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import 'package:flutter/material.dart';
  2. import 'package:fooddeliveryapp/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. numOfItems--;
  30. myTableDetail.removeItem(product);
  31. print("[通知]减少商品" +
  32. product.id.toString() +
  33. "NUMS:" +
  34. myTableDetail.getItemCount(product).toString());
  35. });
  36. }
  37. },
  38. ),
  39. Padding(
  40. padding: const EdgeInsets.symmetric(horizontal: kDefaultPaddin / 2),
  41. child: Text(
  42. // if our item is less then 10 then it shows 01 02 like that
  43. numOfItems.toString().padLeft(2, "0"),
  44. style: Theme.of(context).textTheme.headline6,
  45. ),
  46. ),
  47. buildOutlineButton(
  48. icon: Icons.add,
  49. press: () {
  50. setState(() {
  51. numOfItems++;
  52. myTableDetail.addItem(product);
  53. print("[通知]添加商品" +
  54. product.id.toString() +
  55. "NUMS:" +
  56. myTableDetail.getItemCount(product).toString());
  57. });
  58. }),
  59. ],
  60. );
  61. }
  62. SizedBox buildOutlineButton({IconData icon, Function press}) {
  63. return SizedBox(
  64. width: 40,
  65. height: 32,
  66. child: OutlineButton(
  67. padding: EdgeInsets.zero,
  68. shape: RoundedRectangleBorder(
  69. borderRadius: BorderRadius.circular(13),
  70. ),
  71. onPressed: press,
  72. child: Icon(icon),
  73. ),
  74. );
  75. }
  76. }