Skip to content
This repository has been archived by the owner on Jan 17, 2024. It is now read-only.

Commit

Permalink
feat(ImageManager): Add camera option
Browse files Browse the repository at this point in the history
  • Loading branch information
IsAvaible authored Jan 4, 2024
1 parent a07b4a4 commit 3429485
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/components/ImagePicker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ImagePicker extends StatefulWidget {
class _ImagePickerState extends State<ImagePicker> {
Future getImage() async {
try {
final pickedFile = await ImageManager.pickImage();
final pickedFile = await ImageManager.pickImage(context);

final _i = File(pickedFile.path);
if (_i.lengthSync() > 50 * 1024 * 1024) {
Expand Down
8 changes: 6 additions & 2 deletions lib/components/Popup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ class Popup extends StatelessWidget {
),
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 15, horizontal: 30),
const EdgeInsets.symmetric(vertical: 15),
child: Material(
color: Colors.transparent,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
description != null
? Padding(
padding: const EdgeInsets.only(bottom: 10),
padding: const EdgeInsets.fromLTRB(30, 0, 10, 30),
child: Text(
description!,
style: const TextStyle(fontSize: 20),
Expand All @@ -149,6 +149,10 @@ class Popup extends StatelessWidget {
for (Option option in options) {
list.add(
TextButton.icon(
style: TextButton.styleFrom(
foregroundColor: option.color,
padding: const EdgeInsets.symmetric(horizontal: 30),
),
onPressed: option.callback,
icon: Image.asset(
option.icon,
Expand Down
43 changes: 40 additions & 3 deletions lib/imagemanager/ImageManager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,47 @@ class ImageManager {
}
}

/// Opens the image picker and returns the picked image
static Future<File> pickImage() async {
static Future<File> pickImage(BuildContext context) async {
final ImagePicker picker = ImagePicker();
final XFile? image = await picker.pickImage(source: ImageSource.gallery);
// Show dialog to ask user for source type
final ImageSource? source = await showDialog<ImageSource>(
context: context,
builder: (BuildContext context) => SimpleDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
title: const Text('Quelle auswählen'),
children: <Widget>[
SimpleDialogOption(
child: const Row(
children: [
Icon(Icons.camera),
SizedBox(width: 16), // Add space between icon and text
Text('Kamera'),
],
),
onPressed: () => Navigator.of(context).pop(ImageSource.camera),
),
SimpleDialogOption(
child: const Row(
children: [
Icon(Icons.photo_library),
SizedBox(width: 16), // Add space between icon and text
Text('Gallerie'),
],
),
onPressed: () => Navigator.of(context).pop(ImageSource.gallery),
),
],
),
);

if (source == null) {
// User cancelled the dialog
throw Exception('No image selected');
}

final XFile? image = await picker.pickImage(source: source);
if (image != null) {
return File(image.path);
} else {
Expand Down

0 comments on commit 3429485

Please sign in to comment.