Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:cached_network_image/cached_network_image.dart';
- import 'package:carousel_slider/carousel_slider.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/widgets.dart';
- import 'package:intl/intl.dart';
- import 'package:pasar_online/components/add.dart';
- import 'package:pasar_online/components/models/auth.dart';
- import 'package:pasar_online/components/models/auth_provider.dart';
- import 'package:pasar_online/components/models/product_detail_model.dart';
- import 'package:pasar_online/provider/provider_latlong.dart';
- import 'package:pasar_online/ui/page_keranjang.dart';
- import 'package:pasar_online/ui/page_login.dart';
- import 'package:provider/provider.dart';
- import 'package:sweet_alert_dialogs/sweet_alert_dialogs.dart';
- enum ShoppingAction { ContinueShopping, ViewCart }
- class ProductDetail extends StatefulWidget {
- ProductDetail({Key key, this.productId}) : super(key: key);
- final int productId;
- _ProductDetailState createState() => _ProductDetailState();
- }
- class _ProductDetailState extends State<ProductDetail> {
- bool _isPromo = false;
- ProductDetailModel _productDetailModel;
- List<ProductDetailModel> _relatedProducts;
- final formatter = new NumberFormat("#,###", "id_ID");
- Future<ShoppingAction> _showDialog(BuildContext context) async {
- ShoppingAction _answer = await showDialog(
- barrierDismissible: false,
- context: context,
- builder: (BuildContext context) {
- return RichAlertDialog(
- alertTitle: richTitle("Berhasil!"),
- alertSubtitle: richSubtitle("Pesanan telah berhasil ditambahkan"),
- alertType: RichAlertType.SUCCESS,
- actions: <Widget>[
- FlatButton(
- child: Text(
- "BAYAR",
- style: TextStyle(fontWeight: FontWeight.bold),
- ),
- onPressed: () {
- Navigator.pop(context, ShoppingAction.ViewCart);
- },
- ),
- FlatButton(
- child: Text(
- "LANJUT BELANJA",
- style: TextStyle(fontWeight: FontWeight.bold),
- ),
- onPressed: () {
- Navigator.pop(context, ShoppingAction.ContinueShopping);
- },
- ),
- ],
- );
- });
- return _answer;
- }
- // void getProductDetailById() async {
- // // nantinya ditambahkan untuk auth nya
- // final appState = Provider.of<AppState>(context);
- // // TODO : Auth
- // final ProductDetailController _pdc =
- // ProductDetailController(productId: widget.productId);
- // final ProductDetailModel _pdm =
- // await _pdc.getProductDetailModel(appState.getLang, appState.getLong);
- // if (_pdm != null) {
- // setState(() {
- // _productDetailModel = _pdm;
- // });
- // }
- // }
- void getProductDetailWithSeller() async {
- // nantinya ditambahkan untuk authnya
- final appState = Provider.of<AppState>(context);
- // TODO : Auth
- final ProductDetailController _pdc = ProductDetailController(productId: widget.productId);
- final ProductDetailModel _pdm = await _pdc.getProductDetailModel(appState.getLang, appState.getLong);
- final List<ProductDetailModel> _pdms = await _pdc .getRelatedProductDetailModels(appState.getLang, appState.getLong);
- if (_pdms != null) {
- setState(() {
- _productDetailModel = _pdm;
- _relatedProducts = _pdms;
- });
- }
- }
- // void getRelatedProducts() async {
- // final appState = Provider.of<AppState>(context);
- // // if (auth), ditambahkan authnya
- // // TODO : Auth
- // final ProductDetailController _pdc = ProductDetailController(productId: widget.productId);
- // final List<ProductDetailModel> _pdms = await _pdc .getRelatedProductDetailModels(appState.getLang, appState.getLong);
- // if (_pdms != null) {
- // setState(() {
- // _relatedProducts = _pdms;
- // });
- // }
- // }
- @override
- void didChangeDependencies() {
- // TODO: implement didChangeDependencies
- super.didChangeDependencies();
- getProductDetailWithSeller();
- // getRelatedProducts();
- }
- @override
- Widget build(BuildContext context) {
- if (_productDetailModel == null || _relatedProducts == null) {
- // nanti diganti dengan loading yang benar
- return Scaffold(
- body: Container(
- child: Center(
- child: CircularProgressIndicator(),
- ),
- ),
- );
- }
- return Scaffold(
- appBar: AppBar(
- title: Text(
- _productDetailModel.productName,
- style: TextStyle(color: Colors.white),
- ),
- iconTheme: Theme.of(context).iconTheme.copyWith(color: Colors.white),
- ),
- body: ListView(
- padding: EdgeInsets.only(left: 10.0, right: 10.0),
- children: <Widget>[
- Container(
- width: double.infinity,
- height: 300.0,
- // decoration: BoxDecoration(
- // border: Border.all(style: BorderStyle.solid),
- // ),
- child: Stack(
- children: <Widget>[
- Positioned(
- left: 0.0,
- top: 0.0,
- right: 0.0,
- bottom: 0.0,
- child: CarouselSlider(
- height: 350.0,
- enableInfiniteScroll: false,
- viewportFraction: 1.0,
- items: List<Widget>.from(_productDetailModel.images
- .map((img) => CachedNetworkImage(
- imageUrl: img.imageBig,
- fit: BoxFit.cover,
- ))),
- ))
- ],
- )),
- Container(
- padding: EdgeInsets.only(top: 5.0, bottom: 5.0),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: <Widget>[
- Container(
- width: 250.0,
- padding: EdgeInsets.only(bottom: 5.0),
- child: Text(
- _productDetailModel.productName,
- style: TextStyle(
- fontSize: 18.0,
- ),
- overflow: TextOverflow.fade,
- ),
- ),
- Container(
- padding: EdgeInsets.all(5.0),
- height: 32.0,
- decoration: BoxDecoration(
- // border: Border.all(style: BorderStyle.solid),
- borderRadius: BorderRadius.all(Radius.circular(8.0)),
- color: _productDetailModel.stock != null &&
- _productDetailModel.stock > 0
- ? Colors.lightGreen
- : Colors.grey[300]),
- child: Row(
- children: <Widget>[
- _productDetailModel.stock != null &&
- _productDetailModel.stock > 0
- ? Text(
- "In Stock",
- style: TextStyle(color: Colors.white),
- )
- : Text(
- "Out of Stock",
- style: TextStyle(color: Colors.black
- // backgroundColor: Colors.grey[300],
- ),
- ),
- // Icon(Icons.info_outline)
- ],
- ),
- )
- ],
- ),
- ),
- // (_productDetailModel.discount != null && _productDetailModel.discount != "") ?
- // Column(
- // mainAxisAlignment: MainAxisAlignment.start,
- // crossAxisAlignment: CrossAxisAlignment.start,
- // children: <Widget>[
- // Text("Rp. ${formatter.format(_productDetailModel.price)}", style: TextStyle(decoration: TextDecoration.lineThrough)),
- // Text("Rp. " + formatter.format(_productDetailModel.price - (_productDetailModel.price * double.tryParse(_productDetailModel.discount.replaceAll("%", "")) /100)).toString(),
- // style: TextStyle(
- // fontSize: 22.0,
- // fontWeight: FontWeight.bold,
- // ),
- // ),
- // ],
- // ) : Text("Rp. ${_productDetailModel.price}",
- // style: TextStyle(
- // fontSize: 22.0,
- // fontWeight: FontWeight.bold,
- // ),
- // ),
- Padding(
- padding: EdgeInsets.only(top: 5.0, bottom: 5.0),
- child: Text(
- "Minimal Order: ${_productDetailModel.minimumOrder}",
- style: TextStyle(color: Colors.orange),
- ),
- ),
- Builder(
- builder: (BuildContext ctx) {
- if (_productDetailModel.sellers != null) {
- List<Widget> _rows = [];
- for (int i = 0; i < _productDetailModel.sellers.length; i++) {
- _rows.add(Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: <Widget>[
- SizedBox(
- width: 120.0,
- child: Text(
- "${_productDetailModel.sellers[i].fullName}",
- overflow: TextOverflow.fade,
- ),
- ),
- (_productDetailModel.sellers[i].discount != null &&
- _productDetailModel.sellers[i].discount != "")
- ? Column(
- mainAxisAlignment: MainAxisAlignment.start,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- Text(
- "Rp. ${formatter.format(_productDetailModel.sellers[i].price)}",
- style: TextStyle(
- decoration:
- TextDecoration.lineThrough)),
- Text(
- "Rp. " +
- formatter
- .format(_productDetailModel
- .sellers[i].price -
- (_productDetailModel
- .sellers[i].price *
- double.tryParse(
- _productDetailModel
- .sellers[i].discount
- .replaceAll(
- "%", "")) /
- 100))
- .toString(),
- style: TextStyle(
- fontSize: 16.0,
- fontWeight: FontWeight.bold,
- ),
- ),
- ],
- )
- : Text(
- "Rp. ${_productDetailModel.sellers[i].price}",
- style: TextStyle(
- fontSize: 16.0,
- fontWeight: FontWeight.bold,
- ),
- ),
- AddButton(
- onPressed: (Map<String, dynamic> direction) {
- final BaseAuth _auth = AuthProvider.of(context).auth;
- _auth.isLoggedIn().then((_isLoggedIn) {
- if (_isLoggedIn) {
- // add to cart
- final ProductDetailController _pdc =
- ProductDetailController(
- isAuthenciated: true,
- productId:
- _productDetailModel.sellers[i].id,
- secret: _auth.currentUser.secret);
- _pdc.addtoCart().then((onData) {
- print("data yang masuk adalah: $onData");
- if (onData) {
- final Future<ShoppingAction> _dialog =
- _showDialog(context);
- _dialog.then((ShoppingAction onAnswer) {
- if (onAnswer == ShoppingAction.ViewCart) {
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) =>
- KeranjangPage()));
- }
- });
- }
- });
- } else {
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => LoginPage()));
- }
- });
- },
- )
- ],
- ));
- }
- return Column(
- children: _rows,
- );
- }
- },
- ),
- ListTile(
- leading: Icon(Icons.info_outline),
- title: Text(
- "Gratis ongkos kirim hanya berlaku untuk daerah dengan jangkauan maksimal 5 km dari pasar."),
- ),
- Text(
- "Deskripsi",
- style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18.0),
- ),
- Padding(
- padding: EdgeInsets.only(left: 10.0),
- child: Text(_productDetailModel.descriptions),
- ),
- Text(
- "Informasi Pemesanan",
- style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18.0),
- ),
- Text("Untuk informasi pemesanan hubungi kami di:"),
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- mainAxisSize: MainAxisSize.min,
- children: <Widget>[
- Expanded(
- child: FlatButton(
- child: Column(
- mainAxisSize: MainAxisSize.min,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- Image.asset(
- "assets/contact_chat.png",
- fit: BoxFit.fitHeight,
- height: 28.0,
- ),
- Text("Pesan")
- ],
- ),
- onPressed: () {},
- ),
- ),
- Expanded(
- child: FlatButton(
- child: Column(
- children: <Widget>[
- Image.asset(
- "assets/contact_phone.png",
- fit: BoxFit.fitHeight,
- height: 28.0,
- ),
- Text("Telp")
- ],
- ),
- onPressed: () {},
- ),
- ),
- FlatButton(
- child: Column(
- children: <Widget>[
- Image.asset(
- "assets/contact_whatsapp.png",
- fit: BoxFit.fitHeight,
- height: 28.0,
- ),
- Text("Whatsapp")
- ],
- ),
- onPressed: () {},
- ),
- FlatButton(
- child: Column(
- children: <Widget>[
- Image.asset(
- "assets/contact_message.png",
- fit: BoxFit.fitHeight,
- height: 28.0,
- ),
- Text("Email")
- ],
- ),
- onPressed: () {},
- ),
- ],
- ),
- Padding(
- padding: EdgeInsets.only(
- top: 10.0,
- ),
- child: Text(
- "Produk Terkait",
- style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18.0),
- ),
- ),
- Builder(
- builder: (BuildContext context) {
- int _columnWidth = 2;
- List<Widget> _data = [];
- List<Widget> _tempRow = [];
- for (int i = 0; i < _relatedProducts.length; i++) {
- if (i % _columnWidth == 0) {
- // print("Jumlah data: ${_tempRow.length}");
- // print("data ke: $i");
- _tempRow = [];
- } else {
- _data.add(Row(
- mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- mainAxisSize: MainAxisSize.max,
- children: _tempRow,
- ));
- }
- _tempRow.add(InkWell(
- child: Card(
- child: Container(
- width: 150.0,
- height: 170.0,
- child: Column(
- children: <Widget>[
- _relatedProducts[i].productImage == null
- ? Image.asset(
- "assets/picture.png",
- fit: BoxFit.cover,
- width: 100.0,
- height: 100.0,
- )
- : CachedNetworkImage(
- placeholder: (context, url) =>
- CircularProgressIndicator(),
- imageUrl: _relatedProducts[i]
- .productImage
- .imageThumb,
- ),
- Text(_relatedProducts[i].productName),
- (_relatedProducts[i].discount != null &&
- _relatedProducts[i].discount != "")
- ? Column(
- mainAxisAlignment:
- MainAxisAlignment.start,
- crossAxisAlignment:
- CrossAxisAlignment.start,
- children: <Widget>[
- Text(
- "Rp. ${formatter.format(_relatedProducts[i].priceDiscount)}",
- style: TextStyle(
- decoration: TextDecoration
- .lineThrough)),
- Text(
- "Rp. " +
- formatter
- .format(_relatedProducts[i]
- .priceDiscount -
- (_relatedProducts[i]
- .priceDiscount *
- double.tryParse(
- _relatedProducts[
- i]
- .discount
- .replaceAll(
- "%",
- "")) /
- 100))
- .toString(),
- style: TextStyle(
- fontWeight: FontWeight.bold,
- ),
- ),
- ],
- )
- : Text(
- "Rp. ${formatter.format(_relatedProducts[i].price)}",
- style: TextStyle(
- fontWeight: FontWeight.bold,
- ),
- ),
- ],
- ))),
- onTap: () {
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => ProductDetail(
- productId: _relatedProducts[i].id)));
- },
- ));
- }
- return Column(
- children: _data,
- );
- },
- )
- ],
- ),
- // bottomNavigationBar: BottomAppBar(
- // child: Container(
- // height: 50.0,
- // child: FlatButton(
- // color: Colors.orange,
- // child: Text(
- // "PESAN SEKARANG",
- // style: TextStyle(
- // color: Colors.white,
- // fontSize: 20.0,
- // ),
- // ),
- // onPressed: () {
- // final BaseAuth _auth = AuthProvider.of(context).auth;
- // if (_auth.currentUser == null) {
- // Navigator.push(context,
- // MaterialPageRoute(builder: (context) => LoginPage()));
- // }
- // // add to cart
- // final ProductDetailController _pdc = ProductDetailController(
- // isAuthenciated: true,
- // productId: widget.productId,
- // secret: _auth.currentUser.secret);
- // _pdc.addtoCart().then((onData) {
- // print(onData);
- // if (onData) {
- // final Future<ShoppingAction> _dialog = _showDialog(context);
- // _dialog.then((ShoppingAction onAnswer) {
- // if (onAnswer == ShoppingAction.ViewCart) {
- // Navigator.push(
- // context,
- // MaterialPageRoute(
- // builder: (context) => KeranjangPage()));
- // }
- // });
- // }
- // });
- // },
- // ),
- // ),
- // ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement