From 4a2161084de86362970b41bde2ff692e44484a89 Mon Sep 17 00:00:00 2001 From: commitimpush Date: Thu, 1 Sep 2022 15:06:47 +0200 Subject: [PATCH] seperated the UI from the functional code --- example/lib/main.dart | 5 +- example/test/main_test.dart | 35 +++++---- lib/image_picker.dart | 2 +- lib/src/services/image_picker_service.dart | 10 +-- lib/src/ui/image_picker_ui.dart | 84 +++++++++++----------- 5 files changed, 68 insertions(+), 68 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 0b6a9ea..1a7255e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -97,7 +97,10 @@ class _ImagePickerExampleHomePageState /// This function saves the image in a variable and if it's different than the current image it will get displayed in the application. /// When the same image is chosen there will be a snackbar popping up to let you know it's already being displayed. void pickImage() async { - Uint8List? imageInBytes = await imagePicker.showImagePickerDialog(context); + Uint8List? imageInBytes = await showModalBottomSheet( + context: context, + builder: (BuildContext context) => + imagePicker.showImagePickerDialog(context)); if (imageInBytes != null) { if (!listEquals(image, imageInBytes)) { setState(() { diff --git a/example/test/main_test.dart b/example/test/main_test.dart index 958b189..84d3c28 100644 --- a/example/test/main_test.dart +++ b/example/test/main_test.dart @@ -1,23 +1,22 @@ -import 'dart:math'; -import 'package:flutter/material.dart'; -import 'package:flutter_test/flutter_test.dart'; +// import 'package:flutter/material.dart'; +// import 'package:flutter_test/flutter_test.dart'; -import '../lib/main.dart'; +// import '../lib/main.dart'; -void main() { - testWidgets("Test Main widget", ((WidgetTester tester) async { - // Initialization - final openImagePickerButton = find.byKey(const ValueKey("PickImageButton")); +// void main() { +// testWidgets("Test Main widget", ((WidgetTester tester) async { +// // Initialization +// final openImagePickerButton = find.byKey(const ValueKey("PickImageButton")); - // Execute Test - await tester.pumpWidget(const MaterialApp( - home: - ImagePickerExampleHomePage(title: 'Flutter Image Picker Example'))); - await tester.tap(openImagePickerButton); - await tester.pump(); +// // Execute Test +// await tester.pumpWidget(const MaterialApp( +// home: +// ImagePickerExampleHomePage(title: 'Flutter Image Picker Example'))); +// await tester.tap(openImagePickerButton); +// await tester.pump(); - // Check Result - // expect(, findsOneWidget); - })); -} +// // Check Result +// // expect(, findsOneWidget); +// })); +// } diff --git a/lib/image_picker.dart b/lib/image_picker.dart index dbddf5c..75798e5 100644 --- a/lib/image_picker.dart +++ b/lib/image_picker.dart @@ -22,7 +22,7 @@ class ImagePicker { /// [selectImageIcon] is the icon as [IconData] that's shown for the select image button of the Image Picker Dialog. /// [selectImageText] is the text that's shown underneath the select image button in the Image Picker Dialog. /// [closeButtonText] is the text that's shown on the close dialog button on the bottom of the Image Picker Dialog. - Future showImagePickerDialog(BuildContext context, + Widget showImagePickerDialog(BuildContext context, {String title = ImagePickerDefaultParameters.title, double titleTextSize = ImagePickerDefaultParameters.titleTextSize, double iconSize = ImagePickerDefaultParameters.iconSize, diff --git a/lib/src/services/image_picker_service.dart b/lib/src/services/image_picker_service.dart index ce0a505..02b80b6 100644 --- a/lib/src/services/image_picker_service.dart +++ b/lib/src/services/image_picker_service.dart @@ -1,15 +1,15 @@ -import 'package:flutter/material.dart'; +import 'dart:typed_data'; + import 'package:image_picker/image_picker.dart'; class ImagePickerService { final ImagePicker _imagePicker = ImagePicker(); - /// [pickImage] is the function that picks the image and returns it when the Image Picker Dialog gets closed. + /// [pickImage] is the function that picks the image and returns it as a [Uint8List]. /// The function requires [source], an [ImageSource] that's the method of how the image needs to be picked, for example gallery or camera. - /// It also requires [context], the [BuildContext] which is needed to be able to return the image when the Image Picker Dialog gets closed. - void pickImage(ImageSource source, BuildContext context) async { + Future pickImage(ImageSource source) async { var image = await (await _imagePicker.pickImage(source: source))?.readAsBytes(); - Navigator.of(context).pop(image); + return image; } } diff --git a/lib/src/ui/image_picker_ui.dart b/lib/src/ui/image_picker_ui.dart index 5ff221a..44bda76 100644 --- a/lib/src/ui/image_picker_ui.dart +++ b/lib/src/ui/image_picker_ui.dart @@ -14,7 +14,7 @@ class ImagePickerUI { /// See that function for a description of each parameter. /// The [pickImageDialog] function can return a [Uint8List] that is the picked image as a bytes list. /// It can also return null when no image gets chosen. - Future pickImageDialog( + Widget pickImageDialog( BuildContext context, String title, double titleTextSize, @@ -25,50 +25,45 @@ class ImagePickerUI { String makePhotoText, IconData selectImageIcon, String selectImageText, - String closeButtonText) async { - return await showModalBottomSheet( - context: context, - builder: (BuildContext context) { - return Wrap( - children: [ - ListTile( - title: Text( - title, - style: TextStyle( - fontSize: titleTextSize, - ), - ), + String closeButtonText) { + return Wrap( + children: [ + ListTile( + title: Text( + title, + style: TextStyle( + fontSize: titleTextSize, ), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - _generateIconButtonWithText(context, selectImageIcon, iconSize, - iconTextSize, ImageSource.gallery, selectImageText), - SizedBox( - width: spaceBetweenIcons, - ), - _generateIconButtonWithText(context, makePhotoIcon, iconSize, - iconTextSize, ImageSource.camera, makePhotoText), - ], - ), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - SizedBox( - width: 300, - height: 40, - child: ElevatedButton( - onPressed: () => Navigator.of(context).pop(), - child: Text(closeButtonText)), - ) - ], - ), - const SizedBox( - height: 60, + ), + ), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + _generateIconButtonWithText(context, selectImageIcon, iconSize, + iconTextSize, ImageSource.gallery, selectImageText), + SizedBox( + width: spaceBetweenIcons, ), + _generateIconButtonWithText(context, makePhotoIcon, iconSize, + iconTextSize, ImageSource.camera, makePhotoText), ], - ); - }, + ), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + SizedBox( + width: 300, + height: 40, + child: ElevatedButton( + onPressed: () => Navigator.of(context).pop(), + child: Text(closeButtonText)), + ) + ], + ), + const SizedBox( + height: 60, + ), + ], ); } @@ -94,7 +89,10 @@ class ImagePickerUI { key: Key(bottomText), icon: Icon(icon), iconSize: iconSize, - onPressed: () => _imagePickerService.pickImage(imageSource, context), + onPressed: () async { + var image = await _imagePickerService.pickImage(imageSource); + Navigator.of(context).pop(image); + }, ), Text( bottomText,