选择相册裁剪并中文

<?xml version="1.0" encoding="utf-8"?>

<resources xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="MissingTranslation">

    <string name="app_name">同频</string>
    <string name="ucrop_rotate">旋转</string> <string name="ucrop_scale">縮放</string> <string name="ucrop_crop">裁剪</string>
</resources>



static void showAvatarSelectDialog(BuildContext context,
      {bool crop = false,
        bool isAvatar=true,
      ValueChanged<String>? selectCallback,
      bool IdImage = false,
      Function? viewCallback}) {
    showModalBottomSheet(
        context: context,
        backgroundColor: Colours.transparent,
        builder: (context) {
          return AvatarSelectDialog(
            crop: crop,
            selectCallback: selectCallback,
            IdImage: IdImage,
            isAvatar:isAvatar,
            viewCallback: viewCallback,
          );
        });
  }

import ‘dart:async’;
import ‘dart:io’;

import ‘package:flutter/material.dart’;
import ‘package:flutter_easyloading/flutter_easyloading.dart’;
import ‘package:get/get.dart’;
import ‘package:image_cropper/image_cropper.dart’;
import ‘package:image_picker/image_picker.dart’;
import ‘package:permission_handler/permission_handler.dart’;
import ‘package:toptongpin/app/res/colors.dart’;
import ‘package:toptongpin/app/res/gaps.dart’;
import ‘package:toptongpin/app/res/styles.dart’;
import ‘package:toptongpin/app/utils/log_util.dart’;
import ‘package:toptongpin/app/utils/object_util.dart’;
import ‘package:toptongpin/app/utils/permission.dart’;
import ‘package:toptongpin/app/utils/screen_util.dart’;

import ‘…/…/…/utils/image_util.dart’;

class AvatarSelectDialog extends StatelessWidget {
final bool crop;
final bool IdImage;
final ValueChanged? selectCallback;
final Function? viewCallback;
final bool isAvatar;

final ImagePicker _picker = ImagePicker();

AvatarSelectDialog(
{Key? key,
this.crop = false,
this.selectCallback,
this.viewCallback,
this.isAvatar=true,
this.IdImage = false})
: super(key: key);

Future<String?> _selectImage(ImageSource source) async {
if ((source == ImageSource.camera &&
await Permissions.checkPermission(
Get.context!, Permission.camera.value)) ||
source == ImageSource.gallery &&
(Platform.isIOS
? await Permissions.checkPermission(
Get.context!, Permission.photos.value)
: await Permissions.checkPermission(
Get.context!, Permission.storage.value))) {
EasyLoading.show();
XFile? _pickedFile = await _picker.pickImage(source: source);
String? path = _pickedFile?.path;
File? croppedFile;
if (crop && _pickedFile != null) {
if(isAvatar){
croppedFile = await ImageCropper.cropImage(
sourcePath: _pickedFile.path,
aspectRatio:
CropAspectRatio(ratioX: IdImage ? 1.5 : 1.0, ratioY: 1.0),
maxWidth: 720,
maxHeight: 720,
// aspectRatioPresets: [
// CropAspectRatioPreset.square,
// CropAspectRatioPreset.ratio3x2,
// CropAspectRatioPreset.original,
// CropAspectRatioPreset.ratio4x3,
// CropAspectRatioPreset.ratio16x9,
// ],
androidUiSettings: AndroidUiSettings(hideBottomControls: false, toolbarTitle: ‘裁剪’,),
);
}else{
croppedFile = await ImageCropper.cropImage(
sourcePath: _pickedFile.path,
// aspectRatio:
// CropAspectRatio(ratioX: IdImage ? 1.5 : 1.0, ratioY: 1.0),
// maxWidth: 720,
// maxHeight: 720,
aspectRatioPresets: [
CropAspectRatioPreset.square,
CropAspectRatioPreset.ratio3x2,
CropAspectRatioPreset.original,
CropAspectRatioPreset.ratio4x3,
CropAspectRatioPreset.ratio16x9,
],
androidUiSettings: AndroidUiSettings(hideBottomControls: false, toolbarTitle: ‘裁剪’,),
);
}

    path = croppedFile?.path;
  }

  if (croppedFile != null) {
    final compress = await ImageUtil.imageCompressAndGetFile(croppedFile);
    path = compress?.path;
  }
  LogUtil.v(path ?? "null path");
  EasyLoading.dismiss();

  return path;
}

return "";

}

@override
Widget build(BuildContext context) {
return Container(
child: Column(
mainAxisSize: MainAxisSize.min, //wrap_content
children: [
Container(
margin: EdgeInsets.symmetric(horizontal: 12),
decoration: BoxDecoration(
color: Colours.white,
borderRadius: BorderRadius.all(Radius.circular(14.0)),
),
child: Column(
children: [
FlatButton(
onPressed: () {
_selectImage(ImageSource.camera).then((String? path) {
if (ObjectUtil.isNotEmpty(path)) {
Navigator.pop(context);
if (selectCallback != null) {
selectCallback!(path!);
}
}
});
},
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.vertical(top: Radius.circular(14.0))),
padding: EdgeInsets.all(0.0),
child: Container(
alignment: Alignment.center,
height: 60.0,
width: double.infinity,
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
width: 0.6, color: Colours.default_line))),
child: Text(
“拍照”,
style: TextStyles.textGray800_w400_16,
maxLines: 1,
overflow: TextOverflow.ellipsis,
))),
FlatButton(
onPressed: () {
_selectImage(ImageSource.gallery).then((path) {
if (ObjectUtil.isNotEmpty(path)) {
Navigator.pop(context);
if (selectCallback != null) {
selectCallback!(path!);
}
}
});
},
padding: EdgeInsets.all(0.0),
child: Container(
alignment: Alignment.center,
height: 60.0,
width: double.infinity,
// decoration: BoxDecoration(
// border: Border(
// bottom: BorderSide(
// width: 0.6, color: Colours.default_line))),
child: Text(
“相册”,
style: TextStyles.textGray800_w400_16,
maxLines: 1,
overflow: TextOverflow.ellipsis,
))),
// FlatButton(
// onPressed: () {
// if (viewCallback != null) {
// viewCallback!();
// }
// },
// shape: RoundedRectangleBorder(
// borderRadius:
// BorderRadius.vertical(bottom: Radius.circular(14.0))),
// padding: EdgeInsets.all(0.0),
// child: Container(
// alignment: Alignment.center,
// height: 60.0,
// width: double.infinity,
// child: Text(
// “看大图”,
// style: TextStyles.textGray800_w400_16,
// maxLines: 1,
// overflow: TextOverflow.ellipsis,
// ))),
],
),
),
Gaps.vGap10,
Container(
margin: EdgeInsets.only(
left: 12, right: 12, bottom: ScreenUtil.getBottomBarH(context)),
decoration: BoxDecoration(
color: Colours.white,
borderRadius: BorderRadius.all(Radius.circular(14.0)),
),
child: FlatButton(
onPressed: () => Navigator.pop(context),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(14.0))),
padding: EdgeInsets.all(0.0),
child: Container(
alignment: Alignment.center,
height: 56.0,
width: double.infinity,
child: Text(
“取消”,
style: TextStyles.textGray800_w400_16,
maxLines: 1,
overflow: TextOverflow.ellipsis,
))),
),
],
));
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值