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

View file

@ -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);
// }));
// }

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.
/// [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<Uint8List?> showImagePickerDialog(BuildContext context,
Widget showImagePickerDialog(BuildContext context,
{String title = ImagePickerDefaultParameters.title,
double titleTextSize = ImagePickerDefaultParameters.titleTextSize,
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';
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<Uint8List?> pickImage(ImageSource source) async {
var image =
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.
/// 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<Uint8List?> pickImageDialog(
Widget pickImageDialog(
BuildContext context,
String title,
double titleTextSize,
@ -25,10 +25,7 @@ class ImagePickerUI {
String makePhotoText,
IconData selectImageIcon,
String selectImageText,
String closeButtonText) async {
return await showModalBottomSheet<Uint8List?>(
context: context,
builder: (BuildContext context) {
String closeButtonText) {
return Wrap(
children: <Widget>[
ListTile(
@ -68,8 +65,6 @@ class ImagePickerUI {
),
],
);
},
);
}
/// The [_generateIconButtonWithText] function returns a column that includes an [IconButton] and [Text].
@ -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,