2024-04-29 17:11:35 +02:00
|
|
|
import 'package:cofounderella/components/my_drawer.dart';
|
2024-04-30 19:25:16 +02:00
|
|
|
import 'package:cofounderella/components/user_tile.dart';
|
|
|
|
import 'package:cofounderella/services/auth/auth_service.dart';
|
|
|
|
import 'package:cofounderella/services/chat/chat_service.dart';
|
2024-04-29 13:58:27 +02:00
|
|
|
import 'package:flutter/material.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
|
|
|
// chat and auth service
|
|
|
|
final ChatService _chatService = ChatService();
|
|
|
|
final AuthService _authService = AuthService();
|
2024-04-29 13:58:27 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2024-04-30 19:25:16 +02:00
|
|
|
title: const Text("Home"),
|
|
|
|
// TODO appbar style: remove background and set elevation to 0 ?
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
foregroundColor: Colors.grey.shade800,
|
|
|
|
elevation: 0,
|
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(
|
|
|
|
stream: _chatService.getUsersStream(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
// error
|
|
|
|
if (snapshot.hasError) {
|
|
|
|
return const Text("Error");
|
|
|
|
}
|
|
|
|
|
|
|
|
//loading
|
|
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
|
|
return const Text("Loading..");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if (userData["email"] != _authService.getCurrentUser()!.email) {
|
|
|
|
return UserTile(
|
|
|
|
text: userData["email"],
|
|
|
|
onTap: () {
|
|
|
|
// tapped on a user -> go to chat page // TODO
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => ChatPage(
|
|
|
|
receiverEmail: userData["email"],
|
|
|
|
receiverID: userData["uid"],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Container();
|
|
|
|
}
|
|
|
|
}
|
2024-04-29 17:11:35 +02:00
|
|
|
}
|