From c2891c3c84b5687ee625621705f4baa458c5fd34 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 25 Apr 2019 11:13:02 +0200 Subject: [PATCH] Added image picker --- lib/ui/screens/conversation_screen.dart | 12 ++++-- lib/utils/files_utils.dart | 50 +++++++++++++++++++++++++ pubspec.lock | 7 ++++ pubspec.yaml | 3 ++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 lib/utils/files_utils.dart diff --git a/lib/ui/screens/conversation_screen.dart b/lib/ui/screens/conversation_screen.dart index de90f12..3aaef61 100644 --- a/lib/ui/screens/conversation_screen.dart +++ b/lib/ui/screens/conversation_screen.dart @@ -4,6 +4,7 @@ import 'package:comunic/lists/conversation_messages_list.dart'; import 'package:comunic/lists/users_list.dart'; import 'package:comunic/models/new_conversation_message.dart'; import 'package:comunic/ui/tiles/conversation_message_tile.dart'; +import 'package:comunic/utils/files_utils.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/list_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; @@ -82,6 +83,11 @@ class _ConversationScreenState extends State { }); } + /// Pick and send an image + Future _sendImage(BuildContext context) async { + final image = await pickImage(context); + } + /// Send a new message Future _submitMessage(BuildContext context, String content) async { //Send the message @@ -141,15 +147,15 @@ class _ConversationScreenState extends State { child: new Row( children: [ // Image area - /*new Container( + new Container( margin: new EdgeInsets.symmetric(horizontal: 4.0), child: new IconButton( icon: new Icon( Icons.photo_camera, color: Theme.of(context).accentColor, ), - onPressed: () async {}), - ),*/ + onPressed: () => _sendImage(context)), + ), // Message area new Flexible( diff --git a/lib/utils/files_utils.dart b/lib/utils/files_utils.dart new file mode 100644 index 0000000..38dfee2 --- /dev/null +++ b/lib/utils/files_utils.dart @@ -0,0 +1,50 @@ +import 'dart:io'; + +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 +Future pickImage(BuildContext context) async { + /// 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: [ + //Gallery + FlatButton( + onPressed: () => Navigator.pop(context, _ChooseImageSource.GALLERY), + child: Text( + tr("Image gallery").toUpperCase(), + ), + ), + + // Camera + FlatButton( + onPressed: () => Navigator.pop(context, _ChooseImageSource.CAMERA), + child: Text( + tr("Camera").toUpperCase(), + ), + ), + ], + ); + }, + ); + + if (result == null) return null; + + return await ImagePicker.pickImage( + source: result == _ChooseImageSource.CAMERA + ? ImageSource.camera + : ImageSource.gallery); +} diff --git a/pubspec.lock b/pubspec.lock index 2a87485..dd10036 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -46,6 +46,13 @@ packages: description: flutter source: sdk version: "0.0.0" + image_picker: + dependency: "direct main" + description: + name: image_picker + url: "https://pub.dartlang.org" + source: hosted + version: "0.5.4+3" matcher: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index d97dd06..640ab84 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -30,6 +30,9 @@ dependencies: # SQLite database is used for caching sqflite: ^1.1.5 + # Image picker is used whenever the user wants to send an image + image_picker: ^0.5.4+3 + dev_dependencies: flutter_test: sdk: flutter