cart.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import 'dart:convert';
  2. import 'package:cached_network_image/cached_network_image.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:twong/api/index.dart';
  5. import 'package:twong/router/base.dart';
  6. import 'package:twong/utils/index.dart';
  7. import 'package:twong/config/style.dart';
  8. import 'package:twong/models/index.dart';
  9. import 'package:twong/widgets/app_bar.dart';
  10. import 'package:twong/widgets/circle_check_box.dart';
  11. import 'package:twong/widgets/num_counter.dart';
  12. class CartPage extends StatefulWidget {
  13. final bool show;
  14. CartPage({Key key, this.show = false}):super(key: key);
  15. @override
  16. State<StatefulWidget> createState() {
  17. return _CartPageState();
  18. }
  19. }
  20. class _CartPageState extends State<CartPage> {
  21. CartList _cartList;
  22. bool _editMode = false;
  23. bool _selectAll = false;
  24. @override
  25. void initState() {
  26. super.initState();
  27. loadData();
  28. }
  29. void loadData() {
  30. Network.inst.getCartList().then((data) {
  31. if(!mounted) return;
  32. setState(() {
  33. _cartList = data;
  34. });
  35. });
  36. }
  37. void _editClick () {
  38. setState(() {
  39. _editMode = !_editMode;
  40. });
  41. }
  42. Widget _buildBottom () {
  43. return Row(
  44. children: <Widget>[
  45. Expanded(child: Container(
  46. margin: EdgeInsets.only(left: 12.px, right: 12.px),
  47. child: CircleCheckBox((value) {
  48. _selectAll = value;
  49. }, child: Text("全选"), right: false)
  50. )),
  51. _editMode ? Spacer() : Container(
  52. margin: EdgeInsets.only(right: 8.px),
  53. child: Row(
  54. children: <Widget>[
  55. Text('总计:'),
  56. Text(Utils.formatRMB(6999, show: true),
  57. style: TextStyle(color: Colors.red, fontSize: 16.px)),
  58. ],
  59. ),
  60. ),
  61. Container(
  62. width: 106.px,
  63. margin: EdgeInsets.only(right: 10.px),
  64. child: _editMode ? OutlineButton(
  65. onPressed: () {},
  66. highlightedBorderColor: DColors.Main,
  67. child: Text('删除', style: TextStyle(color: DColors.Main)),
  68. borderSide: BorderSide(color: Colors.red, width: 0.5.px),
  69. shape: StadiumBorder()) : FlatButton(
  70. disabledColor: Colors.grey,
  71. color: DColors.Main,
  72. onPressed: () {
  73. Navigator.pushNamed(context, RouteNames.submitOrder);
  74. },
  75. child: Text('去结算', style: TextStyle(color: Colors.white)),
  76. shape: RoundedRectangleBorder(
  77. borderRadius: BorderRadius.circular(20.px)),
  78. ),
  79. )
  80. ],
  81. );
  82. }
  83. @override
  84. Widget build(BuildContext context) {
  85. return Scaffold(
  86. backgroundColor: Colors.white,
  87. appBar: DAppBar("购物车", autoLeading: widget.show, actions: <Widget>[
  88. GestureDetector(
  89. onTap: _editClick,
  90. child: Container(
  91. width: 60.px,
  92. alignment: Alignment.center,
  93. child: Text(_editMode ? '完成' : '编辑',
  94. textAlign: TextAlign.center,
  95. style: TextStyle(color: Colors.white)),
  96. ),
  97. )
  98. ]),
  99. bottomNavigationBar: SafeArea(
  100. child: Container(
  101. height: 50.px,
  102. color: Colors.white,
  103. child: _buildBottom(),
  104. ),
  105. ),
  106. body: Container(
  107. color: DColors.back,
  108. child: _cartList == null ? Center(child: CircularProgressIndicator())
  109. : ListView(
  110. physics: ClampingScrollPhysics(),
  111. children: [
  112. _buildCartList(),
  113. _buildInvalidList()
  114. ],
  115. ),
  116. ),
  117. );
  118. }
  119. Widget _buildCartList() {
  120. List<Widget> widgets = List<Widget>();
  121. for (var info in _cartList.valid) {
  122. Log.d(info.toJson());
  123. widgets.add(Container(
  124. margin: EdgeInsets.only(bottom: 6.px),
  125. padding: EdgeInsets.all(6.px),
  126. decoration: BoxDecoration(
  127. color: Colors.white,
  128. borderRadius: BorderRadius.circular(10.px)
  129. ),
  130. child: Row(
  131. children: [
  132. Image(
  133. height: 100.px, width: 100.px,
  134. image: CachedNetworkImageProvider(info.productInfo["image"])
  135. ),
  136. Expanded(child: Container(
  137. height: 100.px,
  138. margin: EdgeInsets.only(left: 12.px, right: 12.px),
  139. child: Column(
  140. crossAxisAlignment: CrossAxisAlignment.start,
  141. children: [
  142. Text(info.productInfo["store_name"], style: TextStyle(fontSize: 15.px)),
  143. Text(info.productInfo["attrInfo"]["suk"],
  144. style: TextStyle(color: Colors.grey, fontSize: 14.px)),
  145. Spacer(),
  146. Row(children: [
  147. Expanded(child: Text(Utils.formatRMB(info.truePrice, show: true),
  148. style: TextStyle(color: Colors.red, fontSize: 16.px))),
  149. NumCounter(num: info.cart_num, onChange: (value) {
  150. Log.d(info.toJson());
  151. Network.inst.cartNum(id: info.id, number: value);
  152. }, max: info.trueStock)
  153. ]),
  154. ])
  155. ))
  156. ],
  157. ),
  158. ));
  159. }
  160. return Container(
  161. width: 350.px,
  162. margin: EdgeInsets.all(12.px),
  163. child: Column(children: widgets)
  164. );
  165. }
  166. Widget _buildInvalidList() {
  167. List<Widget> widgets = List<Widget>();
  168. return Container(
  169. child: Column(children: widgets),
  170. );
  171. }
  172. }