-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorefront.dart
More file actions
162 lines (150 loc) · 5.07 KB
/
storefront.dart
File metadata and controls
162 lines (150 loc) · 5.07 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
class StorefrontScreen extends StatefulWidget {
@override
State<StorefrontScreen> createState() => _StorefrontScreenState();
}
class _StorefrontScreenState extends State<StorefrontScreen> {
final _auth = FirebaseAuth.instance;
final _db = FirebaseDatabase.instance.reference().child('storefronts');
List<Map<String, dynamic>> items = [];
@override
void initState() {
super.initState();
final uid = _auth.currentUser?.uid;
if (uid != null) {
_db.child(uid).onValue.listen((event) {
final data = Map<String, dynamic>.from(event.snapshot.value ?? {});
final list = data.entries.map((e) {
final item = Map<String, dynamic>.from(e.value);
return {'id': e.key, ...item};
}).toList();
setState(() => items = list);
});
}
}
void addItem(String title, double price, bool isAuction, double weight) {
final uid = _auth.currentUser?.uid;
if (uid != null) {
final newItem = {
'title': title,
'price': price,
'isAuction': isAuction,
'weight': weight,
'image': '',
};
_db.child(uid).push().set(newItem);
}
}
void showAddItemDialog() {
final titleController = TextEditingController();
final priceController = TextEditingController();
final weightController = TextEditingController();
bool isAuction = false;
showDialog(
context: context,
builder: (context) => StatefulBuilder(
builder: (context, setState) => AlertDialog(
title: Text("Add Store Item"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(controller: titleController, decoration: InputDecoration(labelText: "Title")),
TextField(controller: priceController, decoration: InputDecoration(labelText: "Price"), keyboardType: TextInputType.number),
TextField(controller: weightController, decoration: InputDecoration(labelText: "Weight (oz)"), keyboardType: TextInputType.number),
Row(
children: [
Checkbox(value: isAuction, onChanged: (val) => setState(() => isAuction = val!)),
Text("Auction Item?")
],
),
],
),
actions: [
ElevatedButton(
onPressed: () {
final title = titleController.text;
final price = double.tryParse(priceController.text) ?? 0;
final weight = double.tryParse(weightController.text) ?? 0;
addItem(title, price, isAuction, weight);
Navigator.pop(context);
},
child: Text("Add"),
)
],
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Your Storefront")),
body: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
final item = items[index];
return Card(
child: ListTile(
title: Text(item['title']),
subtitle: Text("\$${item['price']}"),
trailing: item['isAuction']
? Text("Auction")
: ElevatedButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Purchased ${item['title']} (mock)")),
);
},
child: Text("Buy Now"),
),
),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: showAddItemDialog,
child: Icon(Icons.add),
),
);
}
}
#-------Flutter Button to Trigger Payment----
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:url_launcher/url_launcher.dart';
Future<void> startStripeCheckout() async {
final response = await http.post(
Uri.parse("http://localhost:8000/create-checkout-session"),
);
if (response.statusCode == 200) {
final body = json.decode(response.body);
final url = body['url'];
if (await canLaunchUrl(Uri.parse(url))) {
await launchUrl(Uri.parse(url), mode: LaunchMode.externalApplication);
} else {
throw 'Could not launch Stripe Checkout';
}
} else {
throw 'Failed to start checkout: ${response.body}';
}
}
#---Add a button----
ElevatedButton(
onPressed: startStripeCheckout,
child: Text("Pay with Stripe"),
)
#--Images Selected----
List<XFile> selectedImages = [];
Future<void> pickImages() async {
final images = await ImagePicker().pickMultiImage();
if (images != null) setState(() => selectedImages = images);
}
CarouselSlider(
options: CarouselOptions(height: 200),
items: selectedImages.map((img) {
return Image.file(File(img.path), fit: BoxFit.cover);
}).toList(),
)
#send image URLs to /listings/{id}/images via HTTP