cofounderella/lib/pages/conversations_page.dart

88 lines
2.6 KiB
Dart

import 'package:flutter/material.dart';
import '../components/user_tile_chats.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});
final AuthService _authService = AuthService();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Your Chat Contacts'),
backgroundColor: Colors.transparent,
),
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(currentUser.uid, userData, context))
.toList(),
);
} else {
return const Center(child: Text('No matches found'));
}
},
);
}
// build individual user list item
Widget _buildUserListItem(String currentUserId, Map<String, dynamic> userData,
BuildContext context) {
return UserTileChats(
headerText: userData[Constants.dbFieldUsersName],
currentUserId: currentUserId,
otherUserId: userData[Constants.dbFieldUsersID],
profileImageUrl: userData[Constants.dbFieldUsersProfilePic],
onTap: () {
// tapped on a user -> go to chat page
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ChatPage(
receiverEmail: userData[Constants.dbFieldUsersEmail],
receiverID: userData[Constants.dbFieldUsersID],
),
),
);
},
);
}
}