2024-04-29 13:58:27 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
2024-06-02 16:32:19 +02:00
|
|
|
import '../components/my_drawer.dart';
|
|
|
|
import '../components/user_tile.dart';
|
|
|
|
import '../constants.dart';
|
|
|
|
import '../services/auth/auth_service.dart';
|
|
|
|
import '../services/user_service.dart';
|
2024-04-30 19:25:16 +02:00
|
|
|
import 'chat_page.dart';
|
2024-04-29 13:58:27 +02:00
|
|
|
|
2024-04-30 19:25:16 +02:00
|
|
|
class HomePage extends StatelessWidget {
|
|
|
|
HomePage({super.key});
|
2024-04-29 13:58:27 +02:00
|
|
|
|
2024-04-30 19:25:16 +02:00
|
|
|
final AuthService _authService = AuthService();
|
2024-04-29 13:58:27 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2024-06-02 16:32:19 +02:00
|
|
|
title: const Text('Home'),
|
2024-06-07 02:18:20 +02:00
|
|
|
// different appbar style for homepage
|
2024-04-30 19:25:16 +02:00
|
|
|
backgroundColor: Colors.transparent,
|
2024-04-29 13:58:27 +02:00
|
|
|
),
|
2024-04-29 17:11:35 +02:00
|
|
|
drawer: const MyDrawer(),
|
2024-04-30 19:25:16 +02:00
|
|
|
body: _buildUserList(),
|
2024-04-29 13:58:27 +02:00
|
|
|
);
|
|
|
|
}
|
2024-04-30 19:25:16 +02:00
|
|
|
|
|
|
|
// build a list of users except for the current logged in user
|
|
|
|
Widget _buildUserList() {
|
|
|
|
return StreamBuilder(
|
2024-06-02 16:32:19 +02:00
|
|
|
stream: UserService.getUsersStream(),
|
2024-04-30 19:25:16 +02:00
|
|
|
builder: (context, snapshot) {
|
|
|
|
// error
|
|
|
|
if (snapshot.hasError) {
|
2024-06-02 16:32:19 +02:00
|
|
|
return Text('Error: ${snapshot.error.toString()}');
|
2024-04-30 19:25:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//loading
|
|
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
2024-06-02 16:32:19 +02:00
|
|
|
return const Text('Loading..');
|
2024-04-30 19:25:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// return list view
|
|
|
|
return ListView(
|
|
|
|
children: snapshot.data!
|
|
|
|
.map<Widget>(
|
|
|
|
(userData) => _buildUserListItem(userData, context))
|
|
|
|
.toList(),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// build individual user list item
|
|
|
|
Widget _buildUserListItem(
|
|
|
|
Map<String, dynamic> userData, BuildContext context) {
|
|
|
|
// display all users except current user
|
2024-06-02 16:32:19 +02:00
|
|
|
if (userData[Constants.dbFieldUsersEmail] !=
|
|
|
|
_authService.getCurrentUser()!.email) {
|
2024-04-30 19:25:16 +02:00
|
|
|
return UserTile(
|
2024-06-04 20:12:12 +02:00
|
|
|
text: userData[Constants.dbFieldUsersEmail],
|
2024-04-30 19:25:16 +02:00
|
|
|
onTap: () {
|
2024-06-02 16:32:19 +02:00
|
|
|
// tapped on a user -> go to chat page
|
2024-04-30 19:25:16 +02:00
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => ChatPage(
|
2024-06-02 16:32:19 +02:00
|
|
|
receiverEmail: userData[Constants.dbFieldUsersEmail],
|
|
|
|
receiverID: userData[Constants.dbFieldUsersID],
|
2024-04-30 19:25:16 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Container();
|
|
|
|
}
|
|
|
|
}
|
2024-04-29 17:11:35 +02:00
|
|
|
}
|