cofounderella/lib/pages/home_page.dart

81 lines
2.2 KiB
Dart

import 'package:flutter/material.dart';
import '../components/my_drawer.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 HomePage extends StatelessWidget {
HomePage({super.key});
final AuthService _authService = AuthService();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
// TODO appbar style: remove background and set elevation to 0 ?
backgroundColor: Colors.transparent,
foregroundColor: Colors.grey.shade800,
elevation: 0,
),
drawer: const MyDrawer(),
body: _buildUserList(),
);
}
// build a list of users except for the current logged in user
Widget _buildUserList() {
return StreamBuilder(
stream: UserService.getUsersStream(),
builder: (context, snapshot) {
// error
if (snapshot.hasError) {
return Text('Error: ${snapshot.error.toString()}');
}
//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[Constants.dbFieldUsersEmail] !=
_authService.getCurrentUser()!.email) {
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],
),
),
);
},
);
} else {
return Container();
}
}
}