adapt file picker to web

This commit is contained in:
Salvatore Giordano
2020-10-28 17:47:44 +01:00
parent d6fa0b9ee8
commit 65c5fc9d4f
+25 -18
View File
@@ -1,8 +1,8 @@
import 'dart:async'; import 'dart:async';
import 'dart:io';
import 'package:file_picker/file_picker.dart'; import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart'; import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:http_parser/http_parser.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_chat_flutter.dart';
import 'stream_channel.dart'; import 'stream_channel.dart';
typedef FileUploader = Future<String> Function(File, Channel); typedef FileUploader = Future<String> Function(PlatformFile, Channel);
typedef AttachmentThumbnailBuilder = Widget Function( typedef AttachmentThumbnailBuilder = Widget Function(
BuildContext, BuildContext,
_SendingAttachment, _SendingAttachment,
@@ -578,8 +578,8 @@ class MessageInputState extends State<MessageInput> {
case 'image': case 'image':
case 'giphy': case 'giphy':
return attachment.file != null return attachment.file != null
? Image.file( ? Image.memory(
attachment.file, attachment.file.bytes,
fit: BoxFit.cover, fit: BoxFit.cover,
) )
: Image.network( : Image.network(
@@ -711,7 +711,7 @@ class MessageInputState extends State<MessageInput> {
_inputEnabled = false; _inputEnabled = false;
}); });
File file; PlatformFile file;
String attachmentType; String attachmentType;
if (fileType == DefaultAttachmentTypes.image) { if (fileType == DefaultAttachmentTypes.image) {
@@ -729,7 +729,11 @@ class MessageInputState extends State<MessageInput> {
} else if (fileType == DefaultAttachmentTypes.video) { } else if (fileType == DefaultAttachmentTypes.video) {
pickedFile = await _imagePicker.getVideo(source: ImageSource.camera); pickedFile = await _imagePicker.getVideo(source: ImageSource.camera);
} }
file = File(pickedFile.path); final bytes = await pickedFile.readAsBytes();
file = PlatformFile(
path: pickedFile.path,
bytes: bytes,
);
} else { } else {
FileType type; FileType type;
if (fileType == DefaultAttachmentTypes.image) { if (fileType == DefaultAttachmentTypes.image) {
@@ -739,9 +743,13 @@ class MessageInputState extends State<MessageInput> {
} else if (fileType == DefaultAttachmentTypes.file) { } else if (fileType == DefaultAttachmentTypes.file) {
type = FileType.any; 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) { 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<MessageInput> {
} }
final channel = StreamChannel.of(context).channel; final channel = StreamChannel.of(context).channel;
final attachment = _SendingAttachment( final attachment = _SendingAttachment(
file: file, file: file,
attachment: Attachment( attachment: Attachment(
localUri: file.uri, localUri: file.path != null ? Uri.parse(file.path) : null,
type: attachmentType, type: attachmentType,
), ),
); );
@@ -785,7 +792,7 @@ class MessageInputState extends State<MessageInput> {
} }
Future<String> _uploadAttachment( Future<String> _uploadAttachment(
File file, PlatformFile file,
DefaultAttachmentTypes type, DefaultAttachmentTypes type,
Channel channel, Channel channel,
) async { ) async {
@@ -806,9 +813,9 @@ class MessageInputState extends State<MessageInput> {
return url; return url;
} }
Future<String> _uploadImage(File file, Channel channel) async { Future<String> _uploadImage(PlatformFile file, Channel channel) async {
final filename = file.path.split('/').last; final filename = file.name;
final bytes = await file.readAsBytes(); final bytes = file.bytes;
final res = await channel.sendImage( final res = await channel.sendImage(
MultipartFile.fromBytes( MultipartFile.fromBytes(
bytes, bytes,
@@ -819,9 +826,9 @@ class MessageInputState extends State<MessageInput> {
return res.file; return res.file;
} }
Future<String> _uploadFile(File file, Channel channel) async { Future<String> _uploadFile(PlatformFile file, Channel channel) async {
final filename = file.path.split('/').last; final filename = file.name;
final bytes = await file.readAsBytes(); final bytes = file.bytes;
final res = await channel.sendFile( final res = await channel.sendFile(
MultipartFile.fromBytes( MultipartFile.fromBytes(
bytes, bytes,
@@ -1006,7 +1013,7 @@ class MessageInputState extends State<MessageInput> {
} }
class _SendingAttachment { class _SendingAttachment {
File file; PlatformFile file;
Attachment attachment; Attachment attachment;
bool uploaded; bool uploaded;