From a637cfb62fb469701c4b7efd6eb0a9758c98d4a1 Mon Sep 17 00:00:00 2001 From: Xin Yao Date: Sun, 25 Apr 2021 10:39:11 -0700 Subject: [PATCH] WIP: remove file picker --- example/lib/pages/home_page.dart | 14 ++++++++++++++ example/pubspec.yaml | 1 + lib/widgets/editor.dart | 3 ++- lib/widgets/toolbar.dart | 25 ++++++------------------- pubspec.yaml | 1 - 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/example/lib/pages/home_page.dart b/example/lib/pages/home_page.dart index 075f3f9b..e0b33eff 100644 --- a/example/lib/pages/home_page.dart +++ b/example/lib/pages/home_page.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; +import 'package:file_picker/file_picker.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -216,6 +217,19 @@ class _HomePageState extends State { ); } + Future _pickImageWeb(OnImagePickCallback onImagePickCallback) async { + final result = await FilePicker.platform.pickFiles(); + if (result == null) { + return null; + } + + // Take first, because we don't allow picking multiple files. + final fileName = result.files.first.name!; + final file = File(fileName); + + return onImagePickCallback(file); + } + void _readOnly() { Navigator.push( super.context, diff --git a/example/pubspec.yaml b/example/pubspec.yaml index e1bd45f5..00e7c33e 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -24,6 +24,7 @@ dependencies: flutter: sdk: flutter universal_html: ^2.0.7 + file_picker: ^3.0.0 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. diff --git a/lib/widgets/editor.dart b/lib/widgets/editor.dart index d15a8789..1c80ddc6 100644 --- a/lib/widgets/editor.dart +++ b/lib/widgets/editor.dart @@ -96,7 +96,8 @@ String _standardizeImageUrl(String url) { } Widget _defaultEmbedBuilder(BuildContext context, leaf.Embed node) { - assert(!kIsWeb, 'Please provide EmbedBuilder for Web'); + assert(!kIsWeb, 'Please provide EmbedBuilder for Web ' + '(check out example directory for how to do it)'); switch (node.value.type) { case 'image': final imageUrl = _standardizeImageUrl(node.value.data); diff --git a/lib/widgets/toolbar.dart b/lib/widgets/toolbar.dart index c5fe2bab..8fbb2a11 100644 --- a/lib/widgets/toolbar.dart +++ b/lib/widgets/toolbar.dart @@ -1,6 +1,5 @@ import 'dart:io'; -import 'package:file_picker/file_picker.dart'; import 'package:filesystem_picker/filesystem_picker.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -15,7 +14,7 @@ import '../utils/color.dart'; import 'controller.dart'; typedef OnImagePickCallback = Future Function(File file); -typedef ImagePickImpl = Future Function(ImageSource source); +typedef ImagePickImpl = Future Function(ImageSource source, OnImagePickCallback onImagePickCallback); class InsertEmbedButton extends StatelessWidget { const InsertEmbedButton({ @@ -547,11 +546,12 @@ class _ImageButtonState extends State { String? imageUrl; if (widget.imagePickImpl != null) { - imageUrl = await widget.imagePickImpl!(widget.imageSource); + imageUrl = await widget.imagePickImpl!(widget.imageSource, widget.onImagePickCallback!); } else { - if (kIsWeb) { - imageUrl = await _pickImageWeb(); - } else if (Platform.isAndroid || Platform.isIOS) { + assert(!kIsWeb, 'Please provide imagePickImpl for Web ' + '(check out example directory for how to do it)'); + + if (Platform.isAndroid || Platform.isIOS) { imageUrl = await _pickImage(widget.imageSource); } else { imageUrl = await _pickImageDesktop(); @@ -564,19 +564,6 @@ class _ImageButtonState extends State { } } - Future _pickImageWeb() async { - final result = await FilePicker.platform.pickFiles(); - if (result == null) { - return null; - } - - // Take first, because we don't allow picking multiple files. - final fileName = result.files.first.name!; - final file = File(fileName); - - return widget.onImagePickCallback!(file); - } - Future _pickImage(ImageSource source) async { final pickedFile = await ImagePicker().getImage(source: source); if (pickedFile == null) { diff --git a/pubspec.yaml b/pubspec.yaml index 5e87f6d7..db58c7f7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -13,7 +13,6 @@ dependencies: flutter: sdk: flutter collection: ^1.15.0 - file_picker: ^3.0.0 filesystem_picker: ^2.0.0-nullsafety.0 flutter_colorpicker: ^0.4.0 flutter_keyboard_visibility: ^5.0.0