1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-23 06:53:23 +00:00
comunicmobile/lib/utils/files_utils.dart

57 lines
1.7 KiB
Dart
Raw Normal View History

2019-04-25 09:13:02 +00:00
import 'package:comunic/utils/intl_utils.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.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
2021-02-07 16:09:08 +00:00
Future<PickedFile> pickImage(BuildContext context) async {
2019-04-25 09:13:02 +00:00
/// First, we ask the user to choose between image picker and camera
final result = await showDialog<_ChooseImageSource>(
context: context,
builder: (c) {
return AlertDialog(
title: Text(tr("Choose an image")),
actions: <Widget>[
//Gallery
2021-03-13 14:38:43 +00:00
TextButton(
2019-04-25 09:13:02 +00:00
onPressed: () => Navigator.pop(context, _ChooseImageSource.GALLERY),
child: Text(
tr("Image gallery").toUpperCase(),
),
),
// Camera
2021-03-13 14:38:43 +00:00
TextButton(
2019-04-25 09:13:02 +00:00
onPressed: () => Navigator.pop(context, _ChooseImageSource.CAMERA),
child: Text(
tr("Camera").toUpperCase(),
),
),
],
);
},
);
if (result == null) return null;
2021-02-07 16:09:08 +00:00
return await ImagePicker().getImage(
2019-04-25 09:13:02 +00:00
source: result == _ChooseImageSource.CAMERA
? ImageSource.camera
: ImageSource.gallery);
}
/// Check if a mime type maps to an image or not
bool isImage(String mimeType) => mimeType.startsWith("image/");
2021-03-12 18:10:10 +00:00
2021-03-12 19:52:26 +00:00
/// Check if a mime type maps to a video or not
2021-03-12 18:10:10 +00:00
bool isVideo(String mimeType) => mimeType.startsWith("video/mp4");
2021-03-12 19:52:26 +00:00
/// Check if a mime type maps to an audio file or not
bool isAudio(String mimeType) => mimeType.startsWith("audio/mpeg");