cofounderella/lib/components/user_tile_likes.dart

128 lines
3.8 KiB
Dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import '../constants.dart';
import '../models/user_profile.dart';
import '../services/user_service.dart';
import '../utils/math.dart';
class UserTileLikes extends StatefulWidget {
final DocumentSnapshot user;
final bool hasMatch;
final UserProfile? currentUser;
final VoidCallback onUnlike;
final VoidCallback onShowChat;
final VoidCallback onViewInfo;
const UserTileLikes({
super.key,
required this.user,
required this.hasMatch,
this.currentUser,
required this.onUnlike,
required this.onShowChat,
required this.onViewInfo,
});
@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;
});
}
@override
Widget build(BuildContext context) {
if (_isLoading) {
return const CircularProgressIndicator();
}
Map<String, dynamic> userMap = widget.user.data() as Map<String, dynamic>;
bool hasPictureUrl = userMap.containsKey(Constants.dbFieldUsersProfilePic);
bool hasName = userMap.containsKey(Constants.dbFieldUsersName);
bool hasBio = userMap.containsKey(Constants.dbFieldUsersBio);
double? shortDist = (_otherUser != null && widget.currentUser != null
? shortestDistanceBetweenUsers(widget.currentUser!, _otherUser!)
: null);
return Card(
color: Theme.of(context).colorScheme.onInverseSurface,
margin: const EdgeInsets.all(8.0),
child: ListTile(
leading: GestureDetector(onTap: widget.onViewInfo,
child: 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),
),
if (shortDist != null && !shortDist.isNaN)
Text('${shortDist.toStringAsFixed(0)} km'),
],
),
),
title: hasName
? GestureDetector(onTap: widget.onViewInfo,
child: Text(
'${widget.user[Constants.dbFieldUsersName]}',
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
)
: null,
subtitle: hasBio
? GestureDetector(onTap: widget.onViewInfo,
child: Text(
widget.user[Constants.dbFieldUsersBio],
overflow: TextOverflow.ellipsis,
maxLines: 3,
),
)
: null,
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: const Icon(Icons.contact_page_outlined),
onPressed: widget.onViewInfo,
),
IconButton(
icon: widget.hasMatch
? const Icon(Icons.chat_outlined)
: const Icon(Icons.delete_outline, color: Colors.red),
onPressed: widget.hasMatch ? widget.onShowChat : widget.onUnlike,
),
],
),
),
);
}
}