feat: 添加好友功能并集成 LiteLLM 代理服务
- 新增好友搜索、添加、好友列表功能 - 集成 LiteLLM 代理服务及多模型定价配置 - 更新 iOS CocoaPods 配置 - 更新 .gitignore 和环境变量配置
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import '../../../../core/di/injection.dart';
|
||||
import '../../../../core/theme/design_tokens.dart';
|
||||
import '../../../../shared/widgets/toast/index.dart';
|
||||
import '../../../../shared/widgets/page_header.dart' as widgets;
|
||||
import '../../../friends/data/friends_api.dart';
|
||||
import '../../../users/data/models/user_response.dart';
|
||||
import '../../../users/data/users_api.dart';
|
||||
|
||||
class ContactsScreen extends StatefulWidget {
|
||||
const ContactsScreen({super.key});
|
||||
@@ -12,37 +17,156 @@ class ContactsScreen extends StatefulWidget {
|
||||
|
||||
class _ContactsScreenState extends State<ContactsScreen> {
|
||||
final _searchController = TextEditingController();
|
||||
final _searchFocusNode = FocusNode();
|
||||
|
||||
final List<ContactItem> _recentContacts = [
|
||||
ContactItem(
|
||||
name: 'Toki',
|
||||
email: 'toki@xunmee.com',
|
||||
color: AppColors.blue500,
|
||||
),
|
||||
ContactItem(
|
||||
name: 'Mina',
|
||||
email: 'mina@xunmee.com',
|
||||
color: AppColors.violet600,
|
||||
),
|
||||
];
|
||||
List<FriendResponse> _friends = [];
|
||||
List<UserResponse> _searchResults = [];
|
||||
bool _isLoading = true;
|
||||
bool _isSearching = false;
|
||||
bool _hasSearched = false;
|
||||
final Set<String> _sentRequestIds = {};
|
||||
Set<String> _friendIds = {};
|
||||
|
||||
final List<ContactItem> _allContacts = [
|
||||
ContactItem(name: 'Aki', email: 'aki@xunmee.com', color: AppColors.blue600),
|
||||
ContactItem(
|
||||
name: 'Lynn',
|
||||
email: 'lynn@xunmee.com',
|
||||
color: const Color(0xFF0EA5E9),
|
||||
),
|
||||
ContactItem(
|
||||
name: 'Nora',
|
||||
email: 'nora@xunmee.com',
|
||||
color: AppColors.violet500,
|
||||
),
|
||||
];
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadFriends();
|
||||
}
|
||||
|
||||
Future<void> _loadFriends() async {
|
||||
try {
|
||||
final friendsApi = sl<FriendsApi>();
|
||||
final friends = await friendsApi.getFriends();
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_friends = friends;
|
||||
_friendIds = friends.map((f) => f.friend.id).toSet();
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool _isValidEmail(String email) {
|
||||
final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,}$');
|
||||
return emailRegex.hasMatch(email);
|
||||
}
|
||||
|
||||
Future<void> _onSearch() async {
|
||||
final query = _searchController.text.trim();
|
||||
|
||||
if (query.isEmpty) {
|
||||
Toast.show(context, '请输入邮箱地址', type: ToastType.warning);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_isValidEmail(query)) {
|
||||
Toast.show(context, '请输入有效的邮箱地址', type: ToastType.warning);
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isSearching = true;
|
||||
_searchResults = [];
|
||||
_hasSearched = true;
|
||||
});
|
||||
|
||||
try {
|
||||
final usersApi = sl<UsersApi>();
|
||||
final results = await usersApi.searchUsers(query);
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_searchResults = results;
|
||||
_isSearching = false;
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isSearching = false;
|
||||
});
|
||||
Toast.show(context, '搜索失败,请稍后重试', type: ToastType.error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _sendFriendRequest(String targetUserId, String? content) async {
|
||||
try {
|
||||
final friendsApi = sl<FriendsApi>();
|
||||
await friendsApi.sendRequest(targetUserId);
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_sentRequestIds.add(targetUserId);
|
||||
});
|
||||
Toast.show(context, '好友请求已发送', type: ToastType.success);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
Toast.show(context, '发送失败,请稍后重试', type: ToastType.error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _showAddFriendDialog(UserResponse user) {
|
||||
final controller = TextEditingController();
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (dialogContext) => AlertDialog(
|
||||
title: const Text('添加好友'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('向 ${user.username} 发送好友请求'),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: controller,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '验证消息(可选)',
|
||||
hintText: '你好,我是...',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
maxLines: 3,
|
||||
maxLength: 200,
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
controller.dispose();
|
||||
Navigator.pop(dialogContext);
|
||||
},
|
||||
child: const Text('取消'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(dialogContext);
|
||||
_sendFriendRequest(
|
||||
user.id,
|
||||
controller.text.isEmpty ? null : controller.text,
|
||||
);
|
||||
},
|
||||
child: const Text('发送'),
|
||||
),
|
||||
],
|
||||
),
|
||||
).then((_) => controller.dispose());
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_searchController.dispose();
|
||||
_searchFocusNode.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -61,14 +185,21 @@ class _ContactsScreenState extends State<ContactsScreen> {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildSearchRow(),
|
||||
const SizedBox(height: 16),
|
||||
_buildSectionTitle('最近联系'),
|
||||
const SizedBox(height: 8),
|
||||
_buildContactCard(_recentContacts),
|
||||
_buildSearchResults(),
|
||||
const SizedBox(height: 16),
|
||||
_buildSectionTitle('全部联系人'),
|
||||
const SizedBox(height: 8),
|
||||
_buildContactCard(_allContacts),
|
||||
if (_isLoading)
|
||||
const Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(20),
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
)
|
||||
else if (_friends.isEmpty)
|
||||
_buildEmptyState()
|
||||
else
|
||||
_buildContactCard(_friends),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -83,36 +214,52 @@ class _ContactsScreenState extends State<ContactsScreen> {
|
||||
return Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: GestureDetector(
|
||||
onTap: () {},
|
||||
child: Container(
|
||||
height: 40,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surfaceTertiary,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: const Color(0xFFE4EBF7)),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.search, size: 16, color: AppColors.slate400),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'搜索联系人',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: AppColors.slate400,
|
||||
),
|
||||
),
|
||||
],
|
||||
child: Container(
|
||||
height: 40,
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surfaceTertiary,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: const Color(0xFFE4EBF7)),
|
||||
),
|
||||
child: TextField(
|
||||
controller: _searchController,
|
||||
focusNode: _searchFocusNode,
|
||||
decoration: const InputDecoration(
|
||||
hintText: '输入邮箱搜索用户',
|
||||
hintStyle: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: AppColors.slate400,
|
||||
),
|
||||
prefixIcon: Icon(
|
||||
Icons.search,
|
||||
size: 16,
|
||||
color: AppColors.slate400,
|
||||
),
|
||||
border: InputBorder.none,
|
||||
contentPadding: EdgeInsets.symmetric(
|
||||
horizontal: 12,
|
||||
vertical: 10,
|
||||
),
|
||||
),
|
||||
style: const TextStyle(fontSize: 13),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
textInputAction: TextInputAction.search,
|
||||
onSubmitted: (_) => _onSearch(),
|
||||
onChanged: (value) {
|
||||
if (value.isEmpty) {
|
||||
setState(() {
|
||||
_hasSearched = false;
|
||||
_searchResults = [];
|
||||
});
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
GestureDetector(
|
||||
onTap: () => context.push('/contacts/add'),
|
||||
onTap: _onSearch,
|
||||
child: Container(
|
||||
width: 40,
|
||||
height: 40,
|
||||
@@ -121,17 +268,233 @@ class _ContactsScreenState extends State<ContactsScreen> {
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: const Color(0xFFD7E6FF)),
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.person_add,
|
||||
size: 16,
|
||||
color: AppColors.blue500,
|
||||
),
|
||||
child: _isSearching
|
||||
? const Padding(
|
||||
padding: EdgeInsets.all(10),
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.search, size: 16, color: AppColors.blue500),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSearchResults() {
|
||||
if (!_hasSearched) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(top: 8),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.white,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withValues(alpha: 0.08),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
if (_isSearching)
|
||||
Container(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: const Center(child: CircularProgressIndicator()),
|
||||
)
|
||||
else if (_searchResults.isEmpty)
|
||||
Container(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'未找到该用户',
|
||||
style: TextStyle(fontSize: 14, color: AppColors.slate500),
|
||||
),
|
||||
),
|
||||
)
|
||||
else
|
||||
Column(
|
||||
children: [
|
||||
for (int i = 0; i < _searchResults.length; i++) ...[
|
||||
_buildSearchResultItem(_searchResults[i]),
|
||||
if (i < _searchResults.length - 1)
|
||||
Container(
|
||||
height: 1,
|
||||
margin: const EdgeInsets.symmetric(horizontal: 14),
|
||||
color: const Color(0xFFEEF2F7),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSearchResultItem(UserResponse user) {
|
||||
final isFriend = _friendIds.contains(user.id);
|
||||
final isSent = _sentRequestIds.contains(user.id);
|
||||
final avatarColor = _getAvatarColor(user.id);
|
||||
|
||||
return Container(
|
||||
height: 70,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 42,
|
||||
height: 42,
|
||||
decoration: BoxDecoration(
|
||||
color: _getAvatarBackground(avatarColor),
|
||||
borderRadius: BorderRadius.circular(21),
|
||||
border: Border.all(color: _getAvatarBorder(avatarColor)),
|
||||
),
|
||||
child: user.avatarUrl != null
|
||||
? ClipRRect(
|
||||
borderRadius: BorderRadius.circular(21),
|
||||
child: Image.network(
|
||||
user.avatarUrl!,
|
||||
width: 42,
|
||||
height: 42,
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => Icon(
|
||||
Icons.person,
|
||||
size: 18,
|
||||
color: _getAvatarColor(user.id),
|
||||
),
|
||||
),
|
||||
)
|
||||
: Icon(Icons.person, size: 18, color: _getAvatarColor(user.id)),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
user.username,
|
||||
style: const TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.slate900,
|
||||
),
|
||||
),
|
||||
if (user.bio != null) ...[
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
user.bio!,
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: AppColors.slate500,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
_buildAddButton(user.id, isFriend, isSent),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildAddButton(String userId, bool isFriend, bool isSent) {
|
||||
if (isFriend) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.slate300,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Text(
|
||||
'已是好友',
|
||||
style: TextStyle(fontSize: 12, color: AppColors.slate500),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (isSent) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.slate300,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: const Text(
|
||||
'已发送',
|
||||
style: TextStyle(fontSize: 12, color: AppColors.slate500),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
final user = _searchResults.firstWhere((u) => u.id == userId);
|
||||
_showAddFriendDialog(user);
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFF1F7FF),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: const Color(0xFFD7E6FF)),
|
||||
),
|
||||
child: const Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.person_add, size: 14, color: AppColors.blue500),
|
||||
SizedBox(width: 4),
|
||||
Text(
|
||||
'添加',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: AppColors.blue500,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmptyState() {
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(32),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.white,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(color: const Color(0xFFE3EAF6)),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
const Icon(Icons.person_outline, size: 48, color: AppColors.slate400),
|
||||
const SizedBox(height: 12),
|
||||
const Text(
|
||||
'暂无联系人',
|
||||
style: TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: AppColors.slate500,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
const Text(
|
||||
'搜索邮箱添加好友开始聊天吧',
|
||||
style: TextStyle(fontSize: 13, color: AppColors.slate400),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSectionTitle(String title) {
|
||||
return Text(
|
||||
title,
|
||||
@@ -143,7 +506,7 @@ class _ContactsScreenState extends State<ContactsScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContactCard(List<ContactItem> contacts) {
|
||||
Widget _buildContactCard(List<FriendResponse> friends) {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.white,
|
||||
@@ -152,9 +515,9 @@ class _ContactsScreenState extends State<ContactsScreen> {
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
for (int i = 0; i < contacts.length; i++) ...[
|
||||
_buildContactItem(contacts[i]),
|
||||
if (i < contacts.length - 1)
|
||||
for (int i = 0; i < friends.length; i++) ...[
|
||||
_buildContactItem(friends[i]),
|
||||
if (i < friends.length - 1)
|
||||
Container(
|
||||
height: 1,
|
||||
margin: const EdgeInsets.symmetric(horizontal: 14),
|
||||
@@ -166,9 +529,12 @@ class _ContactsScreenState extends State<ContactsScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContactItem(ContactItem contact) {
|
||||
Widget _buildContactItem(FriendResponse friend) {
|
||||
final friendInfo = friend.friend;
|
||||
final color = _getAvatarColor(friendInfo.id);
|
||||
|
||||
return GestureDetector(
|
||||
onTap: () => context.push('/contacts/add?id=${contact.email}'),
|
||||
onTap: () => context.push('/contacts/add?id=${friendInfo.id}'),
|
||||
child: Container(
|
||||
height: 70,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14),
|
||||
@@ -178,11 +544,23 @@ class _ContactsScreenState extends State<ContactsScreen> {
|
||||
width: 42,
|
||||
height: 42,
|
||||
decoration: BoxDecoration(
|
||||
color: _getAvatarBackground(contact.color),
|
||||
color: _getAvatarBackground(color),
|
||||
borderRadius: BorderRadius.circular(21),
|
||||
border: Border.all(color: _getAvatarBorder(contact.color)),
|
||||
border: Border.all(color: _getAvatarBorder(color)),
|
||||
),
|
||||
child: Icon(Icons.person, size: 18, color: contact.color),
|
||||
child: friendInfo.avatarUrl != null
|
||||
? ClipRRect(
|
||||
borderRadius: BorderRadius.circular(21),
|
||||
child: Image.network(
|
||||
friendInfo.avatarUrl!,
|
||||
width: 42,
|
||||
height: 42,
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) =>
|
||||
Icon(Icons.person, size: 18, color: color),
|
||||
),
|
||||
)
|
||||
: Icon(Icons.person, size: 18, color: color),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
@@ -191,22 +569,13 @@ class _ContactsScreenState extends State<ContactsScreen> {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
contact.name,
|
||||
friendInfo.username,
|
||||
style: const TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.slate900,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
contact.email,
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: AppColors.slate500,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -216,6 +585,18 @@ class _ContactsScreenState extends State<ContactsScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Color _getAvatarColor(String id) {
|
||||
final colors = [
|
||||
AppColors.blue500,
|
||||
AppColors.violet600,
|
||||
AppColors.blue600,
|
||||
const Color(0xFF0EA5E9),
|
||||
AppColors.violet500,
|
||||
];
|
||||
final index = id.hashCode.abs() % colors.length;
|
||||
return colors[index];
|
||||
}
|
||||
|
||||
Color _getAvatarBackground(Color color) {
|
||||
if (color == AppColors.blue500) return const Color(0xFFEEF4FF);
|
||||
if (color == AppColors.violet600) return AppColors.surfaceInfoLight;
|
||||
@@ -234,11 +615,3 @@ class _ContactsScreenState extends State<ContactsScreen> {
|
||||
return const Color(0xFFDDE8FB);
|
||||
}
|
||||
}
|
||||
|
||||
class ContactItem {
|
||||
final String name;
|
||||
final String email;
|
||||
final Color color;
|
||||
|
||||
ContactItem({required this.name, required this.email, required this.color});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user