2022-08-31 10:09:36 +02:00
import ' package:flutter/material.dart ' ;
2022-09-05 11:57:48 +02:00
import ' package:flutter_image_picker/src/services/image_picker_service.dart ' ;
2022-08-31 10:09:36 +02:00
import ' package:image_picker/image_picker.dart ' ;
2022-09-05 11:57:48 +02:00
import ' ../models/image_picker_theme.dart ' ;
2022-09-01 16:44:53 +02:00
2022-09-05 09:54:42 +02:00
/// The Image Picker class generates the Image Picker Widget which can be displayed in your application. If you call the class you can give it 2 optional variables:
/// The first one is the [ImagePickerTheme] which can be used to change the UI of the widget.
/// The second one is your own implementation of the ImagePickerService. Which can be used in testing for example.
2022-09-01 16:44:53 +02:00
class ImagePicker extends StatelessWidget {
2022-09-02 16:14:24 +02:00
const ImagePicker (
{ Key ? key ,
this . imagePickerTheme = const ImagePickerTheme ( ) ,
this . imagePickerService } )
2022-09-01 16:44:53 +02:00
: super ( key: key ) ;
2022-09-05 09:54:42 +02:00
/// ImagePickerTheme can be used to change the UI of the Image Picker Widget to change the text/icons to your liking.
2022-09-01 16:44:53 +02:00
final ImagePickerTheme imagePickerTheme ;
2022-09-05 09:54:42 +02:00
/// The ImagePickerService can be used if you want to use your own implementation of the Image Service if you want to use it for testing or add more features. If null the current implementation will be used.
2022-09-02 16:14:24 +02:00
final ImagePickerService ? imagePickerService ;
2022-08-31 10:09:36 +02:00
2022-09-01 16:44:53 +02:00
@ override
Widget build ( BuildContext context ) {
2022-09-05 11:41:32 +02:00
return SingleChildScrollView (
child: Wrap (
2022-09-01 15:06:47 +02:00
children: < Widget > [
ListTile (
title: Text (
2022-09-01 16:44:53 +02:00
imagePickerTheme . title ,
2022-09-01 15:06:47 +02:00
style: TextStyle (
2022-09-06 14:22:28 +02:00
fontFamily: imagePickerTheme . font ,
2022-09-01 16:44:53 +02:00
fontSize: imagePickerTheme . titleTextSize ,
2022-09-06 14:22:28 +02:00
color: imagePickerTheme . textColor ,
2022-08-31 14:42:17 +02:00
) ,
2022-09-01 15:06:47 +02:00
) ,
) ,
Row (
mainAxisAlignment: MainAxisAlignment . center ,
children: [
2022-09-01 16:44:53 +02:00
_generateIconButtonWithText (
context ,
imagePickerTheme ,
imagePickerTheme . selectImageIcon ,
ImageSource . gallery ,
imagePickerTheme . selectImageText ) ,
2022-09-01 15:06:47 +02:00
SizedBox (
2022-09-01 16:44:53 +02:00
width: imagePickerTheme . spaceBetweenIcons ,
2022-08-31 14:42:17 +02:00
) ,
2022-09-01 16:44:53 +02:00
_generateIconButtonWithText (
context ,
imagePickerTheme ,
imagePickerTheme . makePhotoIcon ,
ImageSource . camera ,
imagePickerTheme . makePhotoText ) ,
2022-09-01 15:06:47 +02:00
] ,
) ,
Row (
mainAxisAlignment: MainAxisAlignment . center ,
children: [
SizedBox (
2022-09-01 17:00:59 +02:00
width: imagePickerTheme . closeButtonWidth ,
height: imagePickerTheme . closeButtonHeight ,
2022-09-01 15:06:47 +02:00
child: ElevatedButton (
2022-09-06 14:22:28 +02:00
style: ElevatedButton . styleFrom (
backgroundColor:
imagePickerTheme . closeButtonBackgroundColor ) ,
2022-09-01 15:06:47 +02:00
onPressed: ( ) = > Navigator . of ( context ) . pop ( ) ,
2022-09-06 14:22:28 +02:00
child: Text ( imagePickerTheme . closeButtonText ,
style: TextStyle (
fontFamily: imagePickerTheme . font ,
fontSize: imagePickerTheme . closeButtonTextSize ,
color: imagePickerTheme . closeButtonTextColor ,
) ) ) ,
2022-09-01 15:06:47 +02:00
)
2022-08-31 14:42:17 +02:00
] ,
2022-09-01 15:06:47 +02:00
) ,
const SizedBox (
height: 60 ,
) ,
] ,
2022-09-05 11:41:32 +02:00
) ) ;
2022-08-31 10:09:36 +02:00
}
2022-08-31 12:31:54 +02:00
2022-09-01 10:22:32 +02:00
/// The [_generateIconButtonWithText] function returns a column that includes an [IconButton] and [Text].
/// The function requires the following parameters to be able to generate an icon with text:
/// [context] The build context that is required to make the [pickImage] function in [_imagePickerService] work.
2022-09-01 16:44:53 +02:00
/// [imagePickerTheme] The ImagePickerTheme that includes all default values for the Image Picker Dialog.
2022-09-01 10:22:32 +02:00
/// [icon] The icon that needs to be displayed, requires an [IconData] as value to be used.
/// [imageSource] The type of [ImageSource] to be used to pick an image when pressed on the icon.
/// [bottomText] The text that's displayed underneath the icon.
Column _generateIconButtonWithText (
2022-08-31 16:54:45 +02:00
BuildContext context ,
2022-09-01 16:44:53 +02:00
ImagePickerTheme imagePickerTheme ,
2022-08-31 16:54:45 +02:00
IconData icon ,
ImageSource imageSource ,
String bottomText ) {
2022-08-31 12:31:54 +02:00
return Column (
mainAxisSize: MainAxisSize . min ,
children: < Widget > [
IconButton (
2022-09-01 14:35:38 +02:00
key: Key ( bottomText ) ,
2022-08-31 12:31:54 +02:00
icon: Icon ( icon ) ,
2022-09-01 16:44:53 +02:00
iconSize: imagePickerTheme . iconSize ,
2022-09-06 14:22:28 +02:00
color: imagePickerTheme . iconColor ,
2022-09-01 15:06:47 +02:00
onPressed: ( ) async {
2022-09-05 09:21:35 +02:00
final navigator = Navigator . of ( context ) ;
2022-09-02 16:14:24 +02:00
var image = await ( imagePickerService ? ? ImagePickerService ( ) )
. pickImage ( imageSource ) ;
2022-09-05 09:21:35 +02:00
navigator . pop ( image ) ;
2022-09-01 15:06:47 +02:00
} ,
2022-08-31 12:31:54 +02:00
) ,
2022-08-31 14:42:17 +02:00
Text (
bottomText ,
2022-09-06 14:22:28 +02:00
style: TextStyle (
fontFamily: imagePickerTheme . font ,
fontSize: imagePickerTheme . iconTextSize ,
color: imagePickerTheme . textColor ) ,
2022-08-31 14:42:17 +02:00
) ,
2022-08-31 12:31:54 +02:00
const SizedBox (
height: 20 ,
) ,
] ,
) ;
}
2022-08-31 10:09:36 +02:00
}