-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauction_host.dart
More file actions
104 lines (95 loc) · 2.9 KB
/
auction_host.dart
File metadata and controls
104 lines (95 loc) · 2.9 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
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_auth/firebase_auth.dart';
class AuctionHostScreen extends StatefulWidget {
@override
_AuctionHostScreenState createState() => _AuctionHostScreenState();
}
class _AuctionHostScreenState extends State<AuctionHostScreen> {
final itemController = TextEditingController();
final weightController = TextEditingController();
final db = FirebaseDatabase.instance.reference().child('auctions');
final uid = FirebaseAuth.instance.currentUser?.uid;
int remaining = 0;
Timer? timer;
bool isRunning = false;
String auctionId = "";
void startAuction(int seconds) {
setState(() {
remaining = seconds;
isRunning = true;
});
final newRef = db.push();
auctionId = newRef.key!;
newRef.set({
'itemName': itemController.text,
'weight': double.tryParse(weightController.text) ?? 1.0,
'isActive': true,
'timeLeft': seconds,
'bids': [],
'sellerId': uid,
});
timer = Timer.periodic(Duration(seconds: 1), (t) {
if (remaining <= 0) {
t.cancel();
db.child(auctionId).update({'isActive': false});
setState(() => isRunning = false);
} else {
setState(() => remaining--);
db.child(auctionId).update({'timeLeft': remaining});
}
});
}
void endAuctionNow() {
timer?.cancel();
db.child(auctionId).update({'isActive': false});
setState(() => isRunning = false);
}
@override
void dispose() {
timer?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Host Auction')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(children: [
TextField(
controller: itemController,
decoration: InputDecoration(labelText: 'Item Title'),
),
TextField(
controller: weightController,
decoration: InputDecoration(labelText: 'Weight (oz)'),
keyboardType: TextInputType.number,
),
SizedBox(height: 16),
DropdownButton<int>(
value: 60,
onChanged: isRunning ? null : (val) => startAuction(val!),
items: [30, 60, 120].map((v) => DropdownMenuItem(
value: v,
child: Text("Start $v sec Auction"),
)).toList(),
),
if (isRunning)
Column(
children: [
SizedBox(height: 16),
Text("Time Remaining: $remaining s", style: TextStyle(fontSize: 22)),
ElevatedButton.icon(
onPressed: endAuctionNow,
icon: Icon(Icons.cancel),
label: Text("End Early"),
)
],
)
]),
),
);
}
}