Added ability to add custom teams and events.
This commit is contained in:
parent
84aaf9c527
commit
9327b6120f
8 changed files with 687 additions and 189 deletions
147
lib/teamadder.dart
Normal file
147
lib/teamadder.dart
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class TeamAdder extends StatefulWidget {
|
||||
final String eventCode;
|
||||
const TeamAdder({super.key, required this.eventCode});
|
||||
|
||||
@override
|
||||
State<TeamAdder> createState() => _TeamAdderState();
|
||||
}
|
||||
|
||||
class _TeamAdderState extends State<TeamAdder> {
|
||||
final _teamNumberController = TextEditingController();
|
||||
final _teamNameController = TextEditingController();
|
||||
List<String> customTeamNumbers = [];
|
||||
List<String> customTeamNames = [];
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadTeams();
|
||||
}
|
||||
|
||||
Future<void> _loadTeams() async {
|
||||
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
setState(() {
|
||||
customTeamNumbers = prefs.getStringList('custom_team_numbers_${widget.eventCode}') ?? [];
|
||||
customTeamNames = prefs.getStringList('custom_team_names_${widget.eventCode}') ?? [];
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _saveTeam() async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final String teamNumber = _teamNumberController.text;
|
||||
final String teamName = _teamNameController.text;
|
||||
|
||||
setState(() {
|
||||
customTeamNumbers.add(teamNumber);
|
||||
customTeamNames.add(teamName);
|
||||
});
|
||||
|
||||
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setStringList('custom_team_numbers_${widget.eventCode}', customTeamNumbers);
|
||||
await prefs.setStringList('custom_team_names_${widget.eventCode}', customTeamNames);
|
||||
|
||||
_teamNumberController.clear();
|
||||
_teamNameController.clear();
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Added Team: $teamNumber - $teamName'),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
margin: const EdgeInsets.all(16.0),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_teamNumberController.dispose();
|
||||
_teamNameController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Add Team'),
|
||||
),
|
||||
body: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('Add a New Team', style: Theme.of(context).textTheme.titleLarge),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _teamNumberController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Team Number',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
keyboardType: TextInputType.number,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a team number';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _teamNameController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Team Name',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a team name';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton(
|
||||
onPressed: _saveTeam,
|
||||
child: const Text('Save Team'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const Divider(height: 32),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
||||
child: Text('Saved Teams', style: Theme.of(context).textTheme.titleLarge),
|
||||
),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: customTeamNumbers.length,
|
||||
itemBuilder: (context, index) {
|
||||
return ListTile(
|
||||
title: Text('${customTeamNumbers[index]} - ${customTeamNames[index]}'),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue