1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2025-06-19 00:05:16 +00:00

Simplify image picking code

This commit is contained in:
2021-03-13 18:03:20 +01:00
parent ea45bf828c
commit e70aaabbc9
14 changed files with 85 additions and 105 deletions

View File

@ -1,54 +1,27 @@
import 'dart:io';
import 'package:comunic/utils/intl_utils.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:random_string/random_string.dart';
import '../models/api_request.dart';
import '../ui/dialogs/pick_file_dialog.dart';
/// Files utilities
///
/// @author Pierre HUBERT
enum _ChooseImageSource { GALLERY, CAMERA }
/// Ask the user to choose an image, either from the gallery or using the camera
///
/// Returns null in case of failure
Future<PickedFile> pickImage(BuildContext context) async {
/// First, we ask the user to choose between image picker and camera
final result = await showDialog<_ChooseImageSource>(
/// Throws an exception null in case of failure
Future<BytesFile> pickImage(BuildContext context) async {
return await showPickFileDialog(
context: context,
builder: (c) {
return AlertDialog(
title: Text(tr("Choose an image")),
actions: <Widget>[
//Gallery
TextButton(
onPressed: () => Navigator.pop(context, _ChooseImageSource.GALLERY),
child: Text(
tr("Image gallery").toUpperCase(),
),
),
// Camera
TextButton(
onPressed: () => Navigator.pop(context, _ChooseImageSource.CAMERA),
child: Text(
tr("Camera").toUpperCase(),
),
),
],
);
},
allowedMimeTypes: ["image/png", "image/jpeg", "image/gif"],
imageMaxHeight: 10000,
imageMaxWidth: 10000,
);
if (result == null) return null;
return await ImagePicker().getImage(
source: result == _ChooseImageSource.CAMERA
? ImageSource.camera
: ImageSource.gallery);
}
/// Generate a new temporary file