From 0a5604d0ff8e7cc543cf4b76c2c236f67c072217 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 2 Aug 2024 00:12:16 +0800 Subject: [PATCH] :sparkles: Crop image in personalize --- lib/screens/account/personalize.dart | 63 +++++++++++++------ .../attachments/attachment_editor.dart | 4 +- 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/lib/screens/account/personalize.dart b/lib/screens/account/personalize.dart index ee66960..25c48ba 100644 --- a/lib/screens/account/personalize.dart +++ b/lib/screens/account/personalize.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_animate/flutter_animate.dart'; import 'package:get/get.dart'; +import 'package:image_cropper/image_cropper.dart'; import 'package:image_picker/image_picker.dart'; import 'package:intl/intl.dart'; import 'package:solian/exts.dart'; @@ -50,26 +51,24 @@ class _PersonalizeScreenState extends State { } void _syncWidget() async { - setState(() => _isBusy = true); + _isBusy = true; final AuthProvider auth = Get.find(); final prof = auth.userProfile.value!; - setState(() { - _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()); - } + _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; - }); + _isBusy = false; } Future _editImage(String position) async { @@ -79,15 +78,40 @@ class _PersonalizeScreenState extends State { final image = await _imagePicker.pickImage(source: ImageSource.gallery); 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); final AttachmentProvider provider = Get.find(); Response? attachResp; try { - final file = File(image.path); attachResp = await provider.createAttachment( - await file.readAsBytes(), file.path, 'p.$position', null); + await file.readAsBytes(), + file.path, + 'p.$position', + null, + ); } catch (e) { setState(() => _isBusy = false); context.showErrorDialog(e); @@ -142,8 +166,7 @@ class _PersonalizeScreenState extends State { @override void initState() { super.initState(); - - Future.delayed(Duration.zero, () => _syncWidget()); + _syncWidget(); } @override diff --git a/lib/widgets/attachments/attachment_editor.dart b/lib/widgets/attachments/attachment_editor.dart index 11ddadd..c061046 100644 --- a/lib/widgets/attachments/attachment_editor.dart +++ b/lib/widgets/attachments/attachment_editor.dart @@ -211,8 +211,8 @@ class _AttachmentEditorPopupState extends State { uiSettings: [ AndroidUiSettings( toolbarTitle: 'cropImage'.tr, - toolbarColor: Colors.deepOrange, - toolbarWidgetColor: Colors.white, + toolbarColor: Theme.of(context).colorScheme.primary, + toolbarWidgetColor: Theme.of(context).colorScheme.onPrimary, aspectRatioPresets: CropAspectRatioPreset.values, ), IOSUiSettings(