reorganize the profile page, move My Teams from Homepage to profile add sign out button to popup menu in homepage
This commit is contained in:
@@ -3,71 +3,57 @@ import 'package:frontend_splatournament_manager/models/team.dart';
|
||||
import 'package:frontend_splatournament_manager/providers/team_provider.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class MyTeamsWidget extends StatefulWidget {
|
||||
class MyTeamsWidget extends StatelessWidget {
|
||||
const MyTeamsWidget({super.key});
|
||||
|
||||
@override
|
||||
State<MyTeamsWidget> createState() => _MyTeamsWidgetState();
|
||||
}
|
||||
|
||||
class _MyTeamsWidgetState extends State<MyTeamsWidget> {
|
||||
late Future<List<Team>> _myTeamsFuture;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadMyTeams();
|
||||
}
|
||||
|
||||
void _loadMyTeams() {
|
||||
_myTeamsFuture = Provider.of<TeamProvider>(
|
||||
context,
|
||||
listen: false,
|
||||
).getUserTeams();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder<List<Team>>(
|
||||
future: _myTeamsFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
return Consumer<TeamProvider>(
|
||||
builder: (context, provider, _) {
|
||||
return FutureBuilder<List<Team>>(
|
||||
future: provider.ensureUserTeamsLoaded(),
|
||||
builder: (context, snapshot) {
|
||||
final teams = provider.userTeams;
|
||||
|
||||
if (snapshot.hasError) {
|
||||
return Center(
|
||||
child: Text(
|
||||
'Fehler: ${snapshot.error.toString().replaceFirst('Exception: ', '')}',
|
||||
),
|
||||
);
|
||||
}
|
||||
if (snapshot.connectionState == ConnectionState.waiting &&
|
||||
teams.isEmpty) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
final teams = snapshot.data ?? [];
|
||||
if (teams.isEmpty) {
|
||||
return const Center(
|
||||
child: Text(
|
||||
'Du bist noch in keinem Team.\nTritt einem Team im Tab Alle Teams bei.',
|
||||
),
|
||||
);
|
||||
}
|
||||
if (snapshot.hasError && teams.isEmpty) {
|
||||
return Center(
|
||||
child: Text(
|
||||
"Fehler: ${snapshot.error.toString().replaceFirst("Exception: ", "")}",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return ListView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: teams.length,
|
||||
itemBuilder: (context, index) => _buildTeamCard(teams[index]),
|
||||
if (teams.isEmpty) {
|
||||
return const Center(
|
||||
child: Text(
|
||||
'Du bist noch in keinem Team.\nTritt einem Team unter Teams bei.',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return ListView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: teams.length,
|
||||
itemBuilder: (context, index) =>
|
||||
_buildTeamCard(context, teams[index]),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTeamCard(Team team) {
|
||||
Widget _buildTeamCard(BuildContext context, Team team) {
|
||||
final memberCountText = team.memberCount != null
|
||||
? '${team.memberCount}/4 Mitglieder'
|
||||
: 'Keine Mitglieder';
|
||||
final description = team.description.isEmpty
|
||||
? 'Keine Beschreibung'
|
||||
: team.description;
|
||||
? "${team.memberCount}/4 Mitglieder"
|
||||
: "Keine Mitglieder";
|
||||
final description =
|
||||
team.description.isEmpty ? "Keine Beschreibung" : team.description;
|
||||
|
||||
return Card(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
@@ -80,50 +66,47 @@ class _MyTeamsWidgetState extends State<MyTeamsWidget> {
|
||||
),
|
||||
trailing: IconButton(
|
||||
icon: const Icon(Icons.logout, color: Colors.red),
|
||||
onPressed: () => _leaveTeam(team),
|
||||
onPressed: () => _leaveTeam(context, team),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _leaveTeam(Team team) async {
|
||||
Future<void> _leaveTeam(BuildContext context, Team team) async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Team verlassen?'),
|
||||
title: const Text("Team verlassen?"),
|
||||
content: Text('Soll "${team.name}" verlassen werden?'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Abbrechen'),
|
||||
child: const Text("Abbrechen"),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Verlassen', style: TextStyle(color: Colors.red)),
|
||||
child: const Text("Verlassen", style: TextStyle(color: Colors.red)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirmed == true && mounted) {
|
||||
if (confirmed == true && context.mounted) {
|
||||
try {
|
||||
await Provider.of<TeamProvider>(
|
||||
context,
|
||||
listen: false,
|
||||
).leaveTeam(team.id);
|
||||
if (mounted) {
|
||||
await Provider.of<TeamProvider>(context, listen: false).leaveTeam(
|
||||
team.id,
|
||||
);
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(const SnackBar(content: Text('Team verlassen')));
|
||||
_loadMyTeams();
|
||||
setState(() {});
|
||||
).showSnackBar(const SnackBar(content: Text("Team verlassen")));
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
'Fehler: ${e.toString().replaceFirst('Exception: ', '')}',
|
||||
"Fehler: ${e.toString().replaceFirst("Exception: ", "")}",
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user