From 560d8c3e2e0f372056263ae7ab50e87c828fd506 Mon Sep 17 00:00:00 2001 From: commitimpush Date: Wed, 31 Aug 2022 12:31:54 +0200 Subject: [PATCH] code improvements --- example/lib/main.dart | 20 ++++++----- lib/src/screens/image_picker_ui.dart | 54 ++++++++++++---------------- 2 files changed, 33 insertions(+), 41 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index e192517..ae1034d 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -73,15 +73,17 @@ class _ImagePickerExampleHomePageState void pickImage() async { Uint8List? imageInBytes = await imagePicker.showPickImageDialog(context); - if (imageInBytes != null && !listEquals(image, imageInBytes)) { - setState(() { - image = imageInBytes; - }); - } else { - ScaffoldMessenger.of(context).showSnackBar( - const SnackBar( - content: Text('Selected image is already being displayed!')), - ); + if (imageInBytes != null) { + if (!listEquals(image, imageInBytes)) { + setState(() { + image = imageInBytes; + }); + } else { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar( + content: Text('Selected image is already being displayed!')), + ); + } } } } diff --git a/lib/src/screens/image_picker_ui.dart b/lib/src/screens/image_picker_ui.dart index 3292041..cd5ba34 100644 --- a/lib/src/screens/image_picker_ui.dart +++ b/lib/src/screens/image_picker_ui.dart @@ -23,38 +23,10 @@ class ImagePickerUI { Row( mainAxisAlignment: MainAxisAlignment.center, children: [ - Column( - mainAxisSize: MainAxisSize.min, - children: [ - IconButton( - icon: const Icon(Icons.image), - tooltip: 'Choose Image From Gallery', - iconSize: iconSize, - onPressed: () => _imagePickerService.pickImage( - ImageSource.gallery, context), - ), - const Text("Select file"), - const SizedBox( - height: 20, - ), - ], - ), - Column( - mainAxisSize: MainAxisSize.min, - children: [ - IconButton( - icon: const Icon(Icons.camera_alt_rounded), - tooltip: 'Make Image With Camera', - iconSize: iconSize, - onPressed: () => _imagePickerService.pickImage( - ImageSource.camera, context), - ), - const Text("Take a picture"), - const SizedBox( - height: 20, - ), - ], - ), + generateColumn( + context, Icons.image, ImageSource.gallery, "Select File"), + generateColumn(context, Icons.camera_alt_rounded, + ImageSource.camera, "Take a picture"), ], ), ElevatedButton( @@ -64,4 +36,22 @@ class ImagePickerUI { }, ); } + + Column generateColumn(BuildContext context, IconData icon, + ImageSource imageSource, String bottomText) { + return Column( + mainAxisSize: MainAxisSize.min, + children: [ + IconButton( + icon: Icon(icon), + iconSize: iconSize, + onPressed: () => _imagePickerService.pickImage(imageSource, context), + ), + Text(bottomText), + const SizedBox( + height: 20, + ), + ], + ); + } }