Skip to content

Commit

Permalink
[image_picker] Fix use_build_context_synchronously violations (flutte…
Browse files Browse the repository at this point in the history
…r#3969)

- Fixes a violation of `use_build_context_synchronously` that was
previously undected, which will soon be detected, unblocking future
rolls.
- Reduces unnecessary drift between different versions of the example,
some of which unnecessarily had context be nullable, or passed it
slightly differently.
- Fixes newly-found `use_build_context_synchronously` violations from
making `BuildContext` non-nullable; apparently the lint rule doesn't
work for nullable contexts yet.
  • Loading branch information
stuartmorgan authored and nploi committed Jul 16, 2023
1 parent 39324c3 commit 88c7e03
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 143 deletions.
4 changes: 4 additions & 0 deletions packages/image_picker/image_picker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.7+5

* Fixes `BuildContext` handling in example.

## 0.8.7+4

* Updates README to mention usage of `launchMode: singleInstance` for Android.
Expand Down
90 changes: 46 additions & 44 deletions packages/image_picker/image_picker/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,51 +81,53 @@ class _MyHomePageState extends State<MyHomePage> {
}

Future<void> _onImageButtonPressed(ImageSource source,
{BuildContext? context, bool isMultiImage = false}) async {
{required BuildContext context, bool isMultiImage = false}) async {
if (_controller != null) {
await _controller!.setVolume(0.0);
}
if (isVideo) {
final XFile? file = await _picker.pickVideo(
source: source, maxDuration: const Duration(seconds: 10));
await _playVideo(file);
} else if (isMultiImage) {
await _displayPickImageDialog(context!,
(double? maxWidth, double? maxHeight, int? quality) async {
try {
final List<XFile> pickedFileList = await _picker.pickMultiImage(
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: quality,
);
setState(() {
_imageFileList = pickedFileList;
});
} catch (e) {
setState(() {
_pickImageError = e;
});
}
});
} else {
await _displayPickImageDialog(context!,
(double? maxWidth, double? maxHeight, int? quality) async {
try {
final XFile? pickedFile = await _picker.pickImage(
source: source,
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: quality,
);
setState(() {
_setImageFileListFromFile(pickedFile);
});
} catch (e) {
setState(() {
_pickImageError = e;
});
}
});
if (context.mounted) {
if (isVideo) {
final XFile? file = await _picker.pickVideo(
source: source, maxDuration: const Duration(seconds: 10));
await _playVideo(file);
} else if (isMultiImage) {
await _displayPickImageDialog(context,
(double? maxWidth, double? maxHeight, int? quality) async {
try {
final List<XFile> pickedFileList = await _picker.pickMultiImage(
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: quality,
);
setState(() {
_imageFileList = pickedFileList;
});
} catch (e) {
setState(() {
_pickImageError = e;
});
}
});
} else {
await _displayPickImageDialog(context,
(double? maxWidth, double? maxHeight, int? quality) async {
try {
final XFile? pickedFile = await _picker.pickImage(
source: source,
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: quality,
);
setState(() {
_setImageFileListFromFile(pickedFile);
});
} catch (e) {
setState(() {
_pickImageError = e;
});
}
});
}
}
}

Expand Down Expand Up @@ -332,7 +334,7 @@ class _MyHomePageState extends State<MyHomePage> {
backgroundColor: Colors.red,
onPressed: () {
isVideo = true;
_onImageButtonPressed(ImageSource.gallery);
_onImageButtonPressed(ImageSource.gallery, context: context);
},
heroTag: 'video0',
tooltip: 'Pick Video from gallery',
Expand All @@ -345,7 +347,7 @@ class _MyHomePageState extends State<MyHomePage> {
backgroundColor: Colors.red,
onPressed: () {
isVideo = true;
_onImageButtonPressed(ImageSource.camera);
_onImageButtonPressed(ImageSource.camera, context: context);
},
heroTag: 'video1',
tooltip: 'Take a Video',
Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/image_picker/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for selecting images from the Android and iOS image
library, and taking new pictures with the camera.
repository: https://github.com/flutter/packages/tree/main/packages/image_picker/image_picker
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
version: 0.8.7+4
version: 0.8.7+5

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
4 changes: 4 additions & 0 deletions packages/image_picker/image_picker_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.6+13

* Fixes `BuildContext` handling in example.

## 0.8.6+12

* Improves image resizing performance by decoding Bitmap only when needed.
Expand Down
98 changes: 50 additions & 48 deletions packages/image_picker/image_picker_android/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,55 +98,57 @@ class _MyHomePageState extends State<MyHomePage> {
}

Future<void> _onImageButtonPressed(
BuildContext context, {
required ImageSource source,
ImageSource source, {
required BuildContext context,
bool isMultiImage = false,
}) async {
if (_controller != null) {
await _controller!.setVolume(0.0);
}
if (isVideo) {
final XFile? file = await _picker.getVideo(
source: source, maxDuration: const Duration(seconds: 10));
if (file != null && context.mounted) {
_showPickedSnackBar(context, <XFile>[file]);
}
await _playVideo(file);
} else if (isMultiImage && context.mounted) {
await _displayPickImageDialog(context,
(double? maxWidth, double? maxHeight, int? quality) async {
try {
final List<XFile>? pickedFileList = await _picker.getMultiImage(
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: quality,
);
if (pickedFileList != null && context.mounted) {
_showPickedSnackBar(context, pickedFileList);
}
setState(() => _imageFileList = pickedFileList);
} catch (e) {
setState(() => _pickImageError = e);
if (context.mounted) {
if (isVideo) {
final XFile? file = await _picker.getVideo(
source: source, maxDuration: const Duration(seconds: 10));
if (file != null && context.mounted) {
_showPickedSnackBar(context, <XFile>[file]);
}
});
} else {
await _displayPickImageDialog(context,
(double? maxWidth, double? maxHeight, int? quality) async {
try {
final XFile? pickedFile = await _picker.getImage(
source: source,
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: quality,
);
if (pickedFile != null && context.mounted) {
_showPickedSnackBar(context, <XFile>[pickedFile]);
await _playVideo(file);
} else if (isMultiImage) {
await _displayPickImageDialog(context,
(double? maxWidth, double? maxHeight, int? quality) async {
try {
final List<XFile>? pickedFileList = await _picker.getMultiImage(
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: quality,
);
if (pickedFileList != null && context.mounted) {
_showPickedSnackBar(context, pickedFileList);
}
setState(() => _imageFileList = pickedFileList);
} catch (e) {
setState(() => _pickImageError = e);
}
setState(() => _setImageFileListFromFile(pickedFile));
} catch (e) {
setState(() => _pickImageError = e);
}
});
});
} else {
await _displayPickImageDialog(context,
(double? maxWidth, double? maxHeight, int? quality) async {
try {
final XFile? pickedFile = await _picker.getImage(
source: source,
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: quality,
);
if (pickedFile != null && context.mounted) {
_showPickedSnackBar(context, <XFile>[pickedFile]);
}
setState(() => _setImageFileListFromFile(pickedFile));
} catch (e) {
setState(() => _pickImageError = e);
}
});
}
}
}

Expand Down Expand Up @@ -315,7 +317,7 @@ class _MyHomePageState extends State<MyHomePage> {
key: const Key('image_picker_example_from_gallery'),
onPressed: () {
isVideo = false;
_onImageButtonPressed(context, source: ImageSource.gallery);
_onImageButtonPressed(ImageSource.gallery, context: context);
},
heroTag: 'image0',
tooltip: 'Pick Image from gallery',
Expand All @@ -328,8 +330,8 @@ class _MyHomePageState extends State<MyHomePage> {
onPressed: () {
isVideo = false;
_onImageButtonPressed(
context,
source: ImageSource.gallery,
ImageSource.gallery,
context: context,
isMultiImage: true,
);
},
Expand All @@ -343,7 +345,7 @@ class _MyHomePageState extends State<MyHomePage> {
child: FloatingActionButton(
onPressed: () {
isVideo = false;
_onImageButtonPressed(context, source: ImageSource.camera);
_onImageButtonPressed(ImageSource.camera, context: context);
},
heroTag: 'image2',
tooltip: 'Take a Photo',
Expand All @@ -356,7 +358,7 @@ class _MyHomePageState extends State<MyHomePage> {
backgroundColor: Colors.red,
onPressed: () {
isVideo = true;
_onImageButtonPressed(context, source: ImageSource.gallery);
_onImageButtonPressed(ImageSource.gallery, context: context);
},
heroTag: 'video0',
tooltip: 'Pick Video from gallery',
Expand All @@ -369,7 +371,7 @@ class _MyHomePageState extends State<MyHomePage> {
backgroundColor: Colors.red,
onPressed: () {
isVideo = true;
_onImageButtonPressed(context, source: ImageSource.camera);
_onImageButtonPressed(ImageSource.camera, context: context);
},
heroTag: 'video1',
tooltip: 'Take a Video',
Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/image_picker_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Android implementation of the image_picker plugin.
repository: https://github.com/flutter/packages/tree/main/packages/image_picker/image_picker_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22

version: 0.8.6+12
version: 0.8.6+13

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/image_picker/image_picker_ios/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## NEXT
## 0.8.7+4
* Fixes `BuildContext` handling in example.
* Updates metadata unit test to work on iOS 16.2.

## 0.8.7+3
Expand Down
Loading

0 comments on commit 88c7e03

Please sign in to comment.