<?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,
))),
),
],
));
}
}