2024-05-22 15:18:01 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_animate/flutter_animate.dart';
|
|
|
|
import 'package:get/get.dart';
|
2024-08-01 16:12:16 +00:00
|
|
|
import 'package:image_cropper/image_cropper.dart';
|
2024-05-22 15:18:01 +00:00
|
|
|
import 'package:image_picker/image_picker.dart';
|
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:solian/exts.dart';
|
|
|
|
import 'package:solian/providers/auth.dart';
|
|
|
|
import 'package:solian/providers/content/attachment.dart';
|
|
|
|
import 'package:solian/services.dart';
|
|
|
|
import 'package:solian/widgets/account/account_avatar.dart';
|
|
|
|
|
|
|
|
class PersonalizeScreen extends StatefulWidget {
|
|
|
|
const PersonalizeScreen({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<PersonalizeScreen> createState() => _PersonalizeScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _PersonalizeScreenState extends State<PersonalizeScreen> {
|
|
|
|
final _imagePicker = ImagePicker();
|
|
|
|
|
|
|
|
final _usernameController = TextEditingController();
|
|
|
|
final _nicknameController = TextEditingController();
|
|
|
|
final _firstNameController = TextEditingController();
|
|
|
|
final _lastNameController = TextEditingController();
|
|
|
|
final _descriptionController = TextEditingController();
|
|
|
|
final _birthdayController = TextEditingController();
|
|
|
|
|
|
|
|
int? _avatar;
|
|
|
|
int? _banner;
|
|
|
|
DateTime? _birthday;
|
|
|
|
|
|
|
|
bool _isBusy = false;
|
|
|
|
|
2024-08-01 07:49:42 +00:00
|
|
|
void _selectBirthday() async {
|
2024-05-22 15:18:01 +00:00
|
|
|
final DateTime? picked = await showDatePicker(
|
|
|
|
context: context,
|
2024-05-22 16:11:54 +00:00
|
|
|
initialDate: _birthday?.toLocal(),
|
2024-05-22 15:18:01 +00:00
|
|
|
firstDate: DateTime(DateTime.now().year - 200),
|
2024-05-22 16:11:54 +00:00
|
|
|
lastDate: DateTime(DateTime.now().year),
|
2024-05-22 15:18:01 +00:00
|
|
|
);
|
|
|
|
if (picked != null && picked != _birthday) {
|
|
|
|
setState(() {
|
|
|
|
_birthday = picked;
|
2024-06-27 03:44:27 +00:00
|
|
|
_birthdayController.text = DateFormat('y/M/d').format(_birthday!);
|
2024-05-22 15:18:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-01 07:49:42 +00:00
|
|
|
void _syncWidget() async {
|
2024-08-01 16:12:16 +00:00
|
|
|
_isBusy = true;
|
2024-05-22 15:18:01 +00:00
|
|
|
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-07-24 17:18:47 +00:00
|
|
|
final prof = auth.userProfile.value!;
|
2024-08-01 16:12:16 +00:00
|
|
|
_usernameController.text = prof['name'];
|
|
|
|
_nicknameController.text = prof['nick'];
|
|
|
|
_descriptionController.text = prof['description'];
|
|
|
|
_firstNameController.text = prof['profile']['first_name'];
|
|
|
|
_lastNameController.text = prof['profile']['last_name'];
|
|
|
|
_avatar = prof['avatar'];
|
|
|
|
_banner = prof['banner'];
|
|
|
|
if (prof['profile']['birthday'] != null) {
|
|
|
|
_birthday = DateTime.parse(prof['profile']['birthday']);
|
|
|
|
_birthdayController.text =
|
|
|
|
DateFormat('yyyy-MM-dd').format(_birthday!.toLocal());
|
|
|
|
}
|
|
|
|
|
|
|
|
_isBusy = false;
|
2024-05-22 15:18:01 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 07:49:42 +00:00
|
|
|
Future<void> _editImage(String position) async {
|
2024-05-22 15:18:01 +00:00
|
|
|
final AuthProvider auth = Get.find();
|
2024-07-24 17:18:47 +00:00
|
|
|
if (auth.isAuthorized.isFalse) return;
|
2024-05-22 15:18:01 +00:00
|
|
|
|
|
|
|
final image = await _imagePicker.pickImage(source: ImageSource.gallery);
|
|
|
|
if (image == null) return;
|
|
|
|
|
2024-08-01 16:12:16 +00:00
|
|
|
CroppedFile? croppedFile = await ImageCropper().cropImage(
|
|
|
|
sourcePath: image.path,
|
|
|
|
uiSettings: [
|
|
|
|
AndroidUiSettings(
|
|
|
|
toolbarTitle: 'cropImage'.tr,
|
|
|
|
toolbarColor: Theme.of(context).colorScheme.primary,
|
|
|
|
toolbarWidgetColor: Theme.of(context).colorScheme.onPrimary,
|
|
|
|
aspectRatioPresets: [CropAspectRatioPreset.square],
|
|
|
|
),
|
|
|
|
IOSUiSettings(
|
|
|
|
title: 'cropImage'.tr,
|
|
|
|
aspectRatioPresets: [CropAspectRatioPreset.square],
|
|
|
|
),
|
|
|
|
WebUiSettings(
|
|
|
|
context: context,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
if (croppedFile == null) return;
|
|
|
|
final file = File(croppedFile.path);
|
|
|
|
|
2024-05-22 15:18:01 +00:00
|
|
|
setState(() => _isBusy = true);
|
|
|
|
|
|
|
|
final AttachmentProvider provider = Get.find();
|
|
|
|
|
2024-06-23 11:41:50 +00:00
|
|
|
Response? attachResp;
|
2024-05-22 15:18:01 +00:00
|
|
|
try {
|
|
|
|
attachResp = await provider.createAttachment(
|
2024-08-01 16:12:16 +00:00
|
|
|
await file.readAsBytes(),
|
|
|
|
file.path,
|
|
|
|
'p.$position',
|
|
|
|
null,
|
|
|
|
);
|
2024-05-22 15:18:01 +00:00
|
|
|
} catch (e) {
|
|
|
|
setState(() => _isBusy = false);
|
|
|
|
context.showErrorDialog(e);
|
2024-06-23 11:41:50 +00:00
|
|
|
return;
|
2024-05-22 15:18:01 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
final client = auth.configureClient('auth');
|
2024-05-22 15:18:01 +00:00
|
|
|
|
|
|
|
final resp = await client.put(
|
2024-07-16 11:46:53 +00:00
|
|
|
'/users/me/$position',
|
2024-05-22 15:18:01 +00:00
|
|
|
{'attachment': attachResp.body['id']},
|
|
|
|
);
|
|
|
|
if (resp.statusCode == 200) {
|
2024-08-01 07:49:42 +00:00
|
|
|
_syncWidget();
|
2024-05-22 16:11:54 +00:00
|
|
|
context.showSnackbar('accountPersonalizeApplied'.tr);
|
|
|
|
} else {
|
|
|
|
context.showErrorDialog(resp.bodyString);
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() => _isBusy = false);
|
|
|
|
}
|
|
|
|
|
2024-08-01 07:49:42 +00:00
|
|
|
void _editUserInfo() async {
|
2024-05-22 16:11:54 +00:00
|
|
|
final AuthProvider auth = Get.find();
|
2024-07-24 17:18:47 +00:00
|
|
|
if (auth.isAuthorized.isFalse) return;
|
2024-05-22 15:18:01 +00:00
|
|
|
|
2024-05-22 16:11:54 +00:00
|
|
|
setState(() => _isBusy = true);
|
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
final client = auth.configureClient('auth');
|
2024-05-22 16:11:54 +00:00
|
|
|
|
|
|
|
_birthday?.toIso8601String();
|
|
|
|
final resp = await client.put(
|
2024-07-16 11:46:53 +00:00
|
|
|
'/users/me',
|
2024-05-22 16:11:54 +00:00
|
|
|
{
|
|
|
|
'nick': _nicknameController.value.text,
|
|
|
|
'description': _descriptionController.value.text,
|
|
|
|
'first_name': _firstNameController.value.text,
|
|
|
|
'last_name': _lastNameController.value.text,
|
|
|
|
'birthday': _birthday?.toUtc().toIso8601String(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
if (resp.statusCode == 200) {
|
2024-08-01 07:49:42 +00:00
|
|
|
_syncWidget();
|
2024-05-22 15:18:01 +00:00
|
|
|
context.showSnackbar('accountPersonalizeApplied'.tr);
|
|
|
|
} else {
|
|
|
|
context.showErrorDialog(resp.bodyString);
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() => _isBusy = false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-08-01 16:12:16 +00:00
|
|
|
_syncWidget();
|
2024-05-22 15:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-06-03 15:36:46 +00:00
|
|
|
const double padding = 32;
|
|
|
|
|
2024-05-22 15:18:01 +00:00
|
|
|
return Material(
|
|
|
|
color: Theme.of(context).colorScheme.surface,
|
2024-06-03 15:36:46 +00:00
|
|
|
child: ListView(
|
|
|
|
children: [
|
|
|
|
if (_isBusy) const LinearProgressIndicator().animate().scaleX(),
|
|
|
|
const SizedBox(height: 24),
|
|
|
|
Stack(
|
|
|
|
children: [
|
|
|
|
AccountAvatar(content: _avatar, radius: 40),
|
|
|
|
Positioned(
|
|
|
|
bottom: 0,
|
|
|
|
left: 40,
|
|
|
|
child: FloatingActionButton.small(
|
|
|
|
heroTag: const Key('avatar-editor'),
|
2024-08-01 07:49:42 +00:00
|
|
|
onPressed: () => _editImage('avatar'),
|
2024-06-03 15:36:46 +00:00
|
|
|
child: const Icon(
|
|
|
|
Icons.camera,
|
2024-05-22 15:18:01 +00:00
|
|
|
),
|
|
|
|
),
|
2024-06-03 15:36:46 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
).paddingSymmetric(horizontal: padding),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
Stack(
|
|
|
|
children: [
|
|
|
|
ClipRRect(
|
|
|
|
borderRadius: const BorderRadius.all(Radius.circular(8)),
|
|
|
|
child: AspectRatio(
|
|
|
|
aspectRatio: 16 / 9,
|
|
|
|
child: Container(
|
|
|
|
color: Theme.of(context).colorScheme.surfaceContainerHigh,
|
|
|
|
child: _banner != null
|
|
|
|
? Image.network(
|
2024-08-01 07:49:42 +00:00
|
|
|
ServiceFinder.buildUrl(
|
|
|
|
'files', '/attachments/$_banner'),
|
2024-06-03 15:36:46 +00:00
|
|
|
fit: BoxFit.cover,
|
|
|
|
loadingBuilder: (BuildContext context, Widget child,
|
|
|
|
ImageChunkEvent? loadingProgress) {
|
|
|
|
if (loadingProgress == null) return child;
|
|
|
|
return Center(
|
|
|
|
child: CircularProgressIndicator(
|
|
|
|
value: loadingProgress.expectedTotalBytes !=
|
|
|
|
null
|
|
|
|
? loadingProgress.cumulativeBytesLoaded /
|
|
|
|
loadingProgress.expectedTotalBytes!
|
|
|
|
: null,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
: Container(),
|
2024-05-22 15:18:01 +00:00
|
|
|
),
|
|
|
|
),
|
2024-06-03 15:36:46 +00:00
|
|
|
),
|
|
|
|
Positioned(
|
|
|
|
bottom: 16,
|
|
|
|
right: 16,
|
|
|
|
child: FloatingActionButton(
|
|
|
|
heroTag: const Key('banner-editor'),
|
2024-08-01 07:49:42 +00:00
|
|
|
onPressed: () => _editImage('banner'),
|
2024-06-03 15:36:46 +00:00
|
|
|
child: const Icon(
|
|
|
|
Icons.camera_alt,
|
2024-05-22 15:18:01 +00:00
|
|
|
),
|
|
|
|
),
|
2024-06-03 15:36:46 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
).paddingSymmetric(horizontal: padding),
|
|
|
|
const SizedBox(height: 24),
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Flexible(
|
|
|
|
flex: 1,
|
|
|
|
child: TextField(
|
|
|
|
readOnly: true,
|
|
|
|
controller: _usernameController,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: 'username'.tr,
|
|
|
|
prefixText: '@',
|
2024-05-22 15:18:01 +00:00
|
|
|
),
|
|
|
|
),
|
2024-06-03 15:36:46 +00:00
|
|
|
),
|
|
|
|
const SizedBox(width: 16),
|
|
|
|
Flexible(
|
|
|
|
flex: 1,
|
|
|
|
child: TextField(
|
|
|
|
controller: _nicknameController,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: 'nickname'.tr,
|
2024-05-22 15:18:01 +00:00
|
|
|
),
|
|
|
|
),
|
2024-06-03 15:36:46 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
).paddingSymmetric(horizontal: padding),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Flexible(
|
|
|
|
flex: 1,
|
|
|
|
child: TextField(
|
|
|
|
controller: _firstNameController,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: 'firstName'.tr,
|
2024-05-22 15:18:01 +00:00
|
|
|
),
|
|
|
|
),
|
2024-06-03 15:36:46 +00:00
|
|
|
),
|
|
|
|
const SizedBox(width: 16),
|
|
|
|
Flexible(
|
|
|
|
flex: 1,
|
|
|
|
child: TextField(
|
|
|
|
controller: _lastNameController,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: 'lastName'.tr,
|
2024-05-22 15:18:01 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-06-03 15:36:46 +00:00
|
|
|
],
|
|
|
|
).paddingSymmetric(horizontal: padding),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
TextField(
|
|
|
|
controller: _descriptionController,
|
|
|
|
keyboardType: TextInputType.multiline,
|
|
|
|
maxLines: null,
|
|
|
|
minLines: 3,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: 'description'.tr,
|
2024-05-22 15:18:01 +00:00
|
|
|
),
|
2024-06-03 15:36:46 +00:00
|
|
|
).paddingSymmetric(horizontal: padding),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
TextField(
|
|
|
|
controller: _birthdayController,
|
|
|
|
readOnly: true,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: 'birthday'.tr,
|
2024-05-22 15:18:01 +00:00
|
|
|
),
|
2024-08-01 07:49:42 +00:00
|
|
|
onTap: () => _selectBirthday(),
|
2024-06-03 15:36:46 +00:00
|
|
|
).paddingSymmetric(horizontal: padding),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
children: [
|
|
|
|
TextButton(
|
2024-08-01 07:49:42 +00:00
|
|
|
onPressed: _isBusy ? null : () => _syncWidget(),
|
2024-06-03 15:36:46 +00:00
|
|
|
child: Text('reset'.tr),
|
|
|
|
),
|
|
|
|
ElevatedButton(
|
2024-08-01 07:49:42 +00:00
|
|
|
onPressed: _isBusy ? null : () => _editUserInfo(),
|
2024-06-03 15:36:46 +00:00
|
|
|
child: Text('apply'.tr),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
).paddingSymmetric(horizontal: padding),
|
|
|
|
],
|
2024-05-22 15:18:01 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-07-09 14:39:44 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_usernameController.dispose();
|
|
|
|
_nicknameController.dispose();
|
|
|
|
_firstNameController.dispose();
|
|
|
|
_lastNameController.dispose();
|
|
|
|
_descriptionController.dispose();
|
|
|
|
_birthdayController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
2024-05-22 15:18:01 +00:00
|
|
|
}
|