Insparkspokalen-ui/lib/teams/createTeamsButton.dart

156 lines
4.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:insparkspokalen_ui/services/backend/teamService.dart';
import 'package:insparkspokalen_ui/models/teamModel.dart';
import 'package:flutter_svg/flutter_svg.dart';
class CreateTeamsButton extends StatefulWidget {
final TextEditingController controller;
final Function(TeamModel) onTeamCreated;
const CreateTeamsButton({
super.key,
required this.controller,
required this.onTeamCreated,
});
@override
CreateTeamsButtonState createState() => CreateTeamsButtonState();
}
class CreateTeamsButtonState extends State<CreateTeamsButton> {
final TeamService teamService = TeamService();
final List<String> availableSeeds = [
'rings',
'avataaars-neutral',
'adventurer-neutral',
'bottts-neutral',
'big-ears-neutral',
'bottts',
'fun-emoji',
];
String? selectedSeed;
@override
void initState() {
super.initState();
selectedSeed = availableSeeds.first;
}
@override
Widget build(BuildContext context) {
return AlertDialog(
backgroundColor: const Color(0xFF3A3A3A),
title: const Text(
'Skapa nytt lag',
style: TextStyle(color: Colors.white70),
),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: widget.controller,
style: const TextStyle(color: Colors.white),
decoration: const InputDecoration(
hintText: 'Lagnamn',
hintStyle: TextStyle(color: Colors.white54),
),
),
const SizedBox(height: 16),
DropdownButtonFormField<String>(
dropdownColor: const Color(0xFF3A3A3A),
value: selectedSeed,
items:
availableSeeds.map((seed) {
return DropdownMenuItem<String>(
value: seed,
child: Row(
children: [
SvgPicture.network(
'https://api.dicebear.com/9.x/$seed/svg?seed=Example',
width: 30,
height: 30,
placeholderBuilder:
(context) => const SizedBox(
width: 30,
height: 30,
child: CircularProgressIndicator(
strokeWidth: 1,
),
),
),
const SizedBox(width: 12),
Text(seed, style: const TextStyle(color: Colors.white)),
],
),
);
}).toList(),
onChanged: (value) {
setState(() {
selectedSeed = value;
});
},
decoration: const InputDecoration(
labelText: 'Välj bild (seed)',
labelStyle: TextStyle(color: Colors.white54),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white38),
),
),
),
],
),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Avbryt', style: TextStyle(color: Colors.white70)),
),
TextButton(
onPressed: () async {
String teamName = widget.controller.text.trim();
if (teamName.isNotEmpty && selectedSeed != null) {
final success = await teamService.createTeam(
teamName,
selectedSeed!,
);
if (success) {
final updatedGroups = await teamService.showTeams();
final newGroup = updatedGroups.firstWhere(
(group) => group.name == teamName,
orElse: () => TeamModel(-1, teamName, 0, '', []),
);
widget.onTeamCreated(newGroup);
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Lag '$teamName' skapades!")),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Det gick inte att skapa laget"),
),
);
}
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Vänligen ange namn och välj bild"),
),
);
}
},
child: const Text('Skapa', style: TextStyle(color: Colors.amber)),
),
],
);
}
}