2022-08-31 10:09:36 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-08-31 10:19:57 +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-01 16:44:53 +02:00
|
|
|
import '../models/image_picker_theme.dart';
|
|
|
|
|
|
|
|
class ImagePicker extends StatelessWidget {
|
|
|
|
ImagePicker({Key? key, this.imagePickerTheme = const ImagePickerTheme()})
|
|
|
|
: super(key: key);
|
|
|
|
|
|
|
|
final ImagePickerTheme imagePickerTheme;
|
|
|
|
|
2022-08-31 10:19:57 +02:00
|
|
|
final ImagePickerService _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-01 15:06:47 +02:00
|
|
|
return Wrap(
|
|
|
|
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-01 16:44:53 +02:00
|
|
|
fontSize: imagePickerTheme.titleTextSize,
|
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(
|
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
2022-09-01 16:44:53 +02:00
|
|
|
child: Text(imagePickerTheme.closeButtonText)),
|
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-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-01 15:06:47 +02:00
|
|
|
onPressed: () async {
|
|
|
|
var image = await _imagePickerService.pickImage(imageSource);
|
|
|
|
Navigator.of(context).pop(image);
|
|
|
|
},
|
2022-08-31 12:31:54 +02:00
|
|
|
),
|
2022-08-31 14:42:17 +02:00
|
|
|
Text(
|
|
|
|
bottomText,
|
2022-09-01 16:44:53 +02:00
|
|
|
style: TextStyle(fontSize: imagePickerTheme.iconTextSize),
|
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
|
|
|
}
|