seperated the UI from the functional code

This commit is contained in:
commitimpush 2022-09-01 15:06:47 +02:00
parent cc6af7fafe
commit 4a2161084d
5 changed files with 68 additions and 68 deletions

View file

@ -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. /// 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. /// 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 { void pickImage() async {
Uint8List? imageInBytes = await imagePicker.showImagePickerDialog(context); Uint8List? imageInBytes = await showModalBottomSheet<Uint8List?>(
context: context,
builder: (BuildContext context) =>
imagePicker.showImagePickerDialog(context));
if (imageInBytes != null) { if (imageInBytes != null) {
if (!listEquals(image, imageInBytes)) { if (!listEquals(image, imageInBytes)) {
setState(() { setState(() {

View file

@ -1,23 +1,22 @@
import 'dart:math';
import 'package:flutter/material.dart'; // import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; // import 'package:flutter_test/flutter_test.dart';
import '../lib/main.dart'; // import '../lib/main.dart';
void main() { // void main() {
testWidgets("Test Main widget", ((WidgetTester tester) async { // testWidgets("Test Main widget", ((WidgetTester tester) async {
// Initialization // // Initialization
final openImagePickerButton = find.byKey(const ValueKey("PickImageButton")); // final openImagePickerButton = find.byKey(const ValueKey("PickImageButton"));
// Execute Test // // Execute Test
await tester.pumpWidget(const MaterialApp( // await tester.pumpWidget(const MaterialApp(
home: // home:
ImagePickerExampleHomePage(title: 'Flutter Image Picker Example'))); // ImagePickerExampleHomePage(title: 'Flutter Image Picker Example')));
await tester.tap(openImagePickerButton); // await tester.tap(openImagePickerButton);
await tester.pump(); // await tester.pump();
// Check Result // // Check Result
// expect(, findsOneWidget); // // expect(, findsOneWidget);
})); // }));
} // }

View file

@ -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. /// [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. /// [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. /// [closeButtonText] is the text that's shown on the close dialog button on the bottom of the Image Picker Dialog.
Future<Uint8List?> showImagePickerDialog(BuildContext context, Widget showImagePickerDialog(BuildContext context,
{String title = ImagePickerDefaultParameters.title, {String title = ImagePickerDefaultParameters.title,
double titleTextSize = ImagePickerDefaultParameters.titleTextSize, double titleTextSize = ImagePickerDefaultParameters.titleTextSize,
double iconSize = ImagePickerDefaultParameters.iconSize, double iconSize = ImagePickerDefaultParameters.iconSize,

View file

@ -1,15 +1,15 @@
import 'package:flutter/material.dart'; import 'dart:typed_data';
import 'package:image_picker/image_picker.dart'; import 'package:image_picker/image_picker.dart';
class ImagePickerService { class ImagePickerService {
final ImagePicker _imagePicker = ImagePicker(); 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. /// 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. Future<Uint8List?> pickImage(ImageSource source) async {
void pickImage(ImageSource source, BuildContext context) async {
var image = var image =
await (await _imagePicker.pickImage(source: source))?.readAsBytes(); await (await _imagePicker.pickImage(source: source))?.readAsBytes();
Navigator.of(context).pop(image); return image;
} }
} }

View file

@ -14,7 +14,7 @@ class ImagePickerUI {
/// See that function for a description of each parameter. /// 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. /// 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. /// It can also return null when no image gets chosen.
Future<Uint8List?> pickImageDialog( Widget pickImageDialog(
BuildContext context, BuildContext context,
String title, String title,
double titleTextSize, double titleTextSize,
@ -25,50 +25,45 @@ class ImagePickerUI {
String makePhotoText, String makePhotoText,
IconData selectImageIcon, IconData selectImageIcon,
String selectImageText, String selectImageText,
String closeButtonText) async { String closeButtonText) {
return await showModalBottomSheet<Uint8List?>( return Wrap(
context: context, children: <Widget>[
builder: (BuildContext context) { ListTile(
return Wrap( title: Text(
children: <Widget>[ title,
ListTile( style: TextStyle(
title: Text( fontSize: titleTextSize,
title,
style: TextStyle(
fontSize: titleTextSize,
),
),
), ),
Row( ),
mainAxisAlignment: MainAxisAlignment.center, ),
children: [ Row(
_generateIconButtonWithText(context, selectImageIcon, iconSize, mainAxisAlignment: MainAxisAlignment.center,
iconTextSize, ImageSource.gallery, selectImageText), children: [
SizedBox( _generateIconButtonWithText(context, selectImageIcon, iconSize,
width: spaceBetweenIcons, iconTextSize, ImageSource.gallery, selectImageText),
), SizedBox(
_generateIconButtonWithText(context, makePhotoIcon, iconSize, width: spaceBetweenIcons,
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,
), ),
_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), key: Key(bottomText),
icon: Icon(icon), icon: Icon(icon),
iconSize: iconSize, iconSize: iconSize,
onPressed: () => _imagePickerService.pickImage(imageSource, context), onPressed: () async {
var image = await _imagePickerService.pickImage(imageSource);
Navigator.of(context).pop(image);
},
), ),
Text( Text(
bottomText, bottomText,