Checkoutscreen
Checkoutscreen
dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:HDTech/Provider/CartProvider.dart';
@override
Widget build(BuildContext context) {
final cartProvider = Provider.of<CartProvider>(context, listen: false);
return FutureBuilder(
future: cartProvider.fetchCheckoutDetails(cartId), // Gọi API lấ
y dữ liệu giỏ
hàng
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Scaffold(
body: Center(child: CircularProgressIndicator()), // Chờ đợi dữ liệu
);
} else if (snapshot.hasError) {
return Scaffold(
body: Center(child: Text("Error: ${snapshot.error}")), // Xửlý lỗ i khi
gọi API
);
}
return Scaffold(
appBar: AppBar(
title: const Text(
"Checkout",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
),
centerTitle: true,
backgroundColor: kcontentColor,
leading: IconButton(
icon: const Icon(Icons.arrow_back_ios),
onPressed: () {
Navigator.pop(context);
},
),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
"Order Information",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
),
const SizedBox(height: 10),
// Các ô nhập liệu vẫn giữ nguyên như cũ
_buildInputField("Name"),
const SizedBox(height: 10),
_buildInputField("Phone Number"),
const SizedBox(height: 10),
_buildInputField("Email"),
const SizedBox(height: 10),
_buildInputField("Shipping Address"),
const SizedBox(height: 20),
const Text(
"Order Details",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
),
const SizedBox(height: 10),
// Hiể n thị các sản phẩ m trong giỏ hàng
Expanded(
child: ListView.builder(
itemCount: cartProvider.cartItems.length,
itemBuilder: (context, index) {
final cartItem = cartProvider.cartItems[index];
return ListTile(
title: Text(
cartItem.name,
style: const TextStyle(fontSize: 18),
),
subtitle: Text(
"Quantity: ${cartItem.quantity}",
style: const TextStyle(fontSize: 16),
),
trailing: Text(
formatCurrency.format(cartItem.price),
style: const TextStyle(fontSize: 16),
),
);
},
),
),
const Divider(),
// Hiể n thị tổng giá trị giỏ hàng
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text("Total Price", style: TextStyle(fontSize: 18)),
Text(
formatCurrency.format(cartProvider.totalPrice),
style: const TextStyle(fontSize: 16),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text("VAT", style: TextStyle(fontSize: 18)),
Text(
formatCurrency.format(cartProvider.totalPrice * 0.1),
style: const TextStyle(fontSize: 16),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text("Shipping Fee", style: TextStyle(fontSize: 18)),
Text(
formatCurrency.format(50000),
style: const TextStyle(fontSize: 16),
),
],
),
const Divider(),
// Hiể n thị tổng cộng sau khi thêm VAT và phí vận chuyển
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text("Total Amount",
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
Text(
formatCurrency.format(cartProvider.totalPrice * 1.1 + 50000),
style: const TextStyle(fontSize: 18, fontWeight:
FontWeight.bold, color: Colors.red),
),
],
),
const SizedBox(height: 20),
// Nút thanh toán
ElevatedButton(
onPressed: () {
// Thêm chức năng thanh toán ở đây
},
style: ElevatedButton.styleFrom(
backgroundColor: kprimaryColor,
minimumSize: const Size(double.infinity, 55),
),
child: const Text(
"Pay Now",
style: TextStyle(
fontSize: 20,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
],
),
),
);
},
);
}