flutter_image_picker/lib/src/ui/image_picker_ui.dart

102 lines
3.3 KiB
Dart
Raw Normal View History

2022-08-31 10:09:36 +02:00
import 'package:flutter/material.dart';
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;
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) {
return Wrap(
children: <Widget>[
ListTile(
title: Text(
2022-09-01 16:44:53 +02:00
imagePickerTheme.title,
style: TextStyle(
2022-09-01 16:44:53 +02:00
fontSize: imagePickerTheme.titleTextSize,
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
2022-09-01 16:44:53 +02:00
_generateIconButtonWithText(
context,
imagePickerTheme,
imagePickerTheme.selectImageIcon,
ImageSource.gallery,
imagePickerTheme.selectImageText),
SizedBox(
2022-09-01 16:44:53 +02:00
width: imagePickerTheme.spaceBetweenIcons,
),
2022-09-01 16:44:53 +02:00
_generateIconButtonWithText(
context,
imagePickerTheme,
imagePickerTheme.makePhotoIcon,
ImageSource.camera,
imagePickerTheme.makePhotoText),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
2022-09-01 17:00:59 +02:00
width: imagePickerTheme.closeButtonWidth,
height: imagePickerTheme.closeButtonHeight,
child: ElevatedButton(
onPressed: () => Navigator.of(context).pop(),
2022-09-01 16:44:53 +02:00
child: Text(imagePickerTheme.closeButtonText)),
)
],
),
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(
BuildContext context,
2022-09-01 16:44:53 +02:00
ImagePickerTheme imagePickerTheme,
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,
onPressed: () async {
var image = await _imagePickerService.pickImage(imageSource);
Navigator.of(context).pop(image);
},
2022-08-31 12:31:54 +02:00
),
Text(
bottomText,
2022-09-01 16:44:53 +02:00
style: TextStyle(fontSize: imagePickerTheme.iconTextSize),
),
2022-08-31 12:31:54 +02:00
const SizedBox(
height: 20,
),
],
);
}
2022-08-31 10:09:36 +02:00
}