Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.58 KB | None | 0 0
  1. import 'dart:async';
  2.  
  3. import 'package:cached_network_image/cached_network_image.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_svg/svg.dart';
  6. import 'package:model_per_view/models/cart/item_cart.dart';
  7. import 'package:model_per_view/scoped_models/cart_view_model.dart';
  8. import 'package:model_per_view/scoped_models/product_detail_view_model.dart';
  9. import 'package:model_per_view/ui/views/base_view.dart';
  10. import 'package:model_per_view/ui/widgets/cart/cart_nodata_widget.dart';
  11. import 'package:model_per_view/ui/widgets/loading/loading.dart';
  12.  
  13. const String imageAsset = 'assets/img/2.jpg';
  14.  
  15. class CartView extends StatelessWidget {
  16. final GlobalKey<ScaffoldState> _scaffoldkey = new GlobalKey<ScaffoldState>();
  17.  
  18. @override
  19. Widget build(BuildContext context) {
  20. return BaseView<CartViewModel>(
  21. onModelReady: (model) => model.fetchProductCart(),
  22. builder: (context, child, model) {
  23. switch (model.state) {
  24. case ViewState.Busy:
  25. return Loading.getLoadingUi(context);
  26. case ViewState.NoDataAvailable:
  27. return CartNoDataWidget(context, "Your cart is empty");
  28. case ViewState.Error:
  29. return Loading.errorUi(context);
  30. case ViewState.DataFetched:
  31. default:
  32. model.sumPrice();
  33. return Scaffold(
  34. key: _scaffoldkey,
  35. appBar: PreferredSize(
  36. preferredSize: Size.fromHeight(65.0),
  37. child: AppBar(
  38. title: Padding(
  39. padding: const EdgeInsets.only(top: 35),
  40. child: Container(
  41. height: 33,
  42. width: double.infinity,
  43. child: Text(
  44. 'My Cart (${model.getCartQty().toString()})',
  45. style: TextStyle(fontSize: 16),
  46. ),
  47. ),
  48. ),
  49. leading: Padding(
  50. padding: const EdgeInsets.only(
  51. left: 14, top: 35, right: 15, bottom: 3),
  52. child: InkWell(
  53. onTap: () => Navigator.pop(context),
  54. child: SvgPicture.asset(
  55. 'assets/img/home/left-arrow.svg',
  56. width: 21,
  57. height: 13,
  58. ))),
  59. flexibleSpace: Container(
  60. decoration: new BoxDecoration(
  61. gradient: new LinearGradient(
  62. colors: [
  63. const Color(0xFF39C2C9),
  64. const Color(0xFF21DFC3),
  65. ],
  66. begin: const FractionalOffset(0.0, 0.0),
  67. end: const FractionalOffset(1.0, 0.0),
  68. stops: [0.0, 1.0],
  69. tileMode: TileMode.clamp),
  70. ),
  71. ),
  72. ),
  73. ),
  74. body: SafeArea(
  75. child: RefreshIndicator(
  76. onRefresh: () => onRefresh(model),
  77. child: ListView(
  78. children: <Widget>[
  79. Column(
  80. children: addWidget(context, model),
  81. ),
  82. Row(
  83. children: <Widget>[
  84. Expanded(
  85. child: Container(
  86. padding: EdgeInsets.only(left: 20),
  87. width: MediaQuery
  88. .of(context)
  89. .size
  90. .width,
  91. child: TextField(
  92. decoration: new InputDecoration.collapsed(
  93. hintText: 'Apply coupon'),
  94. ),
  95. ),
  96. flex: 7,
  97. ),
  98. Expanded(
  99. flex: 3,
  100. child: Container(
  101. margin: EdgeInsets.only(left: 30, right: 30),
  102. decoration: BoxDecoration(
  103. borderRadius: BorderRadius.circular(3),
  104. color: Colors.black),
  105. child: Container(
  106. margin: EdgeInsets.all(5),
  107. child: Center(
  108. child: Text(
  109. 'send'.toUpperCase(),
  110. style: TextStyle(color: Colors.white),
  111. ),
  112. ),
  113. ),
  114. ),
  115. )
  116. ],
  117. ),
  118. SizedBox(
  119. height: 10,
  120. )
  121. ],
  122. ),
  123. ),
  124. ),
  125. bottomNavigationBar: Container(
  126. padding: EdgeInsets.only(left: 10, right: 10, top: 5),
  127. decoration: BoxDecoration(
  128. color: Colors.white,
  129. border: Border(
  130. top:
  131. BorderSide(color: Colors.grey[300], width: 1.0))),
  132. child: Column(
  133. mainAxisSize: MainAxisSize.min,
  134. children: <Widget>[
  135. model.collapseCart
  136. ? Container(
  137. width: double.infinity,
  138. child: Stack(
  139. children: <Widget>[
  140. Container(
  141. width: double.infinity,
  142. child: Text(
  143. 'Order Summary',
  144. style: TextStyle(
  145. fontSize: 16,
  146. fontWeight: FontWeight.w400,
  147. color: Color(0xFF000000)),
  148. textAlign: TextAlign.center,
  149. )),
  150. Positioned(
  151. left: 0,
  152. child: GestureDetector(
  153. onTap: () {
  154. model.showDetailOrder();
  155. },
  156. child: Icon(
  157. Icons.clear,
  158. color: Color(0xFF4C4C4C),
  159. size: 14,
  160. ),
  161. ),
  162. ),
  163. ],
  164. ),
  165. )
  166. : Container(),
  167. model.collapseCart
  168. ? Column(
  169. mainAxisSize: MainAxisSize.min,
  170. children: _buildTotalPrice(
  171. model.getProductCart.items, model),
  172. )
  173. : Container(),
  174. Row(
  175. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  176. children: <Widget>[
  177. Flexible(
  178. flex: 4,
  179. child: Column(
  180. crossAxisAlignment: CrossAxisAlignment.start,
  181. children: <Widget>[
  182. InkWell(
  183. onTap: () {
  184. model.showDetailOrder();
  185. },
  186. child: Row(
  187. children: <Widget>[
  188. Text('Total'),
  189. SizedBox(
  190. width: 5,
  191. ),
  192. model.collapseCart
  193. ? SvgPicture.asset(
  194. 'assets/img/product/collapse-up.svg')
  195. : SvgPicture.asset(
  196. 'assets/img/product/collapse-down.svg'),
  197. ],
  198. ),
  199. ),
  200. model.getProductCart != null
  201. ? Text(
  202. 'USD \$${model.sum}00',
  203. style: TextStyle(
  204. fontWeight: FontWeight.w600,
  205. color: Colors.black,
  206. fontSize: 18.0,
  207. ),
  208. )
  209. : Text(
  210. 'USD \$0',
  211. style: TextStyle(
  212. fontWeight: FontWeight.w600,
  213. color: Colors.black,
  214. fontSize: 18.0,
  215. ),
  216. ),
  217. ],
  218. )),
  219. Flexible(
  220. flex: 6,
  221. child: buildCheckoutButton(model, context)),
  222. ],
  223. ),
  224. ],
  225. ),
  226. ),
  227. );
  228. }
  229. });
  230. }
  231.  
  232. Widget buildCheckoutButton(CartViewModel model, BuildContext context) {
  233. if (model.isProcessing) {
  234. return Column(
  235. children: <Widget>[
  236. GestureDetector(
  237. child: Container(
  238. height: 50,
  239. margin: EdgeInsets.only(top: 5, bottom: 5),
  240. padding: EdgeInsets.only(
  241. left: 10,
  242. right: 10,
  243. ),
  244. decoration: BoxDecoration(
  245. borderRadius: BorderRadius.circular(3),
  246. color: Color(0xFF000000),
  247. ),
  248. child: Center(
  249. child: Text(
  250. 'PROCESSING',
  251. style: TextStyle(color: Colors.white),
  252. )),
  253. ),
  254. ),
  255. ],
  256. );
  257. } else {
  258. return Column(
  259. children: <Widget>[
  260. GestureDetector(
  261. onTap: () async {
  262. model.processToCheckoutAction(context);
  263. },
  264. child: Container(
  265. height: 50,
  266. margin: EdgeInsets.only(top: 5, bottom: 5),
  267. padding: EdgeInsets.only(
  268. left: 10,
  269. right: 10,
  270. ),
  271. decoration: BoxDecoration(
  272. borderRadius: BorderRadius.circular(3),
  273. color: Color(0xFF000000),
  274. ),
  275. child: Center(
  276. child: Text(
  277. 'CHECK OUT',
  278. style: TextStyle(color: Colors.white),
  279. )),
  280. ),
  281. ),
  282. ],
  283. );
  284. }
  285. }
  286.  
  287. Future<Null> onRefresh(CartViewModel model) {
  288. final Completer<Null> completer = new Completer<Null>();
  289. new Timer(const Duration(seconds: 1), () {
  290. completer.complete(null);
  291. });
  292. return completer.future.then((_) {
  293. model.refreshProductCart();
  294. });
  295. }
  296.  
  297. addWidget(BuildContext context, CartViewModel model) {
  298. List<Widget> listCart = [];
  299.  
  300. for (var i = 0; i < model.getProductCart.items.length; i++) {
  301.  
  302. ItemCart itemCart = model.getProductCart.items[i];
  303. int itemId = itemCart.item_id;
  304. int qty = itemCart.qty;
  305. var k = 0;
  306.  
  307. listCart.add(Padding(
  308. padding: const EdgeInsets.all(8.0),
  309. child: Container(
  310. padding: const EdgeInsets.all(8.0),
  311. // constraints: BoxConstraints(minHeight: 150, maxHeight: 350),
  312. child: Column(
  313. mainAxisSize: MainAxisSize.min,
  314. crossAxisAlignment: CrossAxisAlignment.start,
  315. mainAxisAlignment: MainAxisAlignment.start,
  316. children: <Widget>[
  317. Divider(
  318. height: 1,
  319. color: Colors.black38,
  320. ),
  321. SizedBox(
  322. height: 10,
  323. ),
  324. Row(
  325. crossAxisAlignment: CrossAxisAlignment.start,
  326. mainAxisAlignment: MainAxisAlignment.start,
  327. children: <Widget>[
  328. Expanded(
  329. flex: 30,
  330. child: Container(
  331. child: CachedNetworkImage(
  332. imageUrl: model.getProductCart.items[i]
  333. .extension_attributes.thumbnail,
  334. fit: BoxFit.cover,
  335. placeholder: (context, url) =>
  336. Center(
  337. child: new CircularProgressIndicator(),
  338. ),
  339. errorWidget: (context, url, error) =>
  340. new Icon(Icons.error)),
  341. ),
  342. ),
  343. Expanded(
  344. flex: 70,
  345. child: Container(
  346. child: Stack(
  347. children: <Widget>[
  348. Padding(
  349. padding: const EdgeInsets.all(8.0),
  350. child: Column(
  351. crossAxisAlignment: CrossAxisAlignment.start,
  352. children: <Widget>[
  353. Text(
  354. '${model.getProductCart.items[i].sku}',
  355. style: TextStyle(
  356. fontSize: 14,
  357. color: Color(0xB3000000),
  358. fontWeight: FontWeight.w500),
  359. ),
  360. SizedBox(
  361. height: 5.0,
  362. ),
  363. Text(
  364. '${model.getProductCart.items[i].name}',
  365. style: TextStyle(
  366. color: Color(0xD9000000),
  367. fontWeight: FontWeight.w400,
  368. fontSize: 15.0),
  369. ),
  370. Text(
  371. "\$${model.getProductCart.items[i].price}",
  372. style: TextStyle(
  373. fontWeight: FontWeight.normal,
  374. fontSize: 14.0),
  375. ),
  376. // Text(
  377. // "\$0.00",
  378. // style: TextStyle(
  379. // fontWeight: FontWeight.normal,
  380. // color: Colors.grey,
  381. // fontSize: 14.0,
  382. // decoration: TextDecoration.lineThrough,
  383. // ),
  384. // ),
  385. // Container(
  386. // child: Column(
  387. // children:_buildItemOptionInCart(),
  388. // ),
  389. // ),
  390. // Container(
  391. // child: Wrap(
  392. // direction: Axis.horizontal,
  393. // alignment: WrapAlignment.center,
  394. // crossAxisAlignment:
  395. // WrapCrossAlignment.center,
  396. // children: <Widget>[
  397. // Container(
  398. // padding: EdgeInsets.only(right: 10),
  399. // child: SvgPicture.asset(
  400. // 'assets/img/product/gift-big-box.svg',
  401. // width: 12.5,
  402. // height: 20,
  403. // ),
  404. // ),
  405. // Text(
  406. // 'Send as a Gift',
  407. // style: TextStyle(
  408. // fontFamily: 'SF',
  409. // color: Colors.black,
  410. // fontSize: 12,
  411. // fontWeight: FontWeight.w400,
  412. // ),
  413. // ),
  414. // SizedBox(
  415. // width: 10,
  416. // ),
  417. // Icon(
  418. // Icons.arrow_forward_ios,
  419. // color: Color(0xFFC7C7CC),
  420. // size: 14,
  421. // )
  422. // ],
  423. // ),
  424. // ),
  425. ],
  426. ),
  427. ),
  428. Positioned(
  429. top: 10,
  430. right: 5,
  431. child: GestureDetector(
  432. onTap: () async => await model.remove(),
  433. child: Container(
  434. height: 25,
  435. width: 25,
  436. alignment: Alignment.center,
  437. child: Icon(
  438. Icons.clear,
  439. color: Colors.black87,
  440. size: 14,
  441. ),
  442. ),
  443. ),
  444. )
  445. ],
  446. ),
  447. ),
  448. ),
  449. ],
  450. ),
  451. SizedBox(
  452. height: 10,
  453. ),
  454. Row(
  455. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  456. children: <Widget>[
  457. _buildRowIncrease(
  458. context, model, model.getProductCart.items[i], i),
  459. GestureDetector(
  460. onTap: () async => await model.remove(),
  461. child: Container(
  462. padding:
  463. EdgeInsets.only(left: 5, right: 5, top: 5, bottom: 5),
  464. decoration: BoxDecoration(
  465. borderRadius: BorderRadius.circular(3),
  466. border: Border.all(color: Colors.grey)),
  467. child: Text(
  468. 'Remove',
  469. style: TextStyle(
  470. fontSize: 12,
  471. color: Color(0xFFB3000000),
  472. fontWeight: FontWeight.w400),
  473. ),
  474. ),
  475. )
  476. ],
  477. ),
  478. SizedBox(
  479. height: 10,
  480. ),
  481. // Divider(
  482. // height: 1,
  483. // color: Colors.black38,
  484. // ),
  485. // SizedBox(
  486. // height: 10,
  487. // ),
  488. // Column(
  489. // mainAxisSize: MainAxisSize.min,
  490. // children: _buildItemInListPrice(data),
  491. // ),
  492. // SizedBox(
  493. // height: 10,
  494. // ),
  495. // Divider(
  496. // height: 1,
  497. // color: Colors.black38,
  498. // ),
  499. ],
  500. ),
  501. ),
  502. ));
  503. }
  504. return listCart;
  505. }
  506.  
  507. Row _buildRowIncrease(BuildContext context, CartViewModel model,
  508. ItemCart item, int position) {
  509. return Row(
  510. mainAxisAlignment: MainAxisAlignment.center,
  511. children: <Widget>[
  512. Listener(
  513. onPointerDown: (details) {
  514. model.increaseCounterDownWhilePressed(position);
  515. },
  516. onPointerUp: (details) {
  517. model.stopIncrease();
  518. },
  519. child: Container(
  520. height: 25,
  521. padding: EdgeInsets.only(left: 5, right: 5, top: 5, bottom: 5),
  522. decoration: BoxDecoration(
  523. border: Border.all(color: Colors.grey),
  524. borderRadius: BorderRadius.only(
  525. topLeft: Radius.circular(5),
  526. bottomLeft: Radius.circular(5),
  527. )),
  528. child: Text(
  529. '-',
  530. style: TextStyle(
  531. color: Color(0xFFB3000000),
  532. fontSize: 12,
  533. fontWeight: FontWeight.w400),
  534. ),
  535. ),
  536. ),
  537. Container(
  538. width: 25,
  539. height: 25,
  540. padding: EdgeInsets.only(left: 5, right: 5, top: 5, bottom: 5),
  541. decoration: BoxDecoration(
  542. border: Border(
  543. top: BorderSide(color: Colors.grey),
  544. bottom: BorderSide(color: Colors.grey)),
  545. ),
  546. child: Text(
  547. '${model.listQty(position)}',
  548. softWrap: true,
  549. style: TextStyle(
  550. fontSize: 12,
  551. color: Color(0xFFB3000000),
  552. fontWeight: FontWeight.w400),
  553. textAlign: TextAlign.center,
  554. ),
  555. ),
  556. Listener(
  557. onPointerDown: (details) {
  558. model.increaseCounterWhilePressed(position);
  559. },
  560. onPointerUp: (details) {
  561. model.stopIncrease();
  562. },
  563. child: Container(
  564. height: 25,
  565. padding: EdgeInsets.only(left: 5, right: 5, top: 5, bottom: 5),
  566. decoration: BoxDecoration(
  567. border: Border.all(color: Colors.grey),
  568. borderRadius: BorderRadius.only(
  569. topRight: Radius.circular(5),
  570. bottomRight: Radius.circular(5),
  571. )),
  572. child: Text(
  573. '+',
  574. style: TextStyle(
  575. fontSize: 12,
  576. color: Color(0xFFB3000000),
  577. fontWeight: FontWeight.w400),
  578. ),
  579. ),
  580. ),
  581. SizedBox(
  582. width: 10,
  583. ),
  584. model.showUpdate ? Material(
  585. child: InkWell(
  586. onTap: () {
  587. model.addProductToCart(context, position);
  588. },
  589. child: Stack(
  590. alignment: Alignment.center,
  591. children: <Widget>[
  592. Container(
  593.  
  594. padding: EdgeInsets.only(
  595. left: 5, right: 5, top: 5, bottom: 5),
  596. decoration: BoxDecoration(
  597. borderRadius: BorderRadius.circular(3),
  598. border: Border.all(color: Colors.grey)),
  599. child: Text(
  600. 'Update',
  601. style: TextStyle(
  602. fontSize: 12,
  603. color: Colors.red,
  604. fontWeight: FontWeight.w400),
  605. ),
  606. ),
  607. Center(
  608. child: model.showUpdateProcessing ? SizedBox(
  609. width: 15,
  610. height: 15,
  611. child: Center(
  612. child: CircularProgressIndicator(),
  613. ),
  614. ) : Container(),
  615. )
  616. ],
  617. ),
  618. ),
  619. ) : Container()
  620. ],
  621. );
  622. }
  623.  
  624. BoxDecoration myBoxDecoration() {
  625. return BoxDecoration(
  626. border: Border(
  627. bottom: BorderSide(
  628. color: Colors.grey,
  629. width: 1.0,
  630. ),
  631. ),
  632. );
  633. }
  634.  
  635. _buildItemInListPrice(ItemCart data, CartViewModel model) {
  636. List<Widget> listDetail = [];
  637. listDetail.add(Row(
  638. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  639. children: <Widget>[
  640. Text(
  641. 'Subtotal',
  642. style: TextStyle(
  643. fontSize: 14,
  644. fontWeight: FontWeight.w400,
  645. color: Color(0xB3000000)),
  646. ),
  647. Text(
  648. 'USD \$${data.price * data.qty}',
  649. style: TextStyle(
  650. fontSize: 14,
  651. fontWeight: FontWeight.w400,
  652. color: Color(0xB3000000)),
  653. )
  654. ],
  655. ));
  656. listDetail.add(Row(
  657. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  658. children: <Widget>[
  659. Text(
  660. 'Shipping',
  661. style: TextStyle(
  662. fontSize: 14,
  663. fontWeight: FontWeight.w400,
  664. color: Color(0xB3000000)),
  665. ),
  666. Text(
  667. 'USD \$0.00',
  668. style: TextStyle(
  669. fontSize: 14,
  670. fontWeight: FontWeight.w400,
  671. color: Color(0xB3000000)),
  672. )
  673. ],
  674. ));
  675. listDetail.add(Row(
  676. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  677. children: <Widget>[
  678. Text(
  679. 'Tax:',
  680. style: TextStyle(
  681. fontSize: 14,
  682. fontWeight: FontWeight.w400,
  683. color: Color(0xB3000000)),
  684. ),
  685. Text(
  686. 'USD \$0.00',
  687. style: TextStyle(
  688. fontSize: 14,
  689. fontWeight: FontWeight.w400,
  690. color: Color(0xB3000000)),
  691. )
  692. ],
  693. ));
  694. listDetail.add(Row(
  695. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  696. children: <Widget>[
  697. Text(
  698. 'Total',
  699. style: TextStyle(
  700. fontSize: 14,
  701. fontWeight: FontWeight.w500,
  702. color: Color(0xFF000000)),
  703. ),
  704. Text(
  705. 'USD \$${data.price * data.qty}',
  706. style: TextStyle(
  707. fontSize: 14,
  708. fontWeight: FontWeight.w500,
  709. color: Color(0xFF000000)),
  710. )
  711. ],
  712. ));
  713. return listDetail;
  714. }
  715.  
  716. ///total
  717. _buildTotalPrice(List<ItemCart> items, CartViewModel model) {
  718. List<Widget> listDetail = [];
  719.  
  720. listDetail.add(Row(
  721. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  722. children: <Widget>[
  723. Text(
  724. 'Subtotal',
  725. style: TextStyle(
  726. fontSize: 14,
  727. fontWeight: FontWeight.w400,
  728. color: Color(0xB3000000)),
  729. ),
  730. Text(
  731. 'USD \$${model.sum}',
  732. style: TextStyle(
  733. fontSize: 14,
  734. fontWeight: FontWeight.w400,
  735. color: Color(0xB3000000)),
  736. )
  737. ],
  738. ));
  739. listDetail.add(Row(
  740. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  741. children: <Widget>[
  742. Text(
  743. 'Shipping',
  744. style: TextStyle(
  745. fontSize: 14,
  746. fontWeight: FontWeight.w400,
  747. color: Color(0xB3000000)),
  748. ),
  749. Text(
  750. 'USD \$0.00',
  751. style: TextStyle(
  752. fontSize: 14,
  753. fontWeight: FontWeight.w400,
  754. color: Color(0xB3000000)),
  755. )
  756. ],
  757. ));
  758. listDetail.add(Row(
  759. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  760. children: <Widget>[
  761. Text(
  762. 'Tax:',
  763. style: TextStyle(
  764. fontSize: 14,
  765. fontWeight: FontWeight.w400,
  766. color: Color(0xB3000000)),
  767. ),
  768. Text(
  769. 'USD \$0.00',
  770. style: TextStyle(
  771. fontSize: 14,
  772. fontWeight: FontWeight.w400,
  773. color: Color(0xB3000000)),
  774. )
  775. ],
  776. ));
  777. listDetail.add(Row(
  778. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  779. children: <Widget>[
  780. Text(
  781. 'Total',
  782. style: TextStyle(
  783. fontSize: 14,
  784. fontWeight: FontWeight.w500,
  785. color: Color(0xFF000000)),
  786. ),
  787. Text(
  788. 'USD \$${model.sum}00',
  789. style: TextStyle(
  790. fontSize: 14,
  791. fontWeight: FontWeight.w500,
  792. color: Color(0xFF000000)),
  793. )
  794. ],
  795. ));
  796. return listDetail;
  797. }
  798. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement