Add cross platform file picker
This commit is contained in:
parent
8838efc073
commit
85f9d3fd0e
|
@ -1,4 +0,0 @@
|
||||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
||||||
import 'package:image_picker/image_picker.dart';
|
|
||||||
|
|
||||||
ImagePicker useImagePicker() => useMemoized(ImagePicker.new);
|
|
|
@ -1,14 +1,13 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_hooks/flutter_hooks.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/pictrs.dart';
|
||||||
import 'package:lemmy_api_client/v3.dart';
|
import 'package:lemmy_api_client/v3.dart';
|
||||||
import 'package:url_launcher/url_launcher.dart' as ul;
|
import 'package:url_launcher/url_launcher.dart' as ul;
|
||||||
|
|
||||||
import '../hooks/delayed_loading.dart';
|
import '../hooks/delayed_loading.dart';
|
||||||
import '../hooks/image_picker.dart';
|
|
||||||
import '../hooks/stores.dart';
|
import '../hooks/stores.dart';
|
||||||
import '../l10n/l10n.dart';
|
import '../l10n/l10n.dart';
|
||||||
|
import '../util/files.dart';
|
||||||
import '../util/icons.dart';
|
import '../util/icons.dart';
|
||||||
import '../util/pictrs.dart';
|
import '../util/pictrs.dart';
|
||||||
import '../widgets/bottom_modal.dart';
|
import '../widgets/bottom_modal.dart';
|
||||||
|
@ -415,13 +414,12 @@ class _ImagePicker extends HookWidget {
|
||||||
final url = useState(initialUrl.value);
|
final url = useState(initialUrl.value);
|
||||||
final pictrsDeleteToken = useState<PictrsUploadFile?>(null);
|
final pictrsDeleteToken = useState<PictrsUploadFile?>(null);
|
||||||
|
|
||||||
final imagePicker = useImagePicker();
|
|
||||||
final accountsStore = useAccountsStore();
|
final accountsStore = useAccountsStore();
|
||||||
final delayedLoading = useDelayedLoading();
|
final delayedLoading = useDelayedLoading();
|
||||||
|
|
||||||
uploadImage() async {
|
uploadImage() async {
|
||||||
try {
|
try {
|
||||||
final pic = await imagePicker.pickImage(source: ImageSource.gallery);
|
final pic = await pickImage();
|
||||||
// pic is null when the picker was cancelled
|
// pic is null when the picker was cancelled
|
||||||
if (pic != null) {
|
if (pic != null) {
|
||||||
delayedLoading.start();
|
delayedLoading.start();
|
||||||
|
|
|
@ -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<XFile?> 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!);
|
||||||
|
}
|
||||||
|
}
|
|
@ -211,6 +211,13 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "6.1.2"
|
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:
|
fixnum:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|
|
@ -61,6 +61,7 @@ dependencies:
|
||||||
flutter_localizations:
|
flutter_localizations:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
wc_form_validators: ^1.0.0
|
wc_form_validators: ^1.0.0
|
||||||
|
file_picker: ^4.3.1
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
Loading…
Reference in New Issue