Crop image in personalize

This commit is contained in:
LittleSheep 2024-08-02 00:12:16 +08:00
parent 5e754ad233
commit 0a5604d0ff
2 changed files with 45 additions and 22 deletions

View File

@ -3,6 +3,7 @@ import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart'; import 'package:flutter_animate/flutter_animate.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart'; import 'package:image_picker/image_picker.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:solian/exts.dart'; import 'package:solian/exts.dart';
@ -50,26 +51,24 @@ class _PersonalizeScreenState extends State<PersonalizeScreen> {
} }
void _syncWidget() async { void _syncWidget() async {
setState(() => _isBusy = true); _isBusy = true;
final AuthProvider auth = Get.find(); final AuthProvider auth = Get.find();
final prof = auth.userProfile.value!; final prof = auth.userProfile.value!;
setState(() { _usernameController.text = prof['name'];
_usernameController.text = prof['name']; _nicknameController.text = prof['nick'];
_nicknameController.text = prof['nick']; _descriptionController.text = prof['description'];
_descriptionController.text = prof['description']; _firstNameController.text = prof['profile']['first_name'];
_firstNameController.text = prof['profile']['first_name']; _lastNameController.text = prof['profile']['last_name'];
_lastNameController.text = prof['profile']['last_name']; _avatar = prof['avatar'];
_avatar = prof['avatar']; _banner = prof['banner'];
_banner = prof['banner']; if (prof['profile']['birthday'] != null) {
if (prof['profile']['birthday'] != null) { _birthday = DateTime.parse(prof['profile']['birthday']);
_birthday = DateTime.parse(prof['profile']['birthday']); _birthdayController.text =
_birthdayController.text = DateFormat('yyyy-MM-dd').format(_birthday!.toLocal());
DateFormat('yyyy-MM-dd').format(_birthday!.toLocal()); }
}
_isBusy = false; _isBusy = false;
});
} }
Future<void> _editImage(String position) async { Future<void> _editImage(String position) async {
@ -79,15 +78,40 @@ class _PersonalizeScreenState extends State<PersonalizeScreen> {
final image = await _imagePicker.pickImage(source: ImageSource.gallery); final image = await _imagePicker.pickImage(source: ImageSource.gallery);
if (image == null) return; if (image == null) return;
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);
setState(() => _isBusy = true); setState(() => _isBusy = true);
final AttachmentProvider provider = Get.find(); final AttachmentProvider provider = Get.find();
Response? attachResp; Response? attachResp;
try { try {
final file = File(image.path);
attachResp = await provider.createAttachment( attachResp = await provider.createAttachment(
await file.readAsBytes(), file.path, 'p.$position', null); await file.readAsBytes(),
file.path,
'p.$position',
null,
);
} catch (e) { } catch (e) {
setState(() => _isBusy = false); setState(() => _isBusy = false);
context.showErrorDialog(e); context.showErrorDialog(e);
@ -142,8 +166,7 @@ class _PersonalizeScreenState extends State<PersonalizeScreen> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_syncWidget();
Future.delayed(Duration.zero, () => _syncWidget());
} }
@override @override

View File

@ -211,8 +211,8 @@ class _AttachmentEditorPopupState extends State<AttachmentEditorPopup> {
uiSettings: [ uiSettings: [
AndroidUiSettings( AndroidUiSettings(
toolbarTitle: 'cropImage'.tr, toolbarTitle: 'cropImage'.tr,
toolbarColor: Colors.deepOrange, toolbarColor: Theme.of(context).colorScheme.primary,
toolbarWidgetColor: Colors.white, toolbarWidgetColor: Theme.of(context).colorScheme.onPrimary,
aspectRatioPresets: CropAspectRatioPreset.values, aspectRatioPresets: CropAspectRatioPreset.values,
), ),
IOSUiSettings( IOSUiSettings(