-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart_page.dart
108 lines (104 loc) · 4.36 KB
/
cart_page.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shivangi/items.dart';
import 'package:provider/provider.dart';
import 'package:upi_payment_flutter/upi_payment_flutter.dart';
import 'cart_model.dart';
import 'item_details_screen.dart';
class CartPage extends StatefulWidget {
@override
_CartPageState createState() => _CartPageState();
}
class _CartPageState extends State<CartPage> {
List<Items> selectedItems = [];
final upiPaymentHandler = UpiPaymentHandler();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Cart'),
backgroundColor: Colors.black,
),
body: Container(
color: Colors.black,
child: Consumer<CartModel>(
builder: (context, cartModel, child) {
double totalPrice = cartModel.items.fold<double>(0, (a, b) => a + num.parse(b.itemPrice!));
return Column(
children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: cartModel.items.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(cartModel.items[index].itemName ?? 'Unknown name', overflow: TextOverflow.ellipsis, style: TextStyle(color: Colors.grey)),
subtitle: Text(cartModel.items[index].itemDescription ?? 'Unknown name', overflow: TextOverflow.ellipsis, style: TextStyle(color: Colors.grey)),
trailing: IconButton(
icon: const Icon(Icons.remove_shopping_cart, color: Colors.grey),
onPressed: () {
cartModel.removeItem(cartModel.items[index]);
},
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ItemDetailsScreen(clickedItemInfo: cartModel.items[index]),
),
);
},
);
},
),
),
Container(
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'Total: Rs${totalPrice.toStringAsFixed(2)}',
style: TextStyle(color: Colors.grey),
),
ElevatedButton(
onPressed: () async {
try {
bool success = await upiPaymentHandler.initiateTransaction(
payeeVpa: '7488321540@paytm',
payeeName: 'Shivangi Suyash',
transactionRefId: 'TXN123456',
transactionNote: 'Test transaction',
amount: totalPrice,
);
if (success) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Transaction successful')),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Transaction failed')),
);
}
} on PlatformException catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('UPI not supported on this device')),
);
}
},
child: Text('Pay Now'),
style: ElevatedButton.styleFrom(
primary: Colors.pinkAccent, // Button color
onPrimary: Colors.white, // Text color
),
),
],
),
),
],
);
},
),
),
);
}
}