getImageWithCropping method
Opens a bottom sheet to pick an image from the camera or gallery. Picks an image from the given source and crops it if cropping is enabled.
imageSource specifies whether the image is from the camera or gallery.
Implementation
/* void openImagePicker() {
showModalBottomSheet(
context: context,
useRootNavigator: false,
backgroundColor: Colors.transparent,
builder: (BuildContext bc) => GestureDetector(
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
padding: const EdgeInsets.symmetric(vertical: 15),
decoration: const BoxDecoration(
color: Colors.black87,
shape: BoxShape.rectangle,
borderRadius: BorderRadius.all(Radius.circular(30)),
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GestureDetector(
child: Column(
children: [
Container(
height: 50,
width: 50,
decoration: const BoxDecoration(
color: Colors.pinkAccent,
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: Color(0x194A841C),
offset: Offset(0.0, 1.0),
blurRadius: 19,
),
],
),
child: const Icon(
Icons.camera_alt_rounded,
size: 25,
color: Colors.white,
),
),
const SizedBox(height: 10),
const Text(
"Camera",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w500,
fontSize: 12,
),
),
],
),
onTap: () async {
if (await isCameraEnabled()) {
Navigator.pop(context);
cropAspectRatioPreset != null
? getImageWithCropping(ImageSource.camera)
: getImageWithoutCropping(ImageSource.camera);
} else {
callback.error('Permission not granted');
}
},
),
const SizedBox(width: 60),
GestureDetector(
child: Column(
children: [
Container(
height: 50,
width: 50,
decoration: const BoxDecoration(
color: Color(0xff6BBBAE),
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: Color(0x194A841C),
offset: Offset(0.0, 1.0),
blurRadius: 19,
),
],
),
child: const Icon(
Icons.image_rounded,
size: 25,
color: Colors.white,
),
),
const SizedBox(height: 10),
const Text(
"Gallery",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w500,
fontSize: 12,
),
),
],
),
onTap: () async {
if (await isStorageEnabled()) {
Navigator.pop(context);
cropAspectRatioPreset != null
? getImageWithCropping(ImageSource.gallery)
: getImageWithoutCropping(ImageSource.gallery);
} else {
callback.error('Permission not granted');
}
},
),
],
),
const SizedBox(height: 15),
InkWell(
child: const Padding(
padding: EdgeInsets.all(13.0),
child: Text(
"Cancel",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.normal,
fontSize: 14,
decoration: TextDecoration.underline,
),
),
),
onTap: () {
Navigator.pop(context);
},
),
],
),
),
onTap: () {
FocusScope.of(context).requestFocus(FocusScopeNode());
},
),
);
}*/
/// Picks an image from the given source and crops it if cropping is enabled.
///
/// [imageSource] specifies whether the image is from the camera or gallery.
Future<void> getImageWithCropping(
ImageSource imageSource, String type) async {
XFile? imageFile = await picker.pickImage(source: imageSource);
if (imageFile != null) {
CroppedFile? croppedFile = await ImageCropper().cropImage(
sourcePath: imageFile.path,
// aspectRatio: CropAspectRatio.
// aspectRatio: CropAspectRatio.
// cropAspectRatioPreset ?? CropAspectRatioPreset.square
// ,
uiSettings: [
AndroidUiSettings(
toolbarTitle: 'Cropper',
toolbarColor: Colors.cyan,
toolbarWidgetColor: Colors.white,
initAspectRatio: CropAspectRatioPreset.original,
lockAspectRatio: false,
),
IOSUiSettings(minimumAspectRatio: 1.0),
],
);
if (croppedFile != null) {
getImageCompressed(XFile(croppedFile.path), type);
} else {
getImageCompressed(imageFile, type);
}
} else {
callback.error('No Image Selected');
}
}