goods_handler.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_note/provide/cart_count_provide.dart';
  4. import 'package:flutter_note/provide/cart_provide.dart';
  5. import 'package:flutter_note/provide/goods_detail_provide.dart';
  6. import 'package:flutter_note/provide/page_index_provide.dart';
  7. import 'package:provide/provide.dart';
  8. class GoodsHandler extends StatelessWidget {
  9. final GoodsDetailProvide detailProvide;
  10. GoodsHandler({Key key, this.detailProvide}) : super(key: key);
  11. Widget _countOperator(CartCountProvide cartCount) {
  12. return Row(
  13. crossAxisAlignment: CrossAxisAlignment.center,
  14. children: <Widget>[
  15. Expanded(
  16. child: InkWell(
  17. onTap:
  18. cartCount.shopCount == 1 ? () {} : () => cartCount.decrease(),
  19. child: DecoratedBox(
  20. decoration: BoxDecoration(
  21. border: Border(
  22. right: BorderSide(color: Colors.black54, width: 1.0)),
  23. ),
  24. child: Center(
  25. child: Text('-',
  26. style:
  27. TextStyle(fontSize: 16.0, color: Colors.black)))),
  28. ),
  29. ),
  30. Expanded(
  31. child: Center(
  32. child: Text('${cartCount.shopCount}',
  33. style: TextStyle(fontSize: 14.0, color: Colors.black))),
  34. flex: 2),
  35. Expanded(
  36. child: InkWell(
  37. onTap: () => cartCount.increase(),
  38. child: DecoratedBox(
  39. decoration: BoxDecoration(
  40. border: Border(
  41. left: BorderSide(color: Colors.black54, width: 1.0)),
  42. ),
  43. child: Center(
  44. child: Text('+',
  45. style:
  46. TextStyle(fontSize: 16.0, color: Colors.black)))),
  47. ),
  48. )
  49. ],
  50. );
  51. }
  52. _countModalSheet(BuildContext ctx) {
  53. Provide.value<CartCountProvide>(ctx).initCount(); // 购买数量初始化为 1
  54. showModalBottomSheet(
  55. context: ctx,
  56. builder: (context) =>
  57. Provide<CartCountProvide>(builder: (_, child, cartCount) {
  58. return Container(
  59. padding:
  60. const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
  61. height: 100.0,
  62. color: Colors.white,
  63. child: Column(
  64. crossAxisAlignment: CrossAxisAlignment.start,
  65. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  66. children: <Widget>[
  67. Text('购买数量',
  68. style: TextStyle(color: Colors.black, fontSize: 12.0)),
  69. Row(children: <Widget>[
  70. Container(
  71. width: 120.0,
  72. height: 30.0,
  73. decoration: ShapeDecoration(
  74. shape: RoundedRectangleBorder(
  75. borderRadius: BorderRadius.all(Radius.circular(2.0)),
  76. side: BorderSide(color: Colors.black54),
  77. ),
  78. ),
  79. child: _countOperator(cartCount),
  80. ),
  81. // 加入购物车按钮
  82. Padding(
  83. padding: const EdgeInsets.only(left: 30.0),
  84. child: OutlineButton(
  85. child: Text('确定加入购物车'),
  86. onPressed: () {
  87. Provide.value<CartProvide>(context).saveCarts(
  88. detailProvide.detail.data.goodInfo,
  89. cartCount.shopCount);
  90. Navigator.pop(context);
  91. }),
  92. )
  93. ])
  94. ],
  95. ),
  96. );
  97. }),
  98. );
  99. }
  100. @override
  101. Widget build(BuildContext context) {
  102. return BottomAppBar(
  103. child: Container(
  104. height: 60.0,
  105. child: Card(
  106. margin: const EdgeInsets.all(0.0),
  107. child: Row(children: <Widget>[
  108. // 购物车按钮
  109. Stack(
  110. alignment: Alignment.topRight,
  111. children: <Widget>[
  112. IconButton(
  113. icon: Icon(CupertinoIcons.shopping_cart,
  114. size: 32.0, color: Colors.pink),
  115. onPressed: () {
  116. Navigator.pop(context);
  117. Provide.value<PageIndexProvide>(context).changePage(2);
  118. }),
  119. Provide<CartProvide>(
  120. builder: (_, child, carts) => DecoratedBox(
  121. decoration: BoxDecoration(
  122. color: Colors.pink,
  123. borderRadius: BorderRadius.all(Radius.circular(12.0)),
  124. ),
  125. child: Padding(
  126. padding: const EdgeInsets.only(
  127. top: 2.0, bottom: 2.0, left: 4.0, right: 4.0),
  128. child: Text('${carts.allCartCount}',
  129. style: TextStyle(color: Colors.white)),
  130. ),
  131. ),
  132. )
  133. ],
  134. ),
  135. // 加入购物车
  136. Expanded(
  137. child: Builder(
  138. builder: (ctx) => InkWell(
  139. child: Container(
  140. color: Colors.green,
  141. alignment: Alignment.center,
  142. child: Text('加入购物车',
  143. style: TextStyle(
  144. color: Colors.white, fontSize: 18.0))),
  145. onTap: () {
  146. _countModalSheet(ctx);
  147. },
  148. ),
  149. ),
  150. ),
  151. // 立即购买
  152. Expanded(
  153. child: InkWell(
  154. child: Container(
  155. color: Colors.red,
  156. alignment: Alignment.center,
  157. child: Text('立即购买',
  158. style: TextStyle(color: Colors.white, fontSize: 18.0)),
  159. ),
  160. onTap: () {}),
  161. )
  162. ]),
  163. ),
  164. ),
  165. );
  166. }
  167. }