cofounderella/lib/pages/conversations_page.dart

88 lines
2.6 KiB
Dart
Raw Normal View History

2024-06-02 16:32:19 +02:00
import 'package:flutter/material.dart';
2024-06-04 20:12:12 +02:00
import '../components/user_tile_chats.dart';
2024-06-02 16:32:19 +02:00
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(
2024-06-03 16:35:33 +02:00
title: const Text('Your Chat Contacts'),
2024-06-02 16:32:19 +02:00
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
2024-06-03 16:35:33 +02:00
.map<Widget>((userData) =>
_buildUserListItem(currentUser.uid, userData, context))
2024-06-02 16:32:19 +02:00
.toList(),
);
} else {
return const Center(child: Text('No matches found'));
}
},
);
}
// build individual user list item
2024-06-03 16:35:33 +02:00
Widget _buildUserListItem(String currentUserId, Map<String, dynamic> userData,
BuildContext context) {
2024-06-04 20:12:12 +02:00
return UserTileChats(
2024-06-03 16:35:33 +02:00
headerText: userData[Constants.dbFieldUsersName],
currentUserId: currentUserId,
otherUserId: userData[Constants.dbFieldUsersID],
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],
),
),
);
},
);
}
}