1
1
import 'package:flutter/material.dart' ;
2
+ import 'cart_items.dart' ; // Import the global cart list
2
3
3
- class ProductDetailPage extends StatelessWidget {
4
+ class ProductDetailPage extends StatefulWidget {
4
5
final String productName;
5
6
final int productPrice;
6
7
final String productImagePath;
7
8
final String productDescription;
8
9
9
10
const ProductDetailPage ({
10
- Key ? key,
11
+ super . key,
11
12
required this .productName,
12
13
required this .productPrice,
13
14
required this .productImagePath,
14
15
required this .productDescription,
15
- }) : super (key: key);
16
+ });
17
+
18
+ @override
19
+ _ProductDetailPageState createState () => _ProductDetailPageState ();
20
+ }
21
+
22
+ class _ProductDetailPageState extends State <ProductDetailPage > {
23
+ int _quantity = 1 ;
24
+
25
+ void _incrementQuantity () {
26
+ setState (() {
27
+ _quantity++ ;
28
+ });
29
+ }
30
+
31
+ void _decrementQuantity () {
32
+ if (_quantity > 1 ) {
33
+ setState (() {
34
+ _quantity-- ;
35
+ });
36
+ }
37
+ }
38
+
39
+ // Function to calculate the total price for the selected product and quantity
40
+ int _calculateTotalPrice () {
41
+ return widget.productPrice * _quantity;
42
+ }
16
43
17
44
@override
18
45
Widget build (BuildContext context) {
19
46
return Scaffold (
20
47
appBar: AppBar (
21
- titleTextStyle: const TextStyle (
22
- color: Color (0xFF313423 ),
23
- fontFamily: 'Serif' ,
24
- fontSize: 25.0 , // Increase the text size
25
- ),
26
- title: Text (productName),
27
- backgroundColor: const Color (0xFF626F47 ),
48
+ title: Text (widget.productName),
49
+ backgroundColor: const Color (0xFF798645 ),
28
50
),
29
51
body: Padding (
30
52
padding: const EdgeInsets .all (16.0 ),
31
53
child: Column (
32
54
crossAxisAlignment: CrossAxisAlignment .start,
33
55
children: [
34
56
Image .asset (
35
- productImagePath,
57
+ widget. productImagePath,
36
58
height: 250 ,
37
59
width: double .infinity,
38
60
fit: BoxFit .cover,
39
61
),
40
62
const SizedBox (height: 16 ),
41
63
Text (
42
- productName,
64
+ widget. productName,
43
65
style: const TextStyle (fontSize: 24 , fontWeight: FontWeight .bold),
44
66
),
45
67
const SizedBox (height: 8 ),
46
68
Text (
47
- 'Rs. $productPrice /-' ,
69
+ 'Rs. ${ widget . productPrice } /-' ,
48
70
style: const TextStyle (fontSize: 20 , color: Colors .red),
49
71
),
50
72
const SizedBox (height: 16 ),
@@ -54,18 +76,56 @@ class ProductDetailPage extends StatelessWidget {
54
76
),
55
77
const SizedBox (height: 4 ),
56
78
Text (
57
- productDescription,
79
+ widget. productDescription,
58
80
style: const TextStyle (fontSize: 16 ),
59
81
),
82
+ const SizedBox (height: 16 ),
83
+ Row (
84
+ mainAxisAlignment: MainAxisAlignment .spaceBetween,
85
+ children: [
86
+ const Text ('Quantity' , style: TextStyle (fontSize: 18 )),
87
+ Row (
88
+ children: [
89
+ IconButton (
90
+ icon: const Icon (Icons .remove),
91
+ onPressed: _decrementQuantity,
92
+ ),
93
+ Text ('$_quantity ' , style: const TextStyle (fontSize: 18 )),
94
+ IconButton (
95
+ icon: const Icon (Icons .add),
96
+ onPressed: _incrementQuantity,
97
+ ),
98
+ ],
99
+ ),
100
+ ],
101
+ ),
102
+ const SizedBox (height: 16 ),
103
+ Text (
104
+ 'Total Price: Rs. ${_calculateTotalPrice ()}/-' , // Display total price based on quantity
105
+ style: const TextStyle (fontSize: 20 , fontWeight: FontWeight .bold),
106
+ ),
60
107
const Spacer (),
61
108
Center (
62
109
child: ElevatedButton (
63
110
onPressed: () {
64
- // Add to cart logic
111
+ // Add the product to the cart with quantity and total price
112
+ cartItems.add ({
113
+ 'name' : widget.productName,
114
+ 'price' : widget.productPrice,
115
+ 'image' : widget.productImagePath,
116
+ 'quantity' : _quantity, // Include the quantity
117
+ 'totalPrice' : _calculateTotalPrice (), // Include the total price
118
+ });
119
+
120
+ ScaffoldMessenger .of (context).showSnackBar (
121
+ SnackBar (
122
+ content: Text ('${widget .productName } added to cart!' ),
123
+ duration: const Duration (seconds: 2 ),
124
+ ),
125
+ );
65
126
},
66
127
style: ElevatedButton .styleFrom (
67
128
backgroundColor: const Color (0xFF798645 ),
68
- textStyle: const TextStyle (color: Color (0xFFF2EED7 )),
69
129
),
70
130
child: const Text ('Add to Cart' ),
71
131
),
0 commit comments