129 lines
4.1 KiB
Dart
129 lines
4.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:travel_mate/models/group.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../../providers/user_provider.dart';
|
|
import '../../services/group_service.dart';
|
|
|
|
class GroupContent extends StatefulWidget {
|
|
const GroupContent({super.key});
|
|
|
|
@override
|
|
State<GroupContent> createState() => _GroupContentState();
|
|
}
|
|
|
|
class _GroupContentState extends State<GroupContent> {
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: Consumer<UserProvider>(
|
|
builder: (context, userProvider, child) {
|
|
final user = userProvider.currentUser;
|
|
if (user == null || user.id == null) {
|
|
return const Center(
|
|
child: Text('Utilisateur non connecté'),
|
|
);
|
|
}
|
|
|
|
return StreamBuilder<List<Group>>(
|
|
stream: GroupService().getGroupsStreamByUser(user.id!), // Filtrer par utilisateur
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return _buildLoadingState();
|
|
} else if (snapshot.hasError) {
|
|
print('Erreur du stream: ${snapshot.error}');
|
|
return Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Text('Erreur lors du chargement des groupes.'),
|
|
const SizedBox(height: 8),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
// Forcer le rechargement du stream
|
|
});
|
|
},
|
|
child: const Text('Réessayer'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
final groups = snapshot.data ?? [];
|
|
print("Groupes reçus pour l'utilisateur ${user.id}: ${groups.length}");
|
|
|
|
return RefreshIndicator(
|
|
onRefresh: () async {
|
|
setState(() {});
|
|
},
|
|
child: SingleChildScrollView(
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'Vos Groupes',
|
|
style: TextStyle(
|
|
fontSize: 24,
|
|
fontWeight: FontWeight.bold,
|
|
color: Theme.of(context).brightness == Brightness.dark ? Colors.white : Colors.black,
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
groups.isEmpty ? _buildEmptyState() : _buildGroupList(groups),
|
|
const SizedBox(height: 80),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildLoadingState() {
|
|
return const Center(
|
|
child: Padding(padding: EdgeInsets.all(16.0),
|
|
child: CircularProgressIndicator(),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildEmptyState() {
|
|
return const Center(
|
|
child: Text(
|
|
'Aucun groupe disponible. Créez ou rejoignez un voyage pour commencer à discuter!',
|
|
style: TextStyle(fontSize: 16, color: Colors.grey),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildGroupList(List<Group> groups) {
|
|
return Column(
|
|
children: groups.map((group) => _buildGroupCard(group)).toList(),
|
|
);
|
|
}
|
|
|
|
Widget _buildGroupCard(Group group) {
|
|
return Card(
|
|
margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
|
|
child: ListTile(
|
|
leading: CircleAvatar(
|
|
child: Text(group.name.isNotEmpty ? group.name[0] : '?'),
|
|
),
|
|
title: Text(group.name),
|
|
subtitle: Text('${group.members.length} membres'),
|
|
onTap: () {
|
|
// Logique pour ouvrir le chat de groupe
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|