I have 2 Widget functions which while initiating onPressed() is supposed to show a imagepicker() using a CupertinoActionsheet with Actions to choose an image from Gallery/Camera.
The CupertinoActionSheet and the actions are part of _imagePickerService.pickImage().
Widget _buildUserAvatar() {
var _onPressed = () {
_showImageBottomSheet(imageType: ImageType.avatar);
return GestureDetector(
onTap: _onPressed,
child: const Icon(
Icons.edit,
size: 20,));
};}
Widget _buildUserProfileCover() {
var _onPressed = () {
_showImageBottomSheet(imageType: ImageType.cover);
return GestureDetector(
onTap: _onPressed,
child: const Icon(
Icons.edit,
size: 20,));
};}
Future<void> _showImageBottomSheet({required ImageType imageType}) async {
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
List<Widget> listActions = [
Expanded(
child: GestureDetector(
onTap: () async {
try {
var image = await _imagePickerService.pickImage(
imageType: imageType, context: context);
_onUserImageSelected(image: image, imageType: imageType);
} on FileTooLargeException catch (e) {
int limit = e.getLimitInMB();
if (context.mounted) {
toastService.error(
message: _localizationService
.user__edit_profile_pick_image_error_too_large(
limit),
context: context);
}
}
if (context.mounted) {
Navigator.pop(context);
}
},
),
)
];
switch (imageType) {
case ImageType.cover:
if (_coverUrl != null || _coverFile != null) {
listActions.add(CupertinoActionSheet());
}
case ImageType.avatar:
if (_avatarUrl != null || _avatarFile != null) {
listActions.add(CupertinoActionSheet());
}
default:
throw 'Unhandled imageType';
}
return Row(mainAxisSize: MainAxisSize.min, children: listActions);
});
}
Unfortunately, I had to used Gesture detector to trigger a onTap to show the CupertinoActionSheet with its options which work fine. The previous onPressed triggers showModalBottomSheet() which again needs to be Tapped using GestureDetector to open up the options.
Note : The switch() is used to clear the Avatar or Cover File based on whether it exists or not. It is yet to be coded and not important now.