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, required this.currentUser, required this.onUnlike, required this.onShowChat, required this.onViewInfo, }); @override State createState() => _UserTileLikesState(); } class _UserTileLikesState extends State { UserProfile? _otherUser; bool _isLoading = true; @override void initState() { super.initState(); _fetchUserProfile(); } Future _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 userMap = widget.user.data() as Map; bool hasPictureUrl = userMap.containsKey(Constants.dbFieldUsersProfilePic); bool hasName = userMap.containsKey(Constants.dbFieldUsersName); bool hasBio = userMap.containsKey(Constants.dbFieldUsersBio); String? shortDist = (_otherUser != null ? shortestDistanceBetweenUsers(widget.currentUser, _otherUser!) .toStringAsFixed(0) : null); return Card( margin: const EdgeInsets.all(8.0), child: ListTile( 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'), ], ), title: hasName ? Text( '${widget.user[Constants.dbFieldUsersName]}', overflow: TextOverflow.ellipsis, maxLines: 1, ) : null, subtitle: hasBio ? 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, ), ], ), ), ); } }