cofounderella/lib/components/user_tile_likes.dart

121 lines
3.5 KiB
Dart
Raw Normal View History

2024-06-04 20:12:12 +02:00
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import '../constants.dart';
2024-06-17 15:05:41 +02:00
import '../models/user_profile.dart';
import '../services/user_service.dart';
import '../utils/math.dart';
2024-06-04 20:12:12 +02:00
2024-06-17 15:05:41 +02:00
class UserTileLikes extends StatefulWidget {
2024-06-04 20:12:12 +02:00
final DocumentSnapshot user;
final bool hasMatch;
2024-06-17 15:37:59 +02:00
final UserProfile? currentUser;
2024-06-04 20:12:12 +02:00
final VoidCallback onUnlike;
final VoidCallback onShowChat;
2024-06-04 20:12:12 +02:00
final VoidCallback onViewInfo;
const UserTileLikes({
super.key,
required this.user,
required this.hasMatch,
2024-06-17 15:37:59 +02:00
this.currentUser,
2024-06-04 20:12:12 +02:00
required this.onUnlike,
required this.onShowChat,
2024-06-04 20:12:12 +02:00
required this.onViewInfo,
});
2024-06-17 15:05:41 +02:00
@override
State<UserTileLikes> createState() => _UserTileLikesState();
}
class _UserTileLikesState extends State<UserTileLikes> {
UserProfile? _otherUser;
bool _isLoading = true;
@override
void initState() {
super.initState();
_fetchUserProfile();
}
Future<void> _fetchUserProfile() async {
UserProfile userProfile =
await UserService.getUserProfileFromDocument(widget.user);
setState(() {
_otherUser = userProfile;
_isLoading = false;
});
}
2024-06-04 20:12:12 +02:00
@override
Widget build(BuildContext context) {
2024-06-17 15:05:41 +02:00
if (_isLoading) {
return const CircularProgressIndicator();
}
Map<String, dynamic> userMap = widget.user.data() as Map<String, dynamic>;
2024-06-04 20:12:12 +02:00
bool hasPictureUrl = userMap.containsKey(Constants.dbFieldUsersProfilePic);
bool hasName = userMap.containsKey(Constants.dbFieldUsersName);
bool hasBio = userMap.containsKey(Constants.dbFieldUsersBio);
2024-06-17 15:37:59 +02:00
String? shortDist = (_otherUser != null && widget.currentUser != null
? shortestDistanceBetweenUsers(widget.currentUser!, _otherUser!)
2024-06-17 15:05:41 +02:00
.toStringAsFixed(0)
: null);
2024-06-04 20:12:12 +02:00
return Card(
margin: const EdgeInsets.all(8.0),
child: ListTile(
2024-06-17 15:05:41 +02:00
leading: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
hasPictureUrl == true &&
widget.user[Constants.dbFieldUsersProfilePic] != null
? CircleAvatar(
maxRadius: 16,
backgroundImage: NetworkImage(
widget.user[Constants.dbFieldUsersProfilePic],
),
)
: const CircleAvatar(
maxRadius: 16,
child: Icon(Icons.person),
),
Text('$shortDist km'),
],
),
2024-06-04 20:12:12 +02:00
title: hasName
? Text(
2024-06-17 15:05:41 +02:00
'${widget.user[Constants.dbFieldUsersName]}',
2024-06-04 20:12:12 +02:00
overflow: TextOverflow.ellipsis,
maxLines: 1,
)
: null,
subtitle: hasBio
? Text(
2024-06-17 15:05:41 +02:00
widget.user[Constants.dbFieldUsersBio],
2024-06-04 20:12:12 +02:00
overflow: TextOverflow.ellipsis,
maxLines: 3,
)
: null,
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: const Icon(Icons.contact_page_outlined),
2024-06-17 15:05:41 +02:00
onPressed: widget.onViewInfo,
2024-06-04 20:12:12 +02:00
),
IconButton(
2024-06-17 15:05:41 +02:00
icon: widget.hasMatch
? const Icon(Icons.chat_outlined)
2024-06-04 20:12:12 +02:00
: const Icon(Icons.delete_outline, color: Colors.red),
2024-06-17 15:05:41 +02:00
onPressed: widget.hasMatch ? widget.onShowChat : widget.onUnlike,
2024-06-04 20:12:12 +02:00
),
],
),
),
);
}
}