feat: add support for handling errors

This commit is contained in:
mike doornenbal 2024-09-05 10:33:26 +02:00
parent 004f0067a4
commit a6f50c54d8
4 changed files with 27 additions and 7 deletions

View file

@ -45,3 +45,6 @@
* Added title options to theme. * Added title options to theme.
* updated iconica_analysis dependency. * updated iconica_analysis dependency.
* Updated default theme. * Updated default theme.
# 4.1.0
* Added support for handling errors when calling `pickImage`.

View file

@ -116,7 +116,13 @@ class ImagePickerExampleHomePageState
Uint8List? imageInBytes = await showModalBottomSheet<Uint8List?>( Uint8List? imageInBytes = await showModalBottomSheet<Uint8List?>(
context: context, context: context,
backgroundColor: Colors.white, backgroundColor: Colors.white,
builder: (BuildContext context) => const ImagePicker()); builder: (BuildContext context) => ImagePicker(
onError: (error) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(error.message ?? "An error occurred")),
);
},
));
if (imageInBytes != null) { if (imageInBytes != null) {
if (!listEquals(uploadedImage, imageInBytes)) { if (!listEquals(uploadedImage, imageInBytes)) {
setState(() { setState(() {

View file

@ -3,6 +3,7 @@
// SPDX-License-Identifier: BSD-3-Clause // SPDX-License-Identifier: BSD-3-Clause
import "package:flutter/material.dart"; import "package:flutter/material.dart";
import "package:flutter/services.dart";
import "package:flutter_image_picker/flutter_image_picker.dart"; import "package:flutter_image_picker/flutter_image_picker.dart";
import "package:image_picker/image_picker.dart"; import "package:image_picker/image_picker.dart";
@ -21,6 +22,7 @@ class ImagePicker extends StatelessWidget {
this.theme = const ImagePickerTheme(), this.theme = const ImagePickerTheme(),
this.config = const ImagePickerConfig(), this.config = const ImagePickerConfig(),
this.service, this.service,
this.onError,
super.key, super.key,
}); });
@ -36,6 +38,8 @@ class ImagePicker extends StatelessWidget {
/// add more features. If null the current implementation will be used. /// add more features. If null the current implementation will be used.
final ImagePickerService? service; final ImagePickerService? service;
final Function(PlatformException error)? onError;
@override @override
Widget build(BuildContext context) => SingleChildScrollView( Widget build(BuildContext context) => SingleChildScrollView(
child: Column( child: Column(
@ -58,6 +62,7 @@ class ImagePicker extends StatelessWidget {
Icons.image, Icons.image,
ImageSource.gallery, ImageSource.gallery,
theme.selectImageText, theme.selectImageText,
onError,
), ),
if (config.cameraOption ?? true) ...[ if (config.cameraOption ?? true) ...[
SizedBox( SizedBox(
@ -70,6 +75,7 @@ class ImagePicker extends StatelessWidget {
Icons.camera_alt_rounded, Icons.camera_alt_rounded,
ImageSource.camera, ImageSource.camera,
theme.makePhotoText, theme.makePhotoText,
onError,
), ),
], ],
], ],
@ -125,6 +131,7 @@ class ImagePicker extends StatelessWidget {
IconData icon, IconData icon,
ImageSource imageSource, ImageSource imageSource,
String bottomText, String bottomText,
Function(PlatformException error)? onError,
) => ) =>
Column( Column(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
@ -133,8 +140,14 @@ class ImagePicker extends StatelessWidget {
key: Key(bottomText), key: Key(bottomText),
onTap: () async { onTap: () async {
var navigator = Navigator.of(context); var navigator = Navigator.of(context);
var image = await (service ?? ImagePickerServiceDefault()) Uint8List? image;
.pickImage(imageSource, config: config); try {
image = await (service ?? ImagePickerServiceDefault())
.pickImage(imageSource, config: config);
} on PlatformException catch (e) {
debugPrint("image_picker_error: $e");
onError?.call(e);
}
navigator.pop(image); navigator.pop(image);
}, },
child: customIcon ?? child: customIcon ??

View file

@ -1,6 +1,6 @@
name: flutter_image_picker name: flutter_image_picker
description: A Flutter Image Picking package. description: A Flutter Image Picking package.
version: 4.0.0 version: 4.1.0
repository: https://github.com/Iconica-Development/flutter_image_picker repository: https://github.com/Iconica-Development/flutter_image_picker
publish_to: https://forgejo.internal.iconica.nl/api/packages/internal/pub publish_to: https://forgejo.internal.iconica.nl/api/packages/internal/pub
@ -12,7 +12,7 @@ environment:
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
image_picker: ^1.0.7 image_picker: ^1.1.2
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
@ -22,5 +22,3 @@ dev_dependencies:
git: git:
url: https://github.com/Iconica-Development/flutter_iconica_analysis url: https://github.com/Iconica-Development/flutter_iconica_analysis
ref: 7.0.0 ref: 7.0.0
flutter: