refactor: unify storage config keys and refresh local dev setup
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import '../../../../core/theme/design_tokens.dart';
|
||||
import '../../../../core/di/injection.dart';
|
||||
import '../../../../shared/widgets/app_button.dart';
|
||||
@@ -24,10 +26,13 @@ class _EditProfileScreenState extends State<EditProfileScreen> {
|
||||
final _bioController = TextEditingController();
|
||||
final _usersApi = sl<UsersApi>();
|
||||
final _userCache = sl<SettingsUserCache>();
|
||||
final _imagePicker = ImagePicker();
|
||||
|
||||
UserResponse? _user;
|
||||
File? _selectedAvatar;
|
||||
bool _isLoading = true;
|
||||
bool _isSaving = false;
|
||||
bool _isUploadingAvatar = false;
|
||||
bool _hasChanges = false;
|
||||
|
||||
@override
|
||||
@@ -73,27 +78,74 @@ class _EditProfileScreenState extends State<EditProfileScreen> {
|
||||
if (_user == null) return;
|
||||
final usernameChanged = _usernameController.text != _user!.username;
|
||||
final bioChanged = _bioController.text != (_user!.bio ?? '');
|
||||
final avatarChanged = _selectedAvatar != null;
|
||||
|
||||
if ((usernameChanged || bioChanged) != _hasChanges) {
|
||||
if ((usernameChanged || bioChanged || avatarChanged) != _hasChanges) {
|
||||
setState(() {
|
||||
_hasChanges = usernameChanged || bioChanged;
|
||||
_hasChanges = usernameChanged || bioChanged || avatarChanged;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _pickAvatar() async {
|
||||
final pickedFile = await _imagePicker.pickImage(
|
||||
source: ImageSource.gallery,
|
||||
maxWidth: 400,
|
||||
maxHeight: 400,
|
||||
imageQuality: 80,
|
||||
);
|
||||
if (pickedFile != null) {
|
||||
setState(() {
|
||||
_selectedAvatar = File(pickedFile.path);
|
||||
_hasChanges = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _uploadAvatar() async {
|
||||
if (_selectedAvatar == null) return;
|
||||
|
||||
setState(() {
|
||||
_isUploadingAvatar = true;
|
||||
});
|
||||
|
||||
try {
|
||||
await _usersApi.uploadAvatar(_selectedAvatar!);
|
||||
if (mounted) {
|
||||
Toast.show(context, '头像上传成功', type: ToastType.success);
|
||||
_selectedAvatar = null;
|
||||
await _loadUser();
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
Toast.show(context, '头像上传失败,请重试', type: ToastType.error);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isUploadingAvatar = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _saveProfile() async {
|
||||
if (!_hasChanges || _user == null) return;
|
||||
|
||||
final newUsername = _usernameController.text.trim();
|
||||
final newBio = _bioController.text.trim();
|
||||
final usernameChanged = newUsername != _user!.username;
|
||||
final bioChanged = newBio != (_user!.bio ?? '');
|
||||
|
||||
if (newUsername.isEmpty) {
|
||||
Toast.show(context, '用户名不能为空', type: ToastType.warning);
|
||||
return;
|
||||
}
|
||||
if (newUsername.length < 3 || newUsername.length > 30) {
|
||||
Toast.show(context, '用户名需要3-30个字符', type: ToastType.warning);
|
||||
return;
|
||||
if (usernameChanged) {
|
||||
if (newUsername.isEmpty) {
|
||||
Toast.show(context, '用户名不能为空', type: ToastType.warning);
|
||||
return;
|
||||
}
|
||||
if (newUsername.length < 3 || newUsername.length > 30) {
|
||||
Toast.show(context, '用户名需要3-30个字符', type: ToastType.warning);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
setState(() {
|
||||
@@ -101,12 +153,18 @@ class _EditProfileScreenState extends State<EditProfileScreen> {
|
||||
});
|
||||
|
||||
try {
|
||||
final request = UserUpdateRequest(
|
||||
username: newUsername,
|
||||
bio: newBio.isEmpty ? null : newBio,
|
||||
);
|
||||
final updatedUser = await _usersApi.updateMe(request);
|
||||
_userCache.set(updatedUser);
|
||||
if (_selectedAvatar != null) {
|
||||
await _uploadAvatar();
|
||||
}
|
||||
|
||||
if (usernameChanged || bioChanged) {
|
||||
final request = UserUpdateRequest(
|
||||
username: usernameChanged ? newUsername : null,
|
||||
bio: bioChanged ? (newBio.isEmpty ? null : newBio) : null,
|
||||
);
|
||||
final updatedUser = await _usersApi.updateMe(request);
|
||||
_userCache.set(updatedUser);
|
||||
}
|
||||
|
||||
if (mounted) {
|
||||
Toast.show(context, '保存成功', type: ToastType.success);
|
||||
@@ -171,6 +229,8 @@ class _EditProfileScreenState extends State<EditProfileScreen> {
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildAvatarSection(),
|
||||
const SizedBox(height: AppSpacing.lg),
|
||||
const Text(
|
||||
'用户名',
|
||||
style: TextStyle(
|
||||
@@ -191,6 +251,87 @@ class _EditProfileScreenState extends State<EditProfileScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildAvatarSection() {
|
||||
final avatarUrl = _user?.avatarUrl;
|
||||
final hasSelectedAvatar = _selectedAvatar != null;
|
||||
|
||||
return Center(
|
||||
child: GestureDetector(
|
||||
onTap: _isUploadingAvatar ? null : _pickAvatar,
|
||||
child: Stack(
|
||||
children: [
|
||||
Container(
|
||||
width: 80,
|
||||
height: 80,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: AppColors.surfaceSecondary,
|
||||
border: Border.all(color: AppColors.borderTertiary, width: 2),
|
||||
image: hasSelectedAvatar
|
||||
? DecorationImage(
|
||||
image: FileImage(_selectedAvatar!),
|
||||
fit: BoxFit.cover,
|
||||
)
|
||||
: avatarUrl != null
|
||||
? DecorationImage(
|
||||
image: NetworkImage(avatarUrl),
|
||||
fit: BoxFit.cover,
|
||||
)
|
||||
: null,
|
||||
),
|
||||
child: !hasSelectedAvatar && avatarUrl == null
|
||||
? const Icon(
|
||||
Icons.person,
|
||||
size: 40,
|
||||
color: AppColors.slate400,
|
||||
)
|
||||
: null,
|
||||
),
|
||||
if (_isUploadingAvatar)
|
||||
Positioned.fill(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: Colors.black.withValues(alpha: 0.4),
|
||||
),
|
||||
child: const Center(
|
||||
child: SizedBox(
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
valueColor: AlwaysStoppedAnimation<Color>(
|
||||
AppColors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
child: Container(
|
||||
width: 28,
|
||||
height: 28,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: AppColors.blue500,
|
||||
border: Border.all(color: AppColors.white, width: 2),
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.camera_alt,
|
||||
size: 14,
|
||||
color: AppColors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBioSection() {
|
||||
return AccountSectionCard(
|
||||
title: '个人简介',
|
||||
|
||||
@@ -188,11 +188,6 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
width: 64,
|
||||
height: 64,
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [AppColors.blue100, AppColors.blue50],
|
||||
),
|
||||
borderRadius: BorderRadius.circular(32),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
@@ -207,11 +202,8 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
),
|
||||
],
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.person,
|
||||
size: 28,
|
||||
color: AppColors.blue600,
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: _buildAvatarImage(_user?.avatarUrl),
|
||||
),
|
||||
const SizedBox(width: AppSpacing.lg),
|
||||
Expanded(
|
||||
@@ -307,6 +299,61 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildAvatarImage(String? avatarUrl) {
|
||||
if (avatarUrl == null) {
|
||||
return Container(
|
||||
decoration: const BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [AppColors.blue100, AppColors.blue50],
|
||||
),
|
||||
),
|
||||
child: const Icon(Icons.person, size: 28, color: AppColors.blue600),
|
||||
);
|
||||
}
|
||||
return Image.network(
|
||||
avatarUrl,
|
||||
fit: BoxFit.cover,
|
||||
width: 64,
|
||||
height: 64,
|
||||
errorBuilder: (context, error, stackTrace) {
|
||||
return Container(
|
||||
decoration: const BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [AppColors.blue100, AppColors.blue50],
|
||||
),
|
||||
),
|
||||
child: const Icon(Icons.person, size: 28, color: AppColors.blue600),
|
||||
);
|
||||
},
|
||||
loadingBuilder: (context, child, loadingProgress) {
|
||||
if (loadingProgress == null) return child;
|
||||
return Container(
|
||||
decoration: const BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [AppColors.blue100, AppColors.blue50],
|
||||
),
|
||||
),
|
||||
child: const Center(
|
||||
child: SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
valueColor: AlwaysStoppedAnimation<Color>(AppColors.blue600),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
String _buildFriendsSubtitle() {
|
||||
if (_friendsCount == 0) {
|
||||
return '暂无联系人';
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:io';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:social_app/core/api/i_api_client.dart';
|
||||
import 'models/user_response.dart';
|
||||
|
||||
@@ -38,6 +40,28 @@ class UsersApi {
|
||||
return UserResponse.fromJson(response.data);
|
||||
}
|
||||
|
||||
Future<String> uploadAvatar(File file) async {
|
||||
final formData = FormData.fromMap({
|
||||
'file': await MultipartFile.fromFile(
|
||||
file.path,
|
||||
filename: file.path.split('/').last,
|
||||
),
|
||||
});
|
||||
final response = await _client.post<Map<String, dynamic>>(
|
||||
'$_prefix/me/avatar',
|
||||
data: formData,
|
||||
);
|
||||
final payload = response.data;
|
||||
if (payload is! Map<String, dynamic>) {
|
||||
throw StateError('Invalid /users/me/avatar response');
|
||||
}
|
||||
final url = payload['url'];
|
||||
if (url is! String) {
|
||||
throw StateError('Missing url in /users/me/avatar response');
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
Future<List<UserResponse>> searchUsers(String query) async {
|
||||
final response = await _client.post(
|
||||
'$_prefix/search',
|
||||
|
||||
Reference in New Issue
Block a user