From 65c5fc9d4f4c30646a755e3b663b54d277d0ee51 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Wed, 28 Oct 2020 17:47:44 +0100 Subject: [PATCH 1/4] adapt file picker to web --- lib/src/message_input.dart | 43 ++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/lib/src/message_input.dart b/lib/src/message_input.dart index 5334b82b..577ba172 100644 --- a/lib/src/message_input.dart +++ b/lib/src/message_input.dart @@ -1,8 +1,8 @@ import 'dart:async'; -import 'dart:io'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/cupertino.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart'; import 'package:http_parser/http_parser.dart'; @@ -16,7 +16,7 @@ import 'package:stream_chat_flutter/src/user_avatar.dart'; import '../stream_chat_flutter.dart'; import 'stream_channel.dart'; -typedef FileUploader = Future Function(File, Channel); +typedef FileUploader = Future Function(PlatformFile, Channel); typedef AttachmentThumbnailBuilder = Widget Function( BuildContext, _SendingAttachment, @@ -578,8 +578,8 @@ class MessageInputState extends State { case 'image': case 'giphy': return attachment.file != null - ? Image.file( - attachment.file, + ? Image.memory( + attachment.file.bytes, fit: BoxFit.cover, ) : Image.network( @@ -711,7 +711,7 @@ class MessageInputState extends State { _inputEnabled = false; }); - File file; + PlatformFile file; String attachmentType; if (fileType == DefaultAttachmentTypes.image) { @@ -729,7 +729,11 @@ class MessageInputState extends State { } else if (fileType == DefaultAttachmentTypes.video) { pickedFile = await _imagePicker.getVideo(source: ImageSource.camera); } - file = File(pickedFile.path); + final bytes = await pickedFile.readAsBytes(); + file = PlatformFile( + path: pickedFile.path, + bytes: bytes, + ); } else { FileType type; if (fileType == DefaultAttachmentTypes.image) { @@ -739,9 +743,13 @@ class MessageInputState extends State { } else if (fileType == DefaultAttachmentTypes.file) { type = FileType.any; } - final res = await FilePicker.platform.pickFiles(type: type); + final res = await FilePicker.platform.pickFiles( + type: type, + withData: true, + ); if (res?.files?.isNotEmpty == true) { - file = File(res.files.first.path); + file = res.files.single; + print('file.bytes?.length: ${file.bytes?.length}'); } } @@ -754,11 +762,10 @@ class MessageInputState extends State { } final channel = StreamChannel.of(context).channel; - final attachment = _SendingAttachment( file: file, attachment: Attachment( - localUri: file.uri, + localUri: file.path != null ? Uri.parse(file.path) : null, type: attachmentType, ), ); @@ -785,7 +792,7 @@ class MessageInputState extends State { } Future _uploadAttachment( - File file, + PlatformFile file, DefaultAttachmentTypes type, Channel channel, ) async { @@ -806,9 +813,9 @@ class MessageInputState extends State { return url; } - Future _uploadImage(File file, Channel channel) async { - final filename = file.path.split('/').last; - final bytes = await file.readAsBytes(); + Future _uploadImage(PlatformFile file, Channel channel) async { + final filename = file.name; + final bytes = file.bytes; final res = await channel.sendImage( MultipartFile.fromBytes( bytes, @@ -819,9 +826,9 @@ class MessageInputState extends State { return res.file; } - Future _uploadFile(File file, Channel channel) async { - final filename = file.path.split('/').last; - final bytes = await file.readAsBytes(); + Future _uploadFile(PlatformFile file, Channel channel) async { + final filename = file.name; + final bytes = file.bytes; final res = await channel.sendFile( MultipartFile.fromBytes( bytes, @@ -1006,7 +1013,7 @@ class MessageInputState extends State { } class _SendingAttachment { - File file; + PlatformFile file; Attachment attachment; bool uploaded; From dcff11e18831849b9cf15b95de432ca4fc450df9 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Thu, 29 Oct 2020 09:49:22 +0100 Subject: [PATCH 2/4] fix mime type --- lib/src/message_input.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/message_input.dart b/lib/src/message_input.dart index 577ba172..36613f3a 100644 --- a/lib/src/message_input.dart +++ b/lib/src/message_input.dart @@ -814,7 +814,7 @@ class MessageInputState extends State { } Future _uploadImage(PlatformFile file, Channel channel) async { - final filename = file.name; + final filename = file.name ?? file.path?.split('/')?.last; final bytes = file.bytes; final res = await channel.sendImage( MultipartFile.fromBytes( @@ -827,7 +827,7 @@ class MessageInputState extends State { } Future _uploadFile(PlatformFile file, Channel channel) async { - final filename = file.name; + final filename = file.name ?? file.path?.split('/')?.last; final bytes = file.bytes; final res = await channel.sendFile( MultipartFile.fromBytes( From 145351846dbb66474c89827e896841bdb2bbb4d1 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Thu, 29 Oct 2020 10:02:22 +0100 Subject: [PATCH 3/4] remove useless native code --- lib/src/message_input.dart | 86 ++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/lib/src/message_input.dart b/lib/src/message_input.dart index 36613f3a..13f8291c 100644 --- a/lib/src/message_input.dart +++ b/lib/src/message_input.dart @@ -664,22 +664,24 @@ class MessageInputState extends State { Navigator.pop(context); }, ), - ListTile( - leading: Icon(Icons.camera_alt), - title: Text('Photo from camera'), - onTap: () { - pickFile(DefaultAttachmentTypes.image, true); - Navigator.pop(context); - }, - ), - ListTile( - leading: Icon(Icons.videocam), - title: Text('Video from camera'), - onTap: () { - pickFile(DefaultAttachmentTypes.video, true); - Navigator.pop(context); - }, - ), + if (!kIsWeb) + ListTile( + leading: Icon(Icons.camera_alt), + title: Text('Photo from camera'), + onTap: () { + pickFile(DefaultAttachmentTypes.image, true); + Navigator.pop(context); + }, + ), + if (!kIsWeb) + ListTile( + leading: Icon(Icons.videocam), + title: Text('Video from camera'), + onTap: () { + pickFile(DefaultAttachmentTypes.video, true); + Navigator.pop(context); + }, + ), ListTile( leading: Icon(Icons.insert_drive_file), title: Text('Upload a file'), @@ -943,34 +945,36 @@ class MessageInputState extends State { void initState() { super.initState(); - _keyboardListener = KeyboardVisibility.onChange.listen((visible) { - if (visible) { - if (_commandsOverlay != null) { - if (textEditingController.text.startsWith('/')) { - WidgetsBinding.instance.addPostFrameCallback((_) { - _commandsOverlay = _buildCommandsOverlayEntry(); - Overlay.of(context).insert(_commandsOverlay); - }); + if (!kIsWeb) { + _keyboardListener = KeyboardVisibility.onChange.listen((visible) { + if (visible) { + if (_commandsOverlay != null) { + if (textEditingController.text.startsWith('/')) { + WidgetsBinding.instance.addPostFrameCallback((_) { + _commandsOverlay = _buildCommandsOverlayEntry(); + Overlay.of(context).insert(_commandsOverlay); + }); + } } - } - if (_mentionsOverlay != null) { - if (textEditingController.text.contains('@')) { - WidgetsBinding.instance.addPostFrameCallback((_) { - _mentionsOverlay = _buildCommandsOverlayEntry(); - Overlay.of(context).insert(_mentionsOverlay); - }); + if (_mentionsOverlay != null) { + if (textEditingController.text.contains('@')) { + WidgetsBinding.instance.addPostFrameCallback((_) { + _mentionsOverlay = _buildCommandsOverlayEntry(); + Overlay.of(context).insert(_mentionsOverlay); + }); + } + } + } else { + if (_commandsOverlay != null) { + _commandsOverlay.remove(); + } + if (_mentionsOverlay != null) { + _mentionsOverlay.remove(); } } - } else { - if (_commandsOverlay != null) { - _commandsOverlay.remove(); - } - if (_mentionsOverlay != null) { - _mentionsOverlay.remove(); - } - } - }); + }); + } textEditingController = widget.textEditingController ?? TextEditingController(); @@ -997,7 +1001,7 @@ class MessageInputState extends State { void dispose() { _commandsOverlay?.remove(); _mentionsOverlay?.remove(); - _keyboardListener.cancel(); + _keyboardListener?.cancel(); super.dispose(); } From 3a575dcbd80ee335848db5a54ffa85c818d6376f Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Thu, 29 Oct 2020 10:27:43 +0100 Subject: [PATCH 4/4] trigger action on prs on every branch --- .github/workflows/main.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4cd593cf..373aa879 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -5,8 +5,6 @@ on: branches: - master pull_request: - branches: - - master release: types: - created