Advertisement
Guest User

Untitled

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