Merge pull request #117 from GetStream/hotfix/webFilePicker
adapt file picker to web
This commit is contained in:
+70
-59
@@ -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<String> Function(File, Channel);
|
||||
typedef FileUploader = Future<String> Function(PlatformFile, Channel);
|
||||
typedef AttachmentThumbnailBuilder = Widget Function(
|
||||
BuildContext,
|
||||
_SendingAttachment,
|
||||
@@ -578,8 +578,8 @@ class MessageInputState extends State<MessageInput> {
|
||||
case 'image':
|
||||
case 'giphy':
|
||||
return attachment.file != null
|
||||
? Image.file(
|
||||
attachment.file,
|
||||
? Image.memory(
|
||||
attachment.file.bytes,
|
||||
fit: BoxFit.cover,
|
||||
)
|
||||
: Image.network(
|
||||
@@ -664,22 +664,24 @@ class MessageInputState extends State<MessageInput> {
|
||||
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'),
|
||||
@@ -711,7 +713,7 @@ class MessageInputState extends State<MessageInput> {
|
||||
_inputEnabled = false;
|
||||
});
|
||||
|
||||
File file;
|
||||
PlatformFile file;
|
||||
String attachmentType;
|
||||
|
||||
if (fileType == DefaultAttachmentTypes.image) {
|
||||
@@ -729,7 +731,11 @@ class MessageInputState extends State<MessageInput> {
|
||||
} 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 +745,13 @@ class MessageInputState extends State<MessageInput> {
|
||||
} 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 +764,10 @@ class MessageInputState extends State<MessageInput> {
|
||||
}
|
||||
|
||||
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 +794,7 @@ class MessageInputState extends State<MessageInput> {
|
||||
}
|
||||
|
||||
Future<String> _uploadAttachment(
|
||||
File file,
|
||||
PlatformFile file,
|
||||
DefaultAttachmentTypes type,
|
||||
Channel channel,
|
||||
) async {
|
||||
@@ -806,9 +815,9 @@ class MessageInputState extends State<MessageInput> {
|
||||
return url;
|
||||
}
|
||||
|
||||
Future<String> _uploadImage(File file, Channel channel) async {
|
||||
final filename = file.path.split('/').last;
|
||||
final bytes = await file.readAsBytes();
|
||||
Future<String> _uploadImage(PlatformFile file, Channel channel) async {
|
||||
final filename = file.name ?? file.path?.split('/')?.last;
|
||||
final bytes = file.bytes;
|
||||
final res = await channel.sendImage(
|
||||
MultipartFile.fromBytes(
|
||||
bytes,
|
||||
@@ -819,9 +828,9 @@ class MessageInputState extends State<MessageInput> {
|
||||
return res.file;
|
||||
}
|
||||
|
||||
Future<String> _uploadFile(File file, Channel channel) async {
|
||||
final filename = file.path.split('/').last;
|
||||
final bytes = await file.readAsBytes();
|
||||
Future<String> _uploadFile(PlatformFile file, Channel channel) async {
|
||||
final filename = file.name ?? file.path?.split('/')?.last;
|
||||
final bytes = file.bytes;
|
||||
final res = await channel.sendFile(
|
||||
MultipartFile.fromBytes(
|
||||
bytes,
|
||||
@@ -936,34 +945,36 @@ class MessageInputState extends State<MessageInput> {
|
||||
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();
|
||||
@@ -990,7 +1001,7 @@ class MessageInputState extends State<MessageInput> {
|
||||
void dispose() {
|
||||
_commandsOverlay?.remove();
|
||||
_mentionsOverlay?.remove();
|
||||
_keyboardListener.cancel();
|
||||
_keyboardListener?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -1006,7 +1017,7 @@ class MessageInputState extends State<MessageInput> {
|
||||
}
|
||||
|
||||
class _SendingAttachment {
|
||||
File file;
|
||||
PlatformFile file;
|
||||
Attachment attachment;
|
||||
bool uploaded;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user