TKK_E32210585_aplikasi/lib/ui/detail_order.dart

358 lines
20 KiB
Dart

// ignore_for_file: camel_case_types, prefer_typing_uninitialized_variables
import 'dart:convert';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:e_commerce/const/AppColors.dart';
import 'package:e_commerce/const/rupiah.dart';
import 'package:e_commerce/ui/midtrans_pay_screen.dart';
import 'package:e_commerce/widgets/fetch_cart.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:http/http.dart' as http;
class detail_order extends StatefulWidget {
const detail_order({Key? key}) : super(key: key);
@override
State<detail_order> createState() => _detail_orderState();
}
class _detail_orderState extends State<detail_order> {
bool isCod = true;
void payWithMidtrans(QuerySnapshot data, int totalPrice) async {
try {
String orderId = DateTime.now().millisecondsSinceEpoch.toString();
Map<String, dynamic> param = {
'transaction_details': {
'order_id': orderId,
'gross_amount': totalPrice,
},
'credit_card': {
'secure': true,
}
};
final response = await http.post(Uri.parse("https://pojok-ti-be.vercel.app/create/snap"), headers: {'Content-Type': 'application/json'}, body: jsonEncode(param));
final token = response.body;
print(token);
Navigator.push(context, CupertinoPageRoute(builder: (_) => MidtransPay(token: token, snapshot: data)));
} catch (e) {
debugPrint("Error Cuy !!!");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Detail Order"),
actions: const [],
),
body: Container(
padding: const EdgeInsets.all(10),
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
),
child: SingleChildScrollView(
// physics: const NeverScrollableScrollPhysics(),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(
width: 60,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
color: Color(0xFFBEBEBE),
),
height: 6,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
// height: MediaQuery.of(context).size.width * 1.28,
decoration: BoxDecoration(
color: Colors.white, // warna putihW
borderRadius: BorderRadius.circular(10),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 2,
blurRadius: 3,
offset: Offset(0, 2),
),
], // rounded border
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
StreamBuilder(
stream: FirebaseFirestore.instance.collection("users-cart-items").doc(FirebaseAuth.instance.currentUser!.email).collection("items").snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return const Center(
child: Text("Something is wrong"),
);
}
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
if (!snapshot.hasData || snapshot.data!.docs.isEmpty) {
return const Center(child: Text("No items in the cart"));
}
final docs = snapshot.data!.docs;
int totalPrice = 0;
docs.forEach((doc) {
final price = int.tryParse(doc['price']) ?? 0;
totalPrice += price;
});
Future<void>? _launched;
final total = totalPrice ?? 0;
// String orderMessage = createOrderMessage(snapshot.data!, totalPrice);
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
SizedBox(
height: MediaQuery.of(context).size.height * 0.50,
// color: Colors.red,
child: ListView.builder(
itemCount: snapshot.data == null ? 0 : snapshot.data!.docs.length,
itemBuilder: (_, index) {
DocumentSnapshot _documentSnapshot = snapshot.data!.docs[index];
final rupiah = int.parse(_documentSnapshot['price']) ?? 0;
return Padding(
padding: const EdgeInsets.only(bottom: 20.0),
child: Container(
color: Colors.white,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(
width: 10.0,
),
Container(
width: MediaQuery.of(context).size.width * 0.61, // Misalnya lebar maksimal setengah dari lebar layar
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
width: MediaQuery.of(context).size.width * 0.40,
height: MediaQuery.of(context).size.height * 0.05,
child: Row(
children: [
Text(
"1x",
style: GoogleFonts.beVietnamPro(
textStyle: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
),
),
),
const SizedBox(
width: 6.0,
),
Flexible(
child: Text(
_documentSnapshot['name'],
style: GoogleFonts.beVietnamPro(
textStyle: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
),
),
),
)
],
),
),
Text(
"${rupiah.toRupiah()}",
style: GoogleFonts.beVietnamPro(
textStyle: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
),
),
),
],
),
),
],
),
),
);
}),
),
Divider(
thickness: 1.5,
indent: 8,
endIndent: 8,
),
Container(
// height:
// MediaQuery.of(context).size.height * 0.13,
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Sub. Total",
style: GoogleFonts.beVietnamPro(
textStyle: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
),
)),
Text(
"${total.toRupiah()}",
style: GoogleFonts.beVietnamPro(
textStyle: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.normal,
),
),
),
],
),
const SizedBox(
height: 5.0,
),
const SizedBox(
height: 5.0,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Pembayaran",
style: GoogleFonts.beVietnamPro(
textStyle: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Cash/Tunai",
style: GoogleFonts.beVietnamPro(
textStyle: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
),
),
),
Radio(
visualDensity: VisualDensity.compact,
splashRadius: 0,
value: true,
groupValue: isCod,
onChanged: (v) {
isCod = v!;
setState(() {});
},
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Instant",
style: GoogleFonts.beVietnamPro(
textStyle: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
),
),
),
Radio(
visualDensity: VisualDensity.compact,
splashRadius: 0,
value: false,
groupValue: isCod,
onChanged: (v) {
isCod = v!;
setState(() {});
},
),
],
),
],
),
],
),
),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: Size(250, 30),
backgroundColor: AppColors.deep_orange,
shape: ContinuousRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
),
onPressed: () {
if (isCod) {
showOrderDialog(context, snapshot.data!, totalPrice);
} else {
payWithMidtrans(snapshot.data!, totalPrice);
}
},
child: Text(
"Order",
style: GoogleFonts.beVietnamPro(
textStyle: const TextStyle(
color: Colors.white,
fontSize: 15,
fontWeight: FontWeight.w400,
),
),
),
)
],
),
);
},
),
],
),
),
)
],
),
),
],
),
)),
);
}
}