feat(chat): attach files on Android and iOS with the native picker
This commit is contained in:
@@ -3,6 +3,7 @@ import 'dart:typed_data';
|
|||||||
import 'chat_attachment_draft.dart';
|
import 'chat_attachment_draft.dart';
|
||||||
import 'chat_attachment_picker_stub.dart'
|
import 'chat_attachment_picker_stub.dart'
|
||||||
if (dart.library.js_interop) 'chat_attachment_picker_web.dart'
|
if (dart.library.js_interop) 'chat_attachment_picker_web.dart'
|
||||||
|
if (dart.library.io) 'chat_attachment_picker_native.dart'
|
||||||
as impl;
|
as impl;
|
||||||
|
|
||||||
/// One file the operator chose in the host's file picker, with the bytes the
|
/// One file the operator chose in the host's file picker, with the bytes the
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import 'package:file_picker/file_picker.dart';
|
||||||
|
|
||||||
|
import 'chat_attachment_picker.dart';
|
||||||
|
|
||||||
|
/// The native (Android/iOS) file picker.
|
||||||
|
///
|
||||||
|
/// It uses the platform's own document picker through `file_picker`, reads the
|
||||||
|
/// bytes with the selection so the draft owns one copy, and records the file's
|
||||||
|
/// name and a MIME type derived from its extension. A cancelled selection
|
||||||
|
/// returns null, exactly like the web picker.
|
||||||
|
ChatAttachmentPicker get hostChatAttachmentPicker =>
|
||||||
|
const ChatAttachmentPicker(select: _selectFiles);
|
||||||
|
|
||||||
|
const Map<String, String> _mimeByExtension = {
|
||||||
|
'png': 'image/png',
|
||||||
|
'jpg': 'image/jpeg',
|
||||||
|
'jpeg': 'image/jpeg',
|
||||||
|
'gif': 'image/gif',
|
||||||
|
'webp': 'image/webp',
|
||||||
|
'heic': 'image/heic',
|
||||||
|
'pdf': 'application/pdf',
|
||||||
|
'txt': 'text/plain',
|
||||||
|
'md': 'text/markdown',
|
||||||
|
'json': 'application/json',
|
||||||
|
'csv': 'text/csv',
|
||||||
|
'html': 'text/html',
|
||||||
|
'zip': 'application/zip',
|
||||||
|
};
|
||||||
|
|
||||||
|
String _mimeFor(String name) {
|
||||||
|
final dot = name.lastIndexOf('.');
|
||||||
|
if (dot < 0 || dot == name.length - 1) return '';
|
||||||
|
return _mimeByExtension[name.substring(dot + 1).toLowerCase()] ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<ChatPickedFile>?> _selectFiles() async {
|
||||||
|
final selected = await FilePicker.pickFiles(type: FileType.any);
|
||||||
|
if (selected.isEmpty) return null;
|
||||||
|
final files = <ChatPickedFile>[];
|
||||||
|
for (final file in selected) {
|
||||||
|
final bytes = await file.xFile.readAsBytes();
|
||||||
|
files.add(
|
||||||
|
ChatPickedFile(name: file.name, mime: _mimeFor(file.name), bytes: bytes),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return files;
|
||||||
|
}
|
||||||
+4
-4
@@ -141,18 +141,18 @@ packages:
|
|||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: file_picker
|
name: file_picker
|
||||||
sha256: "98c0b156b6380ba55bc767a14e2e6b3feb246e713ad40092424596fa79005bea"
|
sha256: e67f1bcbef00f8b28f3b6ce09a1f0d2d9de37cf1e1e7e73b70df7a20d3eee8be
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "13.1.0"
|
version: "13.3.0"
|
||||||
file_picker_darwin:
|
file_picker_darwin:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: file_picker_darwin
|
name: file_picker_darwin
|
||||||
sha256: ca2918264c453400c80ef0d69ca3f8b816f894db6144bebac73064ad64927e0e
|
sha256: "13e731433fb3e59c1aff5a1197e41c5c5f930fad16342d101579086271a70d7b"
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.3.3"
|
version: "2.4.0"
|
||||||
file_picker_linux:
|
file_picker_linux:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
+1
-1
@@ -46,7 +46,7 @@ dependencies:
|
|||||||
path_provider: ^2.1.6
|
path_provider: ^2.1.6
|
||||||
open_filex: ^5.0.0
|
open_filex: ^5.0.0
|
||||||
share_plus: ^13.3.1
|
share_plus: ^13.3.1
|
||||||
file_picker: ^13.1.0
|
file_picker: ^13.3.0
|
||||||
url_launcher: ^6.3.3
|
url_launcher: ^6.3.3
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
|
|||||||
Reference in New Issue
Block a user