147 lines
4.6 KiB
Dart
147 lines
4.6 KiB
Dart
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]}'),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|