Untitled
raw download clone
DART
views 11
,
size 20788 b
import 'dart:convert';

import 'package:eccomerce_amandewi/screen/payment/payment_with.dart';
import 'package:eccomerce_amandewi/screen/model/model_product.dart';
import 'package:eccomerce_amandewi/screen/model/model_product_ticket.dart';
import 'package:eccomerce_amandewi/screen/themes/colors.dart';
import 'package:eccomerce_amandewi/screen/themes/fonts.dart';
import 'package:eccomerce_amandewi/screen/util/api.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:toast/toast.dart';

class BookingDestinasi extends StatefulWidget {
  Map<String, dynamic> idWisata;

  BookingDestinasi({Key key, this.idWisata});

  @override
  _BookingDestinasiState createState() => _BookingDestinasiState();
}

class _BookingDestinasiState extends State<BookingDestinasi> {
  int transHeadID, userID;
  String transHeadBill, userToken;

  getAll() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    if (!mounted) return;
    setState(() {
      transHeadID = preferences.getInt("headId");
      transHeadBill = preferences.getString("headBillNo");
      userToken = preferences.getString("userToken");
      userID = preferences.getInt("userId");
      _getHeaderProduct();
    });
  }

  int _counterTicket = 0;
  int _counterWisata = 0;
  int sumTotalTicket = 0;
  int sumTotalWahana = 0;
  int grandTotal = 0;
  List holder_1 = [];

  var order_adr = <Map>[];

  void counterPlus() {
    setState(() {
      _counterTicket++;
    });
  }

  void counterMinus() {
    setState(() {
      if (_counterTicket > 0) {
        _counterTicket--;
      } else {
        _counterTicket = 0;
      }
    });
  }

  void counterPlusWisata() {
    setState(() {
      _counterWisata++;
    });
  }

  void counterMinusWisata() {
    setState(() {
      if (_counterWisata > 0) {
        _counterWisata--;
      } else {
        _counterWisata = 0;
      }
    });
  }

  var eurosInUSFormat = new NumberFormat.currency(
    locale: "eu",
    symbol: "",
    decimalDigits: 0,
  );

  @override
  void initState() {
    super.initState();
    getAll();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Color(0XFFFF9254),
          centerTitle: true,
          title: Text("Opsi Pemesanan",
              style: GoogleFonts.poppins(
                  fontWeight: FontWeight.w400,
                  color: Colors.white,
                  fontSize: 20)),
        ),
        bottomNavigationBar: Container(
          margin: EdgeInsets.symmetric(horizontal: 20.0, vertical: 15.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    "Total Harga",
                    style: GoogleFonts.poppins(
                        fontWeight: FontWeight.w500,
                        color: Colors.black,
                        fontSize: 10),
                  ),
                  Row(
                    children: [
                      Text(
                        "wahana : ",
                        style: GoogleFonts.poppins(
                            fontWeight: FontWeight.w500,
                            color: Colors.black,
                            fontSize: 14),
                      ),
                      Text(
                        "Rp $grandTotal",
                        style: GoogleFonts.poppins(
                            fontWeight: FontWeight.w500,
                            color: Color(0XFFFF9254),
                            fontSize: 14),
                      ),
                    ],
                  ),
                  Text(
                    "+2000 point",
                    style: GoogleFonts.poppins(
                        fontWeight: FontWeight.w500,
                        color: Colors.black,
                        fontSize: 9),
                  ),
                ],
              ),
              Container(
                // margin: EdgeInsets.symmetric(horizontal: 20.0, vertical: 15.0),
                padding: EdgeInsets.symmetric(vertical: 8.0),
                height: 40,
                width: 150,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(20.0),
                  color: Color(0XFFFF9254),
                ),
                child: InkWell(
                  onTap: () {
                    print(holder_1);
                    print(grandTotal);
                    setState(() {
                      for (int index = 0;
                          index < _listProductWisata.length;
                          index++) {
                        order_adr.add({
                          "id_products": _listProductWisata[index].idProduct,
                          "id_header_trans": transHeadID,
                          "bill_no": ('"' + transHeadBill + '"').toString(),
                          "qty": _listProductWisata[index].qty,
                          "price_total":
                              _listProductWisata[index].priceProduct *
                                  _listProductWisata[index].qty,
                          "price": _listProductWisata[index].priceProduct,
                          "price_wisata": 0,
                          "price_app": 0,
                          "price_maintenance": 0,
                          "price_tax": 0,
                          "price_payment": 0,
                          "id_status": 1,
                          "author_id": userID,
                        });
                      }
                    });
                    setState(() {
                      _transaction();
                    });
                    print(order_adr);

                    // Navigator.push(
                    //   context,
                    //   MaterialPageRoute(
                    //     builder: (_) => BookingDestinasi(
                    //       idWisata: {
                    //         'wisataID': widget.wisata.idWisata.toString(),
                    //       },
                    //     ),
                    //   ),
                    // );
                  },
                  child: Text(
                    "Pesan Sekarang",
                    style: GoogleFonts.poppins(
                        fontWeight: FontWeight.w500,
                        color: Colors.white,
                        fontSize: 14),
                    textAlign: TextAlign.center,
                  ),
                ),
              ),
            ],
          ),
        ),
        body: Container(
          color: Colors.black12,
          height: double.infinity,
          child: Column(
            children: [
              Container(
                margin: EdgeInsets.symmetric(vertical: 5.0, horizontal: 20.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      "Opsi Wahana",
                      style: GoogleFonts.poppins(
                          fontWeight: FontWeight.w500,
                          color: Colors.black,
                          fontSize: 14),
                    ),
                    SizedBox(height: 10.0),
                    Container(
                      // padding: EdgeInsets.symmetric(horizontal: 10.0),
                      width: MediaQuery.of(context).size.width,
                      height: 420,
                      child: new ListView.builder(
                        scrollDirection: Axis.vertical,
                        itemCount: _listProductWisata == null
                            ? 0
                            : _listProductWisata.length,
                        itemBuilder: (context, index) {
                          return _rowHeaderProductListWisata(index);
                        },
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _rowHeaderProductListWisata(index) {
    void counterPlusWisata() {
      setState(() {
        _listProductWisata[index].qty++;
        holder_1.add(_listProductWisata[index].priceProduct);

        int sum = 0;
        for (int i = 0; i < holder_1.length; i++) {
          sum += holder_1[i];
        }
        setState(() {
          grandTotal = sum;
        });
      });
    }

    void counterMinusWisata() {
      setState(() {
        if (_listProductWisata[index].qty > 0) {
          _listProductWisata[index].qty--;
          holder_1.remove(_listProductWisata[index].priceProduct);
          int sum = 0;
          for (int i = 0; i < holder_1.length; i++) {
            sum += holder_1[i];
          }
          setState(() {
            grandTotal = sum;
          });
        } else {
          _listProductWisata[index].qty = 0;
          holder_1.remove(_listProductWisata[index].priceProduct);
          int sum = 0;
          for (int i = 0; i < holder_1.length; i++) {
            sum += holder_1[i];
          }
          setState(() {
            grandTotal = sum;
          });
        }
      });
    }

    return Card(
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(20),
          color: Colors.white,
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // Container(
            //   // decoration: BoxDecoration(
            //   //   borderRadius: BorderRadius.only(
            //   //     topLeft: Radius.circular(20.0),
            //   //     bottomLeft: Radius.circular(20.0),
            //   //   ),
            //   // ),
            //   // padding: EdgeInsets.symmetric(
            //   //     vertical: 20, horizontal: 30),
            //   color: Color(0XFFFF9254),
            //   width: 320,
            //   height: 130,
            // ),
            _listProductWisata[index].picProduct == null
                ? Container(
                    margin: EdgeInsets.symmetric(vertical: 10.0),
                    padding: EdgeInsets.symmetric(vertical: 20, horizontal: 30),
                    color: Colors.grey[300],
                    height: 150,
                  )
                : Container(
                    width: double.infinity,
                    height: 170,
                    child: ClipRRect(
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(20.0),
                        topRight: Radius.circular(20.0),
                        bottomLeft: Radius.zero,
                        bottomRight: Radius.zero,
                      ),
                      child: ColorFiltered(
                        colorFilter: new ColorFilter.mode(
                            Colors.black.withOpacity(0.3), BlendMode.darken),
                        child: Image.network(
                          "http://amandewi.com/public/images/product/${_listProductWisata[index].picProduct}" ==
                                  null
                              ? Container()
                              : "http://amandewi.com/public/images/product/${_listProductWisata[index].picProduct}",
                          fit: BoxFit.cover,
                          colorBlendMode: BlendMode.multiply,
                        ),
                      ),
                    ),
                  ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      width: 180,
                      // alignment: Alignment.bottomCenter,
                      padding: EdgeInsets.symmetric(horizontal: 15.0),
                      child: Text(
                        "${_listProductWisata[index].titleProduct == null ? "-" : _listProductWisata[index].titleProduct}",
                        style: ThemeFonts.textStyle400.copyWith(
                            fontSize: 15,
                            color: Colors.black,
                            decoration: TextDecoration.none),
                      ),
                    ),
                    SizedBox(height: 5.0),
                    Container(
                      width: 180,
                      padding: EdgeInsets.symmetric(horizontal: 15.0),
                      child: Text(
                        "Rp ${eurosInUSFormat.format(_listProductWisata[index].priceProduct)}",
                        style: ThemeFonts.textStyle400.copyWith(
                            fontSize: 13,
                            color: Colors.black,
                            decoration: TextDecoration.none),
                      ),
                    ),
                  ],
                ),
                Container(
                  child: Row(
                    children: [
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 10.0),
                        child: InkWell(
                          child: Icon(Icons.remove_circle_outline_rounded,
                              size: 25, color: Colors.grey),
                          onTap: () {
                            counterMinusWisata();
                            if (_listProductWisata[index].qty == 1) {
                              setState(() {
                                sumTotalWahana =
                                    _listProductWisata[index].priceProduct;
                                // holder_1.add(_listProductWisata[index]
                                //     .priceProduct);
                              });
                            } else {
                              setState(() {
                                sumTotalWahana =
                                    _listProductWisata[index].priceProduct *
                                        _listProductWisata[index].qty;
                                // holder_1.add(_listProductWisata[index]
                                //     .priceProduct);
                              });
                            }
                          },
                        ),
                      ),
                      SizedBox(width: 5),
                      Container(
                        child: Text(
                          "${_listProductWisata[index].qty}",
                          style: ThemeFonts.textStyle700
                              .copyWith(fontSize: 15, color: Colors.black),
                        ),
                      ),
                      SizedBox(width: 5),
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 10.0),
                        child: InkWell(
                          child: Icon(Icons.add_circle_outline_rounded,
                              size: 25, color: Color(0XFFFF9254)),
                          onTap: () {
                            counterPlusWisata();
                            if (_listProductWisata[index].qty == 1) {
                              setState(() {
                                sumTotalWahana =
                                    _listProductWisata[index].priceProduct;
                                // holder_1.remove(_listProductWisata[index]
                                //     .priceProduct);
                              });
                            } else {
                              setState(() {
                                sumTotalWahana =
                                    _listProductWisata[index].priceProduct *
                                        _listProductWisata[index].qty;
                                // holder_1.remove(_listProductWisata[index]
                                //     .priceProduct);
                              });
                            }
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  List<ProductWisata> _listProductWisata = [];

  // List<ProductWisataTicket> _listProductWisataTicket = [];

  Future<List<ProductWisata>> _getHeaderProduct() async {
    print("All Wisata");
    try {
      // _showLoading("Harap Tunggu");
      String nomor = widget.idWisata['wisataID'];
      // String nomor = "1";
      return http
          .get(await Env().getProductObjekWisata(nomor))
          .then((response) {
        print("Response");
        final body = jsonDecode(response.body);
        _listProductWisata.clear();
        print(response.body);

        if (body["status_code"] == "01") {
          for (var i = 0; i < body['Data'].length; i++) {
            setState(() {
              ProductWisata allProduct =
                  ProductWisata.fromJson(body['Data'][i]);
              _listProductWisata.add(allProduct);
            });
          }
        } else {
          setState(() {
            _listProductWisata.clear();
          });
          // Navigator.pop(context);
          showDialog(
            barrierDismissible: false,
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text("Error get Data"),
                // content: Text(body['data']),
                actions: <Widget>[
                  new RaisedButton(
                    child: Text("Ok"),
                    textColor: Colors.grey,
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  ),
                ],
              );
            },
          );
        }
      }).catchError((error) {
        print(error);
      });
    } catch (e) {
      print(e);
    }
    return null;
  }

  Future<Null> _transaction() async {
    print("Login");
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      return http.post(
        await Env().postTransaction(),
        body: jsonEncode({
          "id_Header": transHeadID,
          "status": "Sent Up",
          "data": order_adr,
        }),
        headers: {'Authorization': 'Bearer $userToken'},
      ).then((response) {
        print(response.body);

        final body = jsonDecode(response.body);
        print(body);

        if (body["status_code"] == "01") {
          setState(() {
            Navigator.pushReplacement(
              context,
              MaterialPageRoute(
                builder: (context) => PaymentWith(),
              ),
            );
          });
        } else {
          setState(() {
            // Navigator.pop(context);
          });
          showDialog(
            barrierDismissible: false,
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text(
                  body['error'],
                  style: ThemeFonts.textStyle500
                      .copyWith(fontSize: 16, color: Colors.black),
                ),
                // content: Text(body['data']),
                actions: <Widget>[
                  new RaisedButton(
                    child: Text('Ok'),
                    textColor: Colors.grey,
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  ),
                ],
              );
            },
          );
        }
      }).catchError((error) {
        print(error);
      });
    } catch (e) {
      print(e);
    }
    return null;
  }
}
close fullscreen
Login or Register to edit or fork this paste. It's free.