cofounderella/lib/pages/home_page.dart

174 lines
5.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'),
// different appbar style for homepage
backgroundColor: Colors.transparent,
),
drawer: const MyDrawer(),
// body: _buildUserList(),
body: Center(
child: SingleChildScrollView(
child: Column(
children: [
AspectRatio(
aspectRatio: 1,
child: LayoutBuilder(
builder: (context, constraints) {
// Calculation of the tile size based on the spacing
double size = constraints.biggest.shortestSide / 2 - 16;
return GridView.count(
crossAxisCount: 2,
crossAxisSpacing: 16.0,
mainAxisSpacing: 16.0,
padding: const EdgeInsets.all(16.0),
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
children: [
_buildTile(
context,
'Discover',
Icons.explore,
Colors.lightBlue.shade300,
'/discover',
size,
),
_buildTile(
context,
'View Profiles',
Icons.star,
Colors.teal.shade300,
'/favorites',
size,
),
_buildTile(
context,
'Chat',
Icons.chat,
Colors.indigo.shade300,
'/chats',
size,
),
_buildTile(
context,
'My Profile',
Icons.person,
Colors.blueGrey.shade300,
'/profile',
size,
),
],
);
},
),
),
Container(
height: 400, // Adjust this height as needed
child: _buildUserList(),
),
],
),
),
),
);
}
Widget _buildTile(BuildContext context, String title, IconData icon,
Color color, String route, double size) {
return InkWell(
onTap: () {
Navigator.pushNamed(context, route);
},
child: Container(
width: size,
height: size,
decoration: BoxDecoration(
color: color,
borderRadius: BorderRadius.circular(16.0),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
icon,
size: 50,
color: Colors.white,
),
const SizedBox(height: 10),
Text(title,
style: const TextStyle(color: Colors.white, fontSize: 20)),
],
),
),
);
}
// 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(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
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();
}
}
}