Insparkspokalen-ui/lib/teams/showTeams.dart
2025-05-16 09:04:23 +02:00

85 lines
2.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:insparkspokalen_ui/models/teamModel.dart';
import 'package:insparkspokalen_ui/services/googleAuthService.dart';
import 'package:insparkspokalen_ui/services/teamService.dart';
import 'package:insparkspokalen_ui/services/userService.dart';
class TeamCard extends StatelessWidget {
final TeamModel team;
final VoidCallback onJoin;
final TeamService teamService;
final UserService userService;
const TeamCard({
super.key,
required this.team,
required this.onJoin,
required this.teamService,
required this.userService,
});
@override
Widget build(BuildContext context) {
return Card(
color: Colors.white,
margin: const EdgeInsets.symmetric(vertical: 6),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
side: const BorderSide(color: Colors.black12),
),
child: ListTile(
title: Text(team.name),
subtitle: Row(
children: [
const Text('', style: TextStyle(color: Colors.amber)),
Text(
'${'2'}/${'20'}', //hårdkodat antal personer och max antal personer i ett team
style: const TextStyle(
color: Colors.amber,
fontWeight: FontWeight.w600,
),
),
],
),
trailing: ElevatedButton(
onPressed: () async {
final isAuthenticated = await GoogleAuthService.ensureLoggedIn(context);
if (!isAuthenticated) return;
try {
final addedToTeam = await teamService.addUserToTeam(team.id);
final addedToUser = await userService.addTeamToUser(team.id);
if (addedToTeam && addedToUser) {
onJoin();
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Failed to join team.")),
);
}
} catch (e, stacktrace) {
debugPrint("Join team error: $e");
debugPrint("Stacktrace: $stacktrace");
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Error occurred while joining team.")),
);
}
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey[300],
foregroundColor: Colors.black,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
shadowColor: Colors.black45,
elevation: 4,
),
child: const Text('Join'),
),
),
);
}
}