Newer
Older
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'gamelobbyscreen.dart';
class GameSettingsScreen extends StatefulWidget {
final String playerName;
const GameSettingsScreen({super.key, required this.playerName});
@override
GameSettingsScreenState createState() => GameSettingsScreenState();
class GameSettingsScreenState extends State<GameSettingsScreen> {
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
final TextEditingController _numQuestionsController = TextEditingController();
final List<String> _categories = [
'Compass',
'GPS sensor',
'History',
'Literature',
'Mathematics',
'Pop Culture',
'Science & Nature',
'Sound',
'Sound Sensor',
'Technology'
];
final List<String> _selectedCategories = [];
Future<void> _createGame() async {
if (_numQuestionsController.text.isEmpty || _selectedCategories.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Please select number of questions and categories')),
);
return;
}
try {
final numQuestions = int.parse(_numQuestionsController.text);
final gameCode = (100000 + DateTime.now().millisecondsSinceEpoch % 900000).toString();
// Create game document
await FirebaseFirestore.instance.collection('games').doc(gameCode).set({
'createdAt': FieldValue.serverTimestamp(),
'status': 'waiting',
'host': widget.playerName,
'numQuestions': numQuestions,
'categories': _selectedCategories,
});
if (!mounted) return;
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => GameLobbyScreen(
gameCode: gameCode,
playerName: widget.playerName,
isHost: true,
),
),
);
} catch (e) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error creating game: $e')),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Game Settings'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: _numQuestionsController,
decoration: const InputDecoration(
labelText: 'Number of Questions',
border: OutlineInputBorder(),
),
keyboardType: TextInputType.number,
),
const SizedBox(height: 16),
const Text('Select Categories:'),
Expanded(
child: ListView.builder(
itemCount: _categories.length,
itemBuilder: (context, index) {
final category = _categories[index];
return CheckboxListTile(
title: Text(category),
value: _selectedCategories.contains(category),
onChanged: (bool? value) {
setState(() {
if (value == true) {
_selectedCategories.add(category);
} else {
_selectedCategories.remove(category);
}
});
},
);
},
),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _createGame,
child: const Text('Create Game'),
),
],
),
),
);
}
}