Skip to content

Commit 2d5f61b

Browse files
author
Sem van der Wal
authored
Prevent events from happening while picking image (#460)
I ran into an issue on iOS with the bottom sheet controls (the select source buttons and dismissal) which were receiving tap events while the camera app was opened. This resulted in two issues: - Error 'Cancelled by a second request' when the user taps 'Use Photo' (because a select source button was also receiving the event) - Weird behaviour if the user taps on the created photo before you tap 'Use Photo' (it was dismissing the bottom sheet, resulting in a second Navigation pop on Image confirm) The proposed changes may not be the best way to prevent Events from reaching the bottom sheet while the camera is opened, feedback on how to improve this would be very welcome.
1 parent 286a2ff commit 2d5f61b

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

lib/src/widgets/image_source_sheet.dart

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class ImageSourceBottomSheet extends StatelessWidget {
3939
final Widget cameraLabel;
4040
final Widget galleryLabel;
4141
final EdgeInsets bottomSheetPadding;
42+
43+
bool _isPickingImage = false;
4244

4345
ImageSourceBottomSheet({
4446
Key key,
@@ -57,6 +59,8 @@ class ImageSourceBottomSheet extends StatelessWidget {
5759
super(key: key);
5860

5961
Future<void> _onPickImage(ImageSource source) async {
62+
if(_isPickingImage) return;
63+
_isPickingImage = true;
6064
final imagePicker = ImagePicker();
6165
final pickedFile = await imagePicker.getImage(
6266
source: source,
@@ -65,6 +69,7 @@ class ImageSourceBottomSheet extends StatelessWidget {
6569
imageQuality: imageQuality,
6670
preferredCameraDevice: preferredCameraDevice,
6771
);
72+
_isPickingImage = false;
6873
if (null != pickedFile) {
6974
if (kIsWeb) {
7075
if (null != onImage) {
@@ -84,22 +89,24 @@ class ImageSourceBottomSheet extends StatelessWidget {
8489

8590
@override
8691
Widget build(BuildContext context) {
87-
return Container(
88-
padding: bottomSheetPadding,
89-
child: Wrap(
90-
children: <Widget>[
91-
ListTile(
92-
leading: cameraIcon,
93-
title: cameraLabel,
94-
onTap: () => _onPickImage(ImageSource.camera),
95-
),
96-
ListTile(
97-
leading: galleryIcon,
98-
title: galleryLabel,
99-
onTap: () => _onPickImage(ImageSource.gallery),
100-
)
101-
],
102-
),
92+
return WillPopScope(
93+
onWillPop: () async => !_isPickingImage,
94+
child: Container(
95+
padding: bottomSheetPadding,
96+
child: Wrap(
97+
children: <Widget>[
98+
ListTile(
99+
leading: cameraIcon,
100+
title: cameraLabel,
101+
onTap: () => _onPickImage(ImageSource.camera),
102+
),
103+
ListTile(
104+
leading: galleryIcon,
105+
title: galleryLabel,
106+
onTap: () => _onPickImage(ImageSource.gallery),
107+
)
108+
],
109+
),
103110
);
104111
}
105112
}

0 commit comments

Comments
 (0)