flutter_image_picker/lib/src/ui/image_picker_ui.dart

110 lines
3.9 KiB
Dart
Raw Normal View History

2022-08-31 10:09:36 +02:00
import 'dart:typed_data';
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';
class ImagePickerUI {
final ImagePickerService _imagePickerService = ImagePickerService();
2022-08-31 10:09:36 +02:00
2022-09-01 10:22:32 +02:00
/// [pickImageDialog] returns a [ModalBottomSheet] widget that displays two icons.
/// When clicked on the left icon the user can pick an image from their documents on their phone.
/// When clicked on the right icon the user can make a photo with the camera on their phone.
/// The function gets all the parameters from the [image_picker] class where a function provides them all.
/// 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.
2022-08-31 13:49:25 +02:00
Future<Uint8List?> pickImageDialog(
BuildContext context,
String title,
double titleTextSize,
double iconSize,
double iconTextSize,
double spaceBetweenIcons,
2022-08-31 13:49:25 +02:00
IconData makePhotoIcon,
2022-08-31 17:03:20 +02:00
String makePhotoText,
2022-08-31 13:49:25 +02:00
IconData selectImageIcon,
2022-08-31 17:03:20 +02:00
String selectImageText,
2022-08-31 13:49:25 +02:00
String closeButtonText) async {
return await showModalBottomSheet<Uint8List?>(
2022-08-31 10:09:36 +02:00
context: context,
builder: (BuildContext context) {
return Wrap(
children: <Widget>[
ListTile(
title: Text(
title,
style: TextStyle(
fontSize: titleTextSize,
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
2022-09-01 10:22:32 +02:00
_generateIconButtonWithText(context, selectImageIcon, iconSize,
iconTextSize, ImageSource.gallery, selectImageText),
SizedBox(
width: spaceBetweenIcons,
),
2022-09-01 10:22:32 +02:00
_generateIconButtonWithText(context, makePhotoIcon, iconSize,
iconTextSize, ImageSource.camera, makePhotoText),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 300,
height: 40,
child: ElevatedButton(
onPressed: () => Navigator.of(context).pop(),
child: Text(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.
/// [icon] The icon that needs to be displayed, requires an [IconData] as value to be used.
/// [iconSize] The size of the icon.
/// [iconTextSize] The size of the text that's displayed underneath the icon.
/// [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,
IconData icon,
double iconSize,
double iconTextSize,
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),
iconSize: iconSize,
onPressed: () => _imagePickerService.pickImage(imageSource, context),
),
Text(
bottomText,
style: TextStyle(fontSize: iconTextSize),
),
2022-08-31 12:31:54 +02:00
const SizedBox(
height: 20,
),
],
);
}
2022-08-31 10:09:36 +02:00
}