From 85f9d3fd0ea80a151964ff42f677885fc8b41693 Mon Sep 17 00:00:00 2001 From: shilangyu Date: Sun, 16 Jan 2022 16:34:03 +0100 Subject: [PATCH] Add cross platform file picker --- lib/hooks/image_picker.dart | 4 ---- lib/pages/manage_account.dart | 6 ++---- lib/util/files.dart | 18 ++++++++++++++++++ pubspec.lock | 7 +++++++ pubspec.yaml | 1 + 5 files changed, 28 insertions(+), 8 deletions(-) delete mode 100644 lib/hooks/image_picker.dart create mode 100644 lib/util/files.dart diff --git a/lib/hooks/image_picker.dart b/lib/hooks/image_picker.dart deleted file mode 100644 index 709d480..0000000 --- a/lib/hooks/image_picker.dart +++ /dev/null @@ -1,4 +0,0 @@ -import 'package:flutter_hooks/flutter_hooks.dart'; -import 'package:image_picker/image_picker.dart'; - -ImagePicker useImagePicker() => useMemoized(ImagePicker.new); diff --git a/lib/pages/manage_account.dart b/lib/pages/manage_account.dart index 2a0ba71..6cd017d 100644 --- a/lib/pages/manage_account.dart +++ b/lib/pages/manage_account.dart @@ -1,14 +1,13 @@ import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; -import 'package:image_picker/image_picker.dart'; import 'package:lemmy_api_client/pictrs.dart'; import 'package:lemmy_api_client/v3.dart'; import 'package:url_launcher/url_launcher.dart' as ul; import '../hooks/delayed_loading.dart'; -import '../hooks/image_picker.dart'; import '../hooks/stores.dart'; import '../l10n/l10n.dart'; +import '../util/files.dart'; import '../util/icons.dart'; import '../util/pictrs.dart'; import '../widgets/bottom_modal.dart'; @@ -415,13 +414,12 @@ class _ImagePicker extends HookWidget { final url = useState(initialUrl.value); final pictrsDeleteToken = useState(null); - final imagePicker = useImagePicker(); final accountsStore = useAccountsStore(); final delayedLoading = useDelayedLoading(); uploadImage() async { try { - final pic = await imagePicker.pickImage(source: ImageSource.gallery); + final pic = await pickImage(); // pic is null when the picker was cancelled if (pic != null) { delayedLoading.start(); diff --git a/lib/util/files.dart b/lib/util/files.dart new file mode 100644 index 0000000..21d7bf8 --- /dev/null +++ b/lib/util/files.dart @@ -0,0 +1,18 @@ +import 'dart:io'; + +import 'package:file_picker/file_picker.dart'; +import 'package:flutter/foundation.dart'; +import 'package:image_picker/image_picker.dart'; + +/// Picks a single image from the system +Future pickImage() async { + if (kIsWeb || Platform.isIOS || Platform.isAndroid) { + return ImagePicker().pickImage(source: ImageSource.gallery); + } else { + final result = await FilePicker.platform.pickFiles(type: FileType.image); + + if (result == null) return null; + + return XFile(result.files.single.path!); + } +} diff --git a/pubspec.lock b/pubspec.lock index b216c05..984edbc 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -211,6 +211,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "6.1.2" + file_picker: + dependency: "direct main" + description: + name: file_picker + url: "https://pub.dartlang.org" + source: hosted + version: "4.3.1" fixnum: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 85d77d2..8aba46e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -61,6 +61,7 @@ dependencies: flutter_localizations: sdk: flutter wc_form_validators: ^1.0.0 + file_picker: ^4.3.1 dev_dependencies: flutter_test: