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>>( 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( (userData) => _buildUserListItem(userData, context)) .toList(), ); } else { return const Center(child: Text('No matches found')); } }, ); } // build individual user list item Widget _buildUserListItem( Map userData, BuildContext context) { return UserTile( text: userData[Constants.dbFieldUsersEmail], onTap: () { // tapped on a user -> go to chat page Navigator.push( context, MaterialPageRoute( builder: (context) => ChatPage( receiverEmail: userData[Constants.dbFieldUsersEmail], receiverID: userData[Constants.dbFieldUsersID], ), ), ); }, ); } }