143 lines
No EOL
3.9 KiB
Dart
143 lines
No EOL
3.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:laserscouter/core/api.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:to_csv/to_csv.dart' as exportcsv;
|
|
import 'notespage.dart'; // Import the new notes page file
|
|
|
|
class TeamPicker extends StatefulWidget {
|
|
final String eventCode;
|
|
|
|
const TeamPicker({super.key, required this.eventCode});
|
|
|
|
@override
|
|
State<TeamPicker> createState() => _TeamPickerState();
|
|
}
|
|
|
|
class _TeamPickerState extends State<TeamPicker> {
|
|
List<String> teamNames = [];
|
|
List<String> teamCodes = [];
|
|
bool isLoading = true;
|
|
String? errorMessage;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
// Call the new async method to fetch teams
|
|
_fetchTeams();
|
|
}
|
|
|
|
Future<void> _fetchTeams() async {
|
|
try {
|
|
// Await the result from the refactored API function
|
|
final EventSearchResult result = await eventSearch(widget.eventCode);
|
|
|
|
// Check if the widget is still mounted before updating state
|
|
if (mounted) {
|
|
setState(() {
|
|
teamNames = result.teamNames;
|
|
teamCodes = result.teamCodes;
|
|
isLoading = false;
|
|
});
|
|
}
|
|
} catch (e) {
|
|
// If an error occurs, update the state to show an error message
|
|
if (mounted) {
|
|
setState(() {
|
|
errorMessage = "Failed to load teams. Please try again.";
|
|
isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> makeCSV() async {
|
|
List<String> header = [
|
|
'Team Number',
|
|
'Bot Position',
|
|
'Auton Rundown',
|
|
'Can Score Algae',
|
|
'Coral Level'
|
|
];
|
|
List<List<String>> dataLists = [header];
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
for (int i = 0; i < teamCodes.length; i++) {
|
|
List<String> data = [];
|
|
data.add(teamCodes[i]);
|
|
String botPosition = prefs.getString('${teamCodes[i]}_${widget.eventCode}_note1') ?? '';
|
|
String autonRundown = prefs.getString('${teamCodes[i]}_${widget.eventCode}_note2') ?? '';
|
|
String canScoreAlgae = prefs.getString('${teamCodes[i]}_${widget.eventCode}_note3') ?? '';
|
|
String coralLevel = prefs.getString('${teamCodes[i]}_${widget.eventCode}_note4') ?? '0.0';
|
|
data.add(botPosition.trim());
|
|
data.add(autonRundown);
|
|
data.add(canScoreAlgae);
|
|
data.add(coralLevel);
|
|
dataLists.add(data);
|
|
}
|
|
exportcsv.myCSV(header, dataLists, fileName: '${widget.eventCode}-scouting-data');
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Teams'),
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.share),
|
|
onPressed: isLoading || teamCodes.isEmpty ? null : makeCSV,
|
|
),
|
|
],
|
|
),
|
|
body: _buildBody(),
|
|
);
|
|
}
|
|
|
|
Widget _buildBody() {
|
|
if (isLoading) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
if (errorMessage != null) {
|
|
return Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Text(
|
|
errorMessage!,
|
|
textAlign: TextAlign.center,
|
|
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
|
|
color: Colors.red,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
if (teamCodes.isEmpty) {
|
|
return const Center(
|
|
child: Text('No teams found for this event.'),
|
|
);
|
|
}
|
|
|
|
return ListView.builder(
|
|
itemCount: teamCodes.length,
|
|
itemBuilder: (context, index) {
|
|
return ListTile(
|
|
title: Text('${teamCodes[index]} - ${teamNames[index]}'),
|
|
onTap: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => NotesPage(
|
|
teamName: teamNames[index],
|
|
eventCode: widget.eventCode,
|
|
teamCode: teamCodes[index],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
} |