Files
Vocasia-LMS-Mobile-apps--TA…/lib/screens/checkout/checkout_detail_coupon.dart

392 lines
16 KiB
Dart

import 'package:flutter/material.dart';
import 'package:initial_folder/providers/total_price_provider.dart';
import 'package:initial_folder/screens/checkout/components/course_list.dart';
import 'package:initial_folder/screens/checkout/detail_zero_payment.dart';
import 'package:initial_folder/size_config.dart';
import 'package:initial_folder/theme.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:initial_folder/providers/order_provider.dart' as provOrder;
import '../../providers/payments_provider.dart';
import 'snap_payment_page.dart';
class CheckoutDetailCoupon extends StatefulWidget {
CheckoutDetailCoupon({
Key? key,
required this.idCart,
this.potonganKupon,
this.discountHarga,
this.isKupon,
}) : super(key: key);
final List<String> idCart;
final int? potonganKupon;
final int? discountHarga;
final bool? isKupon;
@override
State<CheckoutDetailCoupon> createState() => _CheckoutDetailCouponState();
}
class _CheckoutDetailCouponState extends State<CheckoutDetailCoupon> {
final controller = TextEditingController();
@override
Widget build(BuildContext context) {
final priceCoupon = Provider.of<TotalPriceProvider>(context).priceCoupon;
final totalPrice2 = Provider.of<TotalPriceProvider>(context).totalPrice;
final totalPrice3 = Provider.of<TotalPriceProvider>(context).totalPrices;
final typeCoupon = Provider.of<TotalPriceProvider>(context).typeCoupon;
final finalPriceCoupon =
Provider.of<TotalPriceProvider>(context).finalPriceCoupon;
final potonganKupon2 =
Provider.of<TotalPriceProvider>(context).potonganKupon;
Widget bottomNav(String totalPrice) {
String totalHarga =
"Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(totalPrice == '0' ? widget.discountHarga : double.parse(totalPrice))}";
return Container(
width: double.infinity,
height: getProportionateScreenHeight(64),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.background,
boxShadow: [
BoxShadow(
color: Theme.of(context).colorScheme.onPrimary.withOpacity(0.1),
spreadRadius: 4,
blurRadius: 15,
offset: Offset(0, 4),
),
],
),
child: Row(
children: [
SizedBox(width: 13),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: getProportionateScreenHeight(6)),
Text(
"Total",
style: thirdTextStyle.copyWith(
fontSize: getProportionateScreenHeight(12),
fontWeight: semiBold,
),
),
SizedBox(height: getProportionateScreenHeight(2)),
if (widget.isKupon == null)
Text(
'Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(finalPriceCoupon! < 50000 ? finalPriceCoupon + 5000 : finalPriceCoupon)}',
style: thirdTextStyle.copyWith(
fontSize: SizeConfig.blockVertical! * 2.5,
letterSpacing: 0.23,
),
),
if (widget.isKupon == true)
Text(
'Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(widget.discountHarga! < 50000 ? widget.discountHarga! + 5000 : widget.discountHarga)}',
style: thirdTextStyle.copyWith(
fontSize: SizeConfig.blockVertical! * 2.5,
letterSpacing: 0.23,
),
),
SizedBox(height: getProportionateScreenHeight(2)),
],
),
Spacer(),
GestureDetector(
onTap: () async {
print("Ini id cart? ${widget.idCart}");
if (totalHarga == 'Rp. 0') {
// Jika total harga adalah 0, arahkan ke halaman zero-payment
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => DetailZeroPayment(idCart: widget.idCart),
),
);
} else {
// Dapatkan total harga dan id transaksi untuk Snap Midtrans
String orderId = 'order-${DateTime.now().millisecondsSinceEpoch}';
int grossAmount = int.parse(
totalHarga.replaceAll('Rp. ', '').replaceAll(',', '')
);
// Persiapkan data_invoice berdasarkan kursus yang dibeli
List<Map<String, dynamic>> dataInvoice = [];
widget.idCart.forEach((cartId) {
dataInvoice.add({
'id_kursus': cartId, // Sesuaikan dengan data yang relevan
'title_kursus': 'Nama Kursus', // Nama kursus yang sesuai
'harga': grossAmount.toString(),
'qty': '1', // Sesuaikan jumlah jika diperlukan
});
});
// Panggil Snap melalui provider
final paymentProvider = Provider.of<PaymentsProvider>(context, listen: false);
Map<String, String>? paymentResponse = await paymentProvider.startSnapPayment(
orderId: orderId,
grossAmount: grossAmount,
dataInvoice: dataInvoice,
);
if (paymentResponse != null) {
// Snap berhasil dibuka, arahkan ke halaman WebView Snap
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => SnapPaymentPage(
transactionToken: paymentResponse[''] ?? '',
orderId: orderId,
grossAmount: grossAmount, courseTitle: '', courseThumbnail: '', courseInstructor: '', courseId: '',
),
),
);
} else {
// Jika Snap gagal dibuka
print("Pembayaran gagal");
}
}
},
child: Container(
margin: EdgeInsets.only(right: getProportionateScreenWidth(15)),
width: getProportionateScreenWidth(135),
height: getProportionateScreenHeight(35),
decoration: BoxDecoration(
color: primaryColor,
borderRadius: BorderRadius.circular(10),
),
child: Center(
child: Text(
'Pilih Pembayaran',
style: thirdTextStyle.copyWith(
fontSize: SizeConfig.blockVertical! * 1.7,
fontWeight: semiBold,
letterSpacing: 0.5,
color: baruTextutih,
),
),
),
),
)
],
),
);
}
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.background,
centerTitle: true,
title: Text(
'Checkout',
style: thirdTextStyle.copyWith(
letterSpacing: 1,
fontWeight: semiBold,
fontSize: getProportionateScreenWidth(14)),
),
),
body: SingleChildScrollView(
child: Container(
margin: EdgeInsets.symmetric(horizontal: getProportionateScreenWidth(16)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Detail Pesanan',
style: thirdTextStyle.copyWith(
letterSpacing: 1,
fontWeight: semiBold,
fontSize: getProportionateScreenWidth(14),
),
),
SizedBox(height: getProportionateScreenHeight(20)),
Consumer<provOrder.OrderProvider>(
builder: (context, state, _) {
return Column(
children: [
...state.orders.map(
(order) => CourseList(
idCourse: order.idCourse,
title: order.title,
price: widget.isKupon == null
? finalPriceCoupon.toString()
: (potonganKupon2 == 0
? finalPriceCoupon.toString()
: (potonganKupon2! > totalPrice2!
? finalPriceCoupon.toString()
: totalPrice3.toString())),
discountPrice: priceCoupon.toString(),
imageUrl: order.imageUrl,
),
),
],
);
},
),
SizedBox(height: getProportionateScreenHeight(5)),
Text(
'Rincian Harga',
style: thirdTextStyle.copyWith(
letterSpacing: 1,
fontWeight: semiBold,
fontSize: getProportionateScreenWidth(14),
),
),
SizedBox(height: getProportionateScreenHeight(10)),
Row(
children: [
Text(
'Subtotal Harga Kursus',
style: thirdTextStyle.copyWith(
fontWeight: reguler,
fontSize: getProportionateScreenWidth(10),
),
),
Spacer(),
if (widget.isKupon == null)
Text(
'Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(priceCoupon!)}',
style: thirdTextStyle.copyWith(
letterSpacing: 0.5,
fontWeight: reguler,
fontSize: getProportionateScreenWidth(12),
),
),
if (widget.isKupon == true)
Text(
'Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(finalPriceCoupon == 0 ? priceCoupon : (potonganKupon2! > totalPrice2! ? finalPriceCoupon : totalPrice3))}',
style: thirdTextStyle.copyWith(
letterSpacing: 0.5,
fontWeight: reguler,
fontSize: getProportionateScreenWidth(12),
),
),
],
),
SizedBox(height: getProportionateScreenHeight(5)),
Row(
children: [
Text(
'Biaya Layanan',
style: thirdTextStyle.copyWith(
fontWeight: reguler,
fontSize: getProportionateScreenWidth(10),
),
),
Spacer(),
Text(
'Rp ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(double.parse(finalPriceCoupon! < 5000 ? "5000" : "0"))}',
style: thirdTextStyle.copyWith(
fontWeight: reguler,
fontSize: getProportionateScreenWidth(12),
),
),
],
),
SizedBox(height: getProportionateScreenHeight(5)),
Row(
children: [
Text(
'Potongan Kupon',
style: primaryTextStyle.copyWith(
fontWeight: reguler,
fontSize: getProportionateScreenWidth(10),
),
),
Spacer(),
Text(
'Rp ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(double.parse(typeCoupon != "3" ? widget.potonganKupon.toString() : (priceCoupon! - finalPriceCoupon).toString()))}',
style: primaryTextStyle.copyWith(
fontWeight: reguler,
fontSize: getProportionateScreenWidth(12),
),
),
],
),
SizedBox(height: getProportionateScreenHeight(8)),
Row(
children: [
Text(
'Total Bayar',
style: thirdTextStyle.copyWith(
fontWeight: reguler,
fontSize: getProportionateScreenWidth(14),
),
),
Spacer(),
if (widget.isKupon == null)
Text(
'Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(finalPriceCoupon! < 50000 ? finalPriceCoupon + 5000 : finalPriceCoupon)}',
style: thirdTextStyle.copyWith(
fontWeight: semiBold,
fontSize: getProportionateScreenWidth(14),
),
),
if (widget.isKupon == true)
Text(
'Rp. ${NumberFormat.currency(locale: 'id', symbol: '', decimalDigits: 0).format(widget.discountHarga! < 50000 ? widget.discountHarga! + 5000 : widget.discountHarga)}',
style: thirdTextStyle.copyWith(
fontWeight: semiBold,
fontSize: getProportionateScreenWidth(14),
),
),
],
),
Row(
children: [
Text(''),
Spacer(),
if (widget.isKupon == null)
Text(
(int.parse(Provider.of<provOrder.OrderProvider>(context)
.totalPrice!) !=
0 &&
int.parse(
Provider.of<provOrder.OrderProvider>(context)
.totalPrice!) < 50000) ||
finalPriceCoupon! < 50000
? "+ admin Rp 5000"
: "",
style: secondaryTextStyle.copyWith(
letterSpacing: 1,
fontWeight: reguler,
fontSize: getProportionateScreenWidth(10),
),
),
if (widget.isKupon != null)
Text(
(widget.discountHarga != 0 &&
widget.discountHarga! < 50000)
? "+ admin Rp 5000"
: "",
style: secondaryTextStyle.copyWith(
letterSpacing: 1,
fontWeight: reguler,
fontSize: getProportionateScreenWidth(10),
),
),
],
),
],
),
),
),
bottomNavigationBar: bottomNav(
(int.parse(Provider.of<provOrder.OrderProvider>(context).totalPrice
as String))
.toString(),
),
);
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
}