Advertisement
Guest User

Untitled

a guest
Sep 10th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 16.25 KB | None | 0 0
  1. import 'package:cached_network_image/cached_network_image.dart';
  2. import 'package:carousel_slider/carousel_slider.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/widgets.dart';
  5. import 'package:intl/intl.dart';
  6. import 'package:pasar_online/components/add.dart';
  7. import 'package:pasar_online/components/models/auth.dart';
  8. import 'package:pasar_online/components/models/auth_provider.dart';
  9. import 'package:pasar_online/components/models/product_detail_model.dart';
  10. import 'package:pasar_online/provider/provider_latlong.dart';
  11. import 'package:pasar_online/ui/page_keranjang.dart';
  12. import 'package:pasar_online/ui/page_login.dart';
  13. import 'package:provider/provider.dart';
  14. import 'package:sweet_alert_dialogs/sweet_alert_dialogs.dart';
  15.  
  16. enum ShoppingAction { ContinueShopping, ViewCart }
  17.  
  18. class ProductDetail extends StatefulWidget {
  19.   ProductDetail({Key key, this.productId}) : super(key: key);
  20.  
  21.   final int productId;
  22.  
  23.   _ProductDetailState createState() => _ProductDetailState();
  24. }
  25.  
  26. class _ProductDetailState extends State<ProductDetail> {
  27.   bool _isPromo = false;
  28.   ProductDetailModel _productDetailModel;
  29.   List<ProductDetailModel> _relatedProducts;
  30.   final formatter = new NumberFormat("#,###", "id_ID");
  31.  
  32.   Future<ShoppingAction> _showDialog(BuildContext context) async {
  33.     ShoppingAction _answer = await showDialog(
  34.         barrierDismissible: false,
  35.         context: context,
  36.         builder: (BuildContext context) {
  37.           return RichAlertDialog(
  38.             alertTitle: richTitle("Berhasil!"),
  39.             alertSubtitle: richSubtitle("Pesanan telah berhasil ditambahkan"),
  40.             alertType: RichAlertType.SUCCESS,
  41.             actions: <Widget>[
  42.               FlatButton(
  43.                 child: Text(
  44.                   "BAYAR",
  45.                   style: TextStyle(fontWeight: FontWeight.bold),
  46.                 ),
  47.                 onPressed: () {
  48.                   Navigator.pop(context, ShoppingAction.ViewCart);
  49.                 },
  50.               ),
  51.               FlatButton(
  52.                 child: Text(
  53.                   "LANJUT BELANJA",
  54.                   style: TextStyle(fontWeight: FontWeight.bold),
  55.                 ),
  56.                 onPressed: () {
  57.                   Navigator.pop(context, ShoppingAction.ContinueShopping);
  58.                 },
  59.               ),
  60.             ],
  61.           );
  62.         });
  63.     return _answer;
  64.   }
  65.  
  66.   // void getProductDetailById() async {
  67.   //   // nantinya ditambahkan untuk auth nya
  68.   //   final appState = Provider.of<AppState>(context);
  69.   //   // TODO : Auth
  70.   //   final ProductDetailController _pdc =
  71.   //       ProductDetailController(productId: widget.productId);
  72.   //   final ProductDetailModel _pdm =
  73.   //       await _pdc.getProductDetailModel(appState.getLang, appState.getLong);
  74.   //   if (_pdm != null) {
  75.   //     setState(() {
  76.   //       _productDetailModel = _pdm;
  77.   //     });
  78.   //   }
  79.   // }
  80.  
  81.   void getProductDetailWithSeller()  {
  82.     // nantinya ditambahkan untuk authnya
  83.     final appState = Provider.of<AppState>(context);
  84.     // TODO : Auth
  85.     final ProductDetailController _pdc = ProductDetailController(productId: widget.productId);
  86.     _pdc.getProductDetailModel(appState.getLang, appState.getLong).then((_pdm){
  87.       _pdc .getRelatedProductDetailModels(appState.getLang, appState.getLong).then((_pdms){
  88.         setState(() {
  89.           _productDetailModel = _pdm;
  90.           _relatedProducts = _pdms;
  91.         });
  92.       });
  93.     });
  94.  
  95. //    if (_pdms != null) {
  96. //      setState(() {
  97. //        _productDetailModel = _pdm;
  98. //        _relatedProducts = _pdms;
  99. //      });
  100. //    }
  101.   }
  102.  
  103.   // void getRelatedProducts() async {
  104.   //   final appState = Provider.of<AppState>(context);
  105.   //   // if (auth), ditambahkan authnya
  106.   //   // TODO : Auth
  107.   //   final ProductDetailController _pdc = ProductDetailController(productId: widget.productId);
  108.   //   final List<ProductDetailModel> _pdms = await _pdc .getRelatedProductDetailModels(appState.getLang, appState.getLong);
  109.   //   if (_pdms != null) {
  110.   //     setState(() {
  111.   //       _relatedProducts = _pdms;
  112.   //     });
  113.   //   }
  114.   // }
  115.  
  116.   @override
  117.   void didChangeDependencies() {
  118.     // TODO: implement didChangeDependencies
  119.     super.didChangeDependencies();
  120.     getProductDetailWithSeller();
  121.     // getRelatedProducts();
  122.   }
  123.  
  124.   @override
  125.   Widget build(BuildContext context) {
  126.     if (_productDetailModel == null || _relatedProducts == null) {
  127.       // nanti diganti dengan loading yang benar
  128.       return Scaffold(
  129.         body: Container(
  130.           child: Center(
  131.             child: CircularProgressIndicator(),
  132.           ),
  133.         ),
  134.       );
  135.     }
  136.     return Scaffold(
  137.         appBar: AppBar(
  138.           title: Text(
  139.             _productDetailModel.productName,
  140.             style: TextStyle(color: Colors.white),
  141.           ),
  142.           iconTheme: Theme.of(context).iconTheme.copyWith(color: Colors.white),
  143.         ),
  144.         body: ListView(
  145.             padding: EdgeInsets.only(left: 10.0, right: 10.0),
  146.             children: <Widget>[
  147.         Container(
  148.         width: double.infinity,
  149.             height: 300.0,
  150.             // decoration: BoxDecoration(
  151.             //   border: Border.all(style: BorderStyle.solid),
  152.             // ),
  153.             child: Stack(
  154.               children: <Widget>[
  155.               Positioned(
  156.               left: 0.0,
  157.               top: 0.0,
  158.               right: 0.0,
  159.               bottom: 0.0,
  160.               child: CarouselSlider(
  161.                   height: 350.0,
  162.                   enableInfiniteScroll: false,
  163.                   viewportFraction: 1.0,
  164.                   items: List<Widget>.from(_productDetailModel.images
  165.                   .map((img) => CachedNetworkImage(
  166.               imageUrl: img.imageBig,
  167.               fit: BoxFit.cover,
  168.             ))),
  169.     ))
  170.     ],
  171.     )),
  172.     Container(
  173.     padding: EdgeInsets.only(top: 5.0, bottom: 5.0),
  174.     child: Row(
  175.     mainAxisAlignment: MainAxisAlignment.spaceBetween,
  176.     children: <Widget>[
  177.     Container(
  178.     width: 250.0,
  179.     padding: EdgeInsets.only(bottom: 5.0),
  180.     child: Text(
  181.     _productDetailModel.productName,
  182.     style: TextStyle(
  183.     fontSize: 18.0,
  184.     ),
  185.     overflow: TextOverflow.fade,
  186.     ),
  187.     ),
  188.     Container(
  189.     padding: EdgeInsets.all(5.0),
  190.     height: 32.0,
  191.     decoration: BoxDecoration(
  192.     // border: Border.all(style: BorderStyle.solid),
  193.     borderRadius: BorderRadius.all(Radius.circular(8.0)),
  194.     color: _productDetailModel.stock != null &&
  195.     _productDetailModel.stock > 0
  196.     ? Colors.lightGreen
  197.         : Colors.grey[300]),
  198.     child: Row(
  199.     children: <Widget>[
  200.     _productDetailModel.stock != null &&
  201.     _productDetailModel.stock > 0
  202.     ? Text(
  203.     "In Stock",
  204.     style: TextStyle(color: Colors.white),
  205.     )
  206.         : Text(
  207.     "Out of Stock",
  208.     style: TextStyle(color: Colors.black
  209.     // backgroundColor: Colors.grey[300],
  210.     ),
  211.     ),
  212.     // Icon(Icons.info_outline)
  213.     ],
  214.     ),
  215.     )
  216.     ],
  217.     ),
  218.     ),
  219.     // (_productDetailModel.discount != null && _productDetailModel.discount != "") ?
  220.     // Column(
  221.     //   mainAxisAlignment: MainAxisAlignment.start,
  222.     //   crossAxisAlignment: CrossAxisAlignment.start,
  223.     //   children: <Widget>[
  224.     //     Text("Rp. ${formatter.format(_productDetailModel.price)}", style: TextStyle(decoration: TextDecoration.lineThrough)),
  225.     //     Text("Rp. " + formatter.format(_productDetailModel.price - (_productDetailModel.price * double.tryParse(_productDetailModel.discount.replaceAll("%", "")) /100)).toString(),
  226.     //       style: TextStyle(
  227.     //         fontSize: 22.0,
  228.     //         fontWeight: FontWeight.bold,
  229.     //       ),
  230.     //     ),
  231.     //   ],
  232.     // ) : Text("Rp. ${_productDetailModel.price}",
  233.     //   style: TextStyle(
  234.     //     fontSize: 22.0,
  235.     //     fontWeight: FontWeight.bold,
  236.     //   ),
  237.     // ),
  238.     Padding(
  239.     padding: EdgeInsets.only(top: 5.0, bottom: 5.0),
  240.     child: Text(
  241.     "Minimal Order: ${_productDetailModel.minimumOrder}",
  242.     style: TextStyle(color: Colors.orange),
  243.     ),
  244.     ),
  245.     Builder(
  246.     builder: (BuildContext ctx) {
  247.     if (_productDetailModel.sellers != null) {
  248.     List<Widget> _rows = [];
  249.     for (int i = 0; i < _productDetailModel.sellers.length; i++) {
  250.     _rows.add(Row(
  251.     mainAxisAlignment: MainAxisAlignment.spaceBetween,
  252.     children: <Widget>[
  253.     SizedBox(
  254.     width: 120.0,
  255.     child: Text(
  256.     "${_productDetailModel.sellers[i].fullName}",
  257.     overflow: TextOverflow.fade,
  258.     ),
  259.     ),
  260.     (_productDetailModel.sellers[i].discount != null &&
  261.     _productDetailModel.sellers[i].discount != "")
  262.     ? Column(
  263.     mainAxisAlignment: MainAxisAlignment.start,
  264.     crossAxisAlignment: CrossAxisAlignment.start,
  265.     children: <Widget>[
  266.     Text(
  267.     "Rp. ${formatter.format(_productDetailModel.sellers[i].price)}",
  268.     style: TextStyle(
  269.     decoration:
  270.     TextDecoration.lineThrough)),
  271.     Text(
  272.     "Rp. " +
  273.     formatter
  274.         .format(_productDetailModel
  275.         .sellers[i].price -
  276.     (_productDetailModel
  277.         .sellers[i].price *
  278.     double.tryParse(
  279.     _productDetailModel
  280.         .sellers[i].discount
  281.         .replaceAll(
  282.     "%", "")) /
  283.     100))
  284.         .toString(),
  285.     style: TextStyle(
  286.     fontSize: 16.0,
  287.     fontWeight: FontWeight.bold,
  288.     ),
  289.     ),
  290.     ],
  291.     )
  292.         : Text(
  293.     "Rp. ${_productDetailModel.sellers[i].price}",
  294.     style: TextStyle(
  295.     fontSize: 16.0,
  296.     fontWeight: FontWeight.bold,
  297.     ),
  298.     ),
  299.     AddButton(
  300.     onPressed: (Map<String, dynamic> direction) {
  301.     final BaseAuth _auth = AuthProvider.of(context).auth;
  302.     _auth.isLoggedIn().then((_isLoggedIn) {
  303.     if (_isLoggedIn) {
  304.     // add to cart
  305.     final ProductDetailController _pdc =
  306.     ProductDetailController(
  307.     isAuthenciated: true,
  308.     productId:
  309.     _productDetailModel.sellers[i].id,
  310.     secret: _auth.currentUser.secret);
  311.     _pdc.addtoCart().then((onData) {
  312.     print("data yang masuk adalah: $onData");
  313.     if (onData) {
  314.     final Future<ShoppingAction> _dialog =
  315.     _showDialog(context);
  316.     _dialog.then((ShoppingAction onAnswer) {
  317.     if (onAnswer == ShoppingAction.ViewCart) {
  318.     Navigator.push(
  319.     context,
  320.     MaterialPageRoute(
  321.     builder: (context) =>
  322.     KeranjangPage()));
  323.     }
  324.     });
  325.     }
  326.     });
  327.     } else {
  328.     Navigator.push(
  329.     context,
  330.     MaterialPageRoute(
  331.     builder: (context) => LoginPage()));
  332.     }
  333.     });
  334.     },
  335.     )
  336.     ],
  337.     ));
  338.     }
  339.     return Column(
  340.     children: _rows,
  341.     );
  342.     }else{
  343.       return Container(
  344.       width: 0.0,
  345.       height: 0.0,
  346.       );
  347.     }
  348.     },
  349.     ),
  350.     ListTile(
  351.     leading: Icon(Icons.info_outline),
  352.     title: Text(
  353.     "Gratis ongkos kirim hanya berlaku untuk daerah dengan jangkauan maksimal 5 km dari pasar."),
  354.     ),
  355.     Text(
  356.     "Deskripsi",
  357.     style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18.0),
  358.     ),
  359.     Padding(
  360.     padding: EdgeInsets.only(left: 10.0),
  361.     child: Text(_productDetailModel.descriptions),
  362.     ),
  363.     Text(
  364.     "Informasi Pemesanan",
  365.     style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18.0),
  366.     ),
  367.     Text("Untuk informasi pemesanan hubungi kami di:"),
  368.     Row(
  369.     mainAxisAlignment: MainAxisAlignment.center,
  370.     mainAxisSize: MainAxisSize.min,
  371.     children: <Widget>[
  372.     Expanded(
  373.     child: FlatButton(
  374.     child: Column(
  375.     mainAxisSize: MainAxisSize.min,
  376.     crossAxisAlignment: CrossAxisAlignment.start,
  377.     children: <Widget>[
  378.     Image.asset(
  379.     "assets/contact_chat.png",
  380.     fit: BoxFit.fitHeight,
  381.     height: 28.0,
  382.     ),
  383.     Text("Pesan")
  384.     ],
  385.     ),
  386.     onPressed: () {},
  387.     ),
  388.     ),
  389.     Expanded(
  390.     child: FlatButton(
  391.     child: Column(
  392.     children: <Widget>[
  393.     Image.asset(
  394.     "assets/contact_phone.png",
  395.     fit: BoxFit.fitHeight,
  396.     height: 28.0,
  397.     ),
  398.     Text("Telp")
  399.     ],
  400.     ),
  401.     onPressed: () {},
  402.     ),
  403.     ),
  404.     FlatButton(
  405.     child: Column(
  406.     children: <Widget>[
  407.     Image.asset(
  408.     "assets/contact_whatsapp.png",
  409.     fit: BoxFit.fitHeight,
  410.     height: 28.0,
  411.     ),
  412.     Text("Whatsapp")
  413.     ],
  414.     ),
  415.     onPressed: () {},
  416.     ),
  417.     FlatButton(
  418.     child: Column(
  419.     children: <Widget>[
  420.     Image.asset(
  421.     "assets/contact_message.png",
  422.     fit: BoxFit.fitHeight,
  423.     height: 28.0,
  424.     ),
  425.     Text("Email")
  426.     ],
  427.     ),
  428.     onPressed: () {},
  429.     ),
  430.     ],
  431.     ),
  432.     Padding(
  433.     padding: EdgeInsets.only(
  434.     top: 10.0,
  435.     ),
  436.     child: Text(
  437.     "Produk Terkait",
  438.     style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18.0),
  439.     ),
  440.     ),
  441.     Builder(
  442.     builder: (BuildContext context) {
  443.     int _columnWidth = 2;
  444.     List<Widget> _data = [];
  445.     List<Widget> _tempRow = [];
  446.     for (int i = 0; i < _relatedProducts.length; i++) {
  447.     if (i % _columnWidth == 0) {
  448.     // print("Jumlah data: ${_tempRow.length}");
  449.     // print("data ke: $i");
  450.     _tempRow = [];
  451.     } else {
  452.     _data.add(Row(
  453.     mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  454.     mainAxisSize: MainAxisSize.max,
  455.     children: _tempRow,
  456.     ));
  457.     }
  458.     _tempRow.add(InkWell(
  459.     child: Card(
  460.     child: Container(
  461.     width: 150.0,
  462.     height: 170.0,
  463.     child: Column(
  464.     children: <Widget>[
  465.     _relatedProducts[i].productImage == null
  466.     ? Image.asset(
  467.     "assets/picture.png",
  468.     fit: BoxFit.cover,
  469.     width: 100.0,
  470.     height: 100.0,
  471.     )
  472.         : CachedNetworkImage(
  473.     placeholder: (context, url) =>
  474.     CircularProgressIndicator(),
  475.     imageUrl: _relatedProducts[i]
  476.         .productImage
  477.         .imageThumb,
  478.     ),
  479.     Text(_relatedProducts[i].productName),
  480.     (_relatedProducts[i].discount != null &&
  481.     _relatedProducts[i].discount != "")
  482.     ? Column(
  483.     mainAxisAlignment:
  484.     MainAxisAlignment.start,
  485.     crossAxisAlignment:
  486.     CrossAxisAlignment.start,
  487.     children: <Widget>[
  488.     Text(
  489.     "Rp. ${formatter.format(_relatedProducts[i].priceDiscount)}",
  490.     style: TextStyle(
  491.     decoration: TextDecoration
  492.         .lineThrough)),
  493.     Text(
  494.     "Rp. " +
  495.     formatter
  496.         .format(_relatedProducts[i]
  497.         .priceDiscount -
  498.     (_relatedProducts[i]
  499.         .priceDiscount *
  500.     double.tryParse(
  501.     _relatedProducts[
  502.     i]
  503.         .discount
  504.         .replaceAll(
  505.     "%",
  506.     "")) /
  507.     100))
  508.         .toString(),
  509.     style: TextStyle(
  510.     fontWeight: FontWeight.bold,
  511.     ),
  512.     ),
  513.     ],
  514.     )
  515.         : Text(
  516.     "Rp. ${formatter.format(_relatedProducts[i].price)}",
  517.     style: TextStyle(
  518.     fontWeight: FontWeight.bold,
  519.     ),
  520.     ),
  521.     ],
  522.     ))),
  523.     onTap: () {
  524.     Navigator.push(
  525.     context,
  526.     MaterialPageRoute(
  527.     builder: (context) => ProductDetail(
  528.     productId: _relatedProducts[i].id)));
  529.     },
  530.     ));
  531.     }
  532.     return Column(
  533.     children: _data,
  534.     );
  535.     },
  536.     )
  537.     ],
  538.     ),
  539.     // bottomNavigationBar: BottomAppBar(
  540.     //   child: Container(
  541.     //     height: 50.0,
  542.     //     child: FlatButton(
  543.     //       color: Colors.orange,
  544.     //       child: Text(
  545.     //         "PESAN SEKARANG",
  546.     //         style: TextStyle(
  547.     //           color: Colors.white,
  548.     //           fontSize: 20.0,
  549.     //         ),
  550.     //       ),
  551.     //       onPressed: () {
  552.     //         final BaseAuth _auth = AuthProvider.of(context).auth;
  553.     //         if (_auth.currentUser == null) {
  554.     //           Navigator.push(context,
  555.     //               MaterialPageRoute(builder: (context) => LoginPage()));
  556.     //         }
  557.     //         // add to cart
  558.     //         final ProductDetailController _pdc = ProductDetailController(
  559.     //             isAuthenciated: true,
  560.     //             productId: widget.productId,
  561.     //             secret: _auth.currentUser.secret);
  562.     //         _pdc.addtoCart().then((onData) {
  563.     //           print(onData);
  564.     //           if (onData) {
  565.     //             final Future<ShoppingAction> _dialog = _showDialog(context);
  566.     //             _dialog.then((ShoppingAction onAnswer) {
  567.     //               if (onAnswer == ShoppingAction.ViewCart) {
  568.     //                 Navigator.push(
  569.     //                     context,
  570.     //                     MaterialPageRoute(
  571.     //                         builder: (context) => KeranjangPage()));
  572.     //               }
  573.     //             });
  574.     //           }
  575.     //         });
  576.     //       },
  577.     //     ),
  578.     //   ),
  579.     // ),
  580.     );
  581.   }
  582. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement