An open-source cross-platform alternative to AirDrop
feat: add file_selector package (#643)
Tien Do Nam6b4ff0b7c80b
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xmlindex ad3bb9f8..da419b48 100644--- a/android/app/src/main/AndroidManifest.xml+++ b/android/app/src/main/AndroidManifest.xml@@ -24,7 +24,9 @@android:name="${applicationName}"android:usesCleartextTraffic="true"android:banner="@drawable/banner"- android:icon="@mipmap/ic_launcher">+ android:icon="@mipmap/ic_launcher"+ android:requestLegacyExternalStorage="true"+ ><!-- localsend: prevent multiple instances by android:launchMode="singleTask" --><activitydiff --git a/lib/pages/receive_options_page.dart b/lib/pages/receive_options_page.dartindex 91a8a61a..a77ce779 100644--- a/lib/pages/receive_options_page.dart+++ b/lib/pages/receive_options_page.dart@@ -1,9 +1,9 @@-import 'package:file_picker/file_picker.dart';import 'package:flutter/material.dart';import 'package:localsend_app/gen/strings.g.dart';import 'package:localsend_app/provider/network/server/server_provider.dart';import 'package:localsend_app/provider/selection/selected_receiving_files_provider.dart';import 'package:localsend_app/util/file_size_helper.dart';+import 'package:localsend_app/util/native/pick_directory_path.dart';import 'package:localsend_app/util/native/platform_check.dart';import 'package:localsend_app/widget/custom_dropdown_button.dart';import 'package:localsend_app/widget/custom_icon_button.dart';@@ -41,7 +41,7 @@ class ReceiveOptionsPage extends StatelessWidget {padding: const EdgeInsets.only(left: 10),child: CustomIconButton(onPressed: () async {- final directory = await FilePicker.platform.getDirectoryPath();+ final directory = await pickDirectoryPath();if (directory != null) {ref.notifier(serverProvider).setSessionDestinationDir(directory);}diff --git a/lib/pages/tabs/settings_tab.dart b/lib/pages/tabs/settings_tab.dartindex b3d8ddc4..f6afda8f 100644--- a/lib/pages/tabs/settings_tab.dart+++ b/lib/pages/tabs/settings_tab.dart@@ -1,4 +1,3 @@-import 'package:file_picker/file_picker.dart';import 'package:flutter/material.dart';import 'package:localsend_app/constants.dart';import 'package:localsend_app/gen/strings.g.dart';@@ -13,6 +12,7 @@ import 'package:localsend_app/provider/settings_provider.dart';import 'package:localsend_app/provider/version_provider.dart';import 'package:localsend_app/theme.dart';import 'package:localsend_app/util/native/autostart_helper.dart';+import 'package:localsend_app/util/native/pick_directory_path.dart';import 'package:localsend_app/util/native/platform_check.dart';import 'package:localsend_app/util/sleep.dart';import 'package:localsend_app/util/ui/snackbar.dart';@@ -254,7 +254,7 @@ class _SettingsTabState extends State<SettingsTab> with Riverpie {return;}- final directory = await FilePicker.platform.getDirectoryPath();+ final directory = await pickDirectoryPath();if (directory != null) {await ref.notifier(settingsProvider).setDestination(directory);}diff --git a/lib/util/native/file_picker.dart b/lib/util/native/file_picker.dartindex 4c46187e..91130a42 100644--- a/lib/util/native/file_picker.dart+++ b/lib/util/native/file_picker.dart@@ -1,11 +1,13 @@import 'dart:async';import 'package:file_picker/file_picker.dart';+import 'package:file_selector/file_selector.dart' as file_selector;import 'package:flutter/material.dart';import 'package:localsend_app/gen/strings.g.dart';import 'package:localsend_app/pages/apk_picker_page.dart';import 'package:localsend_app/provider/selection/selected_sending_files_provider.dart';import 'package:localsend_app/theme.dart';+import 'package:localsend_app/util/native/pick_directory_path.dart';import 'package:localsend_app/util/native/platform_check.dart';import 'package:localsend_app/util/sleep.dart';import 'package:localsend_app/util/ui/asset_picker_translated_text_delegate.dart';@@ -92,12 +94,23 @@ enum FilePickerOption {);}try {- final result = await FilePicker.platform.pickFiles(allowMultiple: true);- if (result != null) {- await ref.notifier(selectedSendingFilesProvider).addFiles(- files: result.files,- converter: CrossFileConverters.convertPlatformFile,- );+ if (checkPlatform([TargetPlatform.android])) {+ // We also need to use the file_picker package because file_selector does not expose the raw path.+ final result = await FilePicker.platform.pickFiles(allowMultiple: true);+ if (result != null) {+ await ref.notifier(selectedSendingFilesProvider).addFiles(+ files: result.files,+ converter: CrossFileConverters.convertPlatformFile,+ );+ }+ } else {+ final result = await file_selector.openFiles();+ if (result.isNotEmpty) {+ await ref.notifier(selectedSendingFilesProvider).addFiles<file_selector.XFile>(+ files: result,+ converter: CrossFileConverters.convertXFile,+ );+ }}} catch (e) {// ignore: use_build_context_synchronously@@ -130,7 +143,7 @@ enum FilePickerOption {);await sleepAsync(200); // Wait for the dialog to be showntry {- final directoryPath = await FilePicker.platform.getDirectoryPath();+ final directoryPath = await pickDirectoryPath();if (directoryPath != null) {await ref.notifier(selectedSendingFilesProvider).addDirectory(directoryPath);}diff --git a/lib/util/native/pick_directory_path.dart b/lib/util/native/pick_directory_path.dartnew file mode 100644index 00000000..c489884d--- /dev/null+++ b/lib/util/native/pick_directory_path.dart@@ -0,0 +1,18 @@+import 'package:file_picker/file_picker.dart';+import 'package:file_selector/file_selector.dart' as file_selector;+import 'package:flutter/material.dart';+import 'package:localsend_app/util/native/platform_check.dart';++/// Opens a file picker to select a directory.+/// Returns null if the user cancels the selection.+Future<String?> pickDirectoryPath() async {+ if (checkPlatform([TargetPlatform.android])) {+ /// We need to use the file_picker package because file_selector does not expose the raw path.+ /// We need the raw path to properly manipulate the path to save new files, or+ /// to list files recursively.+ /// https://github.com/miguelpruivo/flutter_file_picker/issues/1282+ return await FilePicker.platform.getDirectoryPath();+ } else {+ return await file_selector.getDirectoryPath();+ }+}diff --git a/pubspec.lock b/pubspec.lockindex 7997b033..34fd915f 100644--- a/pubspec.lock+++ b/pubspec.lock@@ -369,6 +369,30 @@ packages:url: "https://pub.dev"source: hostedversion: "5.3.4"+ file_selector:+ dependency: "direct main"+ description:+ name: file_selector+ sha256: "59b35aa4af7988be7ec88f9ddaa6c71c5b54bf0f8b35009389d9343b10e9c3af"+ url: "https://pub.dev"+ source: hosted+ version: "1.0.0"+ file_selector_android:+ dependency: transitive+ description:+ name: file_selector_android+ sha256: "43e5c719f671b9181bef1bf2851135c3ad993a9a6c804a4ccb07579cfee84e34"+ url: "https://pub.dev"+ source: hosted+ version: "0.5.0+2"+ file_selector_ios:+ dependency: transitive+ description:+ name: file_selector_ios+ sha256: "507af301b21b1dbb6fd0615ba21190b2b4574edb672929f32ce7f610c40a9bd9"+ url: "https://pub.dev"+ source: hosted+ version: "0.5.1+5"file_selector_linux:dependency: transitivedescription:@@ -393,6 +417,14 @@ packages:url: "https://pub.dev"source: hostedversion: "2.6.0"+ file_selector_web:+ dependency: transitive+ description:+ name: file_selector_web+ sha256: e292740c469df0aeeaba0895bf622bea351a05e87d22864c826bf21c4780e1d7+ url: "https://pub.dev"+ source: hosted+ version: "0.9.2"file_selector_windows:dependency: transitivedescription:diff --git a/pubspec.yaml b/pubspec.yamlindex b1a79e0d..61405696 100644--- a/pubspec.yaml+++ b/pubspec.yaml@@ -19,6 +19,7 @@ dependencies:dio: 5.3.2dynamic_color: 1.6.6file_picker: 5.3.4+ file_selector: 1.0.0flutter:sdk: flutterflutter_localizations: