cofounderella/lib/pages/conversations_page.dart

89 lines
2.5 KiB
Dart
Raw Normal View History

2024-06-02 16:32:19 +02:00
import 'package:flutter/material.dart';
import '../components/user_tile.dart';
import '../constants.dart';
import '../services/auth/auth_service.dart';
import '../services/user_service.dart';
import 'chat_page.dart';
class ConversationsPage extends StatelessWidget {
ConversationsPage({super.key});
// auth service
final AuthService _authService = AuthService();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Conversations'),
backgroundColor: Colors.transparent,
foregroundColor: Colors.grey.shade800,
elevation: 0,
),
body: _buildUserMatchesList(),
);
}
// build the list of matches for the current user
Widget _buildUserMatchesList() {
final currentUser = _authService.getCurrentUser();
if (currentUser == null) {
return const Center(child: Text('Error: User not logged in'));
}
return StreamBuilder<List<Map<String, dynamic>>>(
stream: UserService.getMatchedUsersStream(currentUser.uid),
builder: (context, snapshot) {
// error
if (snapshot.hasError) {
return Text('Error: ${snapshot.error.toString()}');
}
// loading
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
// return list view
if (snapshot.hasData) {
final matchedUsers = snapshot.data!;
if (matchedUsers.isEmpty) {
return const Center(child: Text('No matches yet'));
}
return ListView(
children: matchedUsers
.map<Widget>(
(userData) => _buildUserListItem(userData, context))
.toList(),
);
} else {
return const Center(child: Text('No matches found'));
}
},
);
}
// build individual user list item
Widget _buildUserListItem(
Map<String, dynamic> userData, BuildContext context) {
return UserTile(
text: userData[Constants.dbFieldUsersEmail],
2024-06-02 18:58:43 +02:00
profileImageUrl: userData[Constants.dbFieldUsersProfilePic],
2024-06-02 16:32:19 +02:00
onTap: () {
// tapped on a user -> go to chat page
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ChatPage(
receiverEmail: userData[Constants.dbFieldUsersEmail],
receiverID: userData[Constants.dbFieldUsersID],
),
),
);
},
);
}
}