@@ -13,6 +13,7 @@ import 'package:flutter_quill/flutter_quill.dart' hide Text;
1313import 'package:flutter_quill_extensions/flutter_quill_extensions.dart' ;
1414import 'package:path/path.dart' ;
1515import 'package:path_provider/path_provider.dart' ;
16+ import 'package:mime/mime.dart' ;
1617
1718import 'web_embeds.dart' ;
1819
@@ -45,9 +46,9 @@ class _HomePageState extends State<HomePage> {
4546
4647 Future <void > _initializeText () async {
4748 final doc = Document ()..insert (0 , 'Just a friendly empty text :)' );
48- setState (() {
49- _controller = QuillController (document: doc, selection: const TextSelection .collapsed (offset: 0 ));
50- });
49+ setState (() {
50+ _controller = QuillController (document: doc, selection: const TextSelection .collapsed (offset: 0 ));
51+ });
5152 }
5253
5354 @override
@@ -204,11 +205,12 @@ class _HomePageState extends State<HomePage> {
204205 showFormulaButton: false ,
205206 showVideoButton: false ,
206207 showImageButton: true ,
207-
208+
208209 // provide a callback to enable picking images from device.
209210 // if omit, "image" button only allows adding images from url.
210211 // same goes for videos.
211212 onImagePickCallback: _onImagePickCallback,
213+ webImagePickImpl: _webImagePickImpl,
212214 mediaPickSettingSelector: (context) {
213215 return Future .value (MediaPickSetting .Gallery );
214216 },
@@ -217,7 +219,7 @@ class _HomePageState extends State<HomePage> {
217219 // uncomment to provide a custom "pick from" dialog.
218220 // cameraPickSettingSelector: _selectCameraPickSetting,
219221 );
220- var toolbar = QuillToolbar (
222+ final toolbar = QuillToolbar (
221223 afterButtonPressed: _focusNode.requestFocus,
222224 children: [
223225 HistoryButton (
@@ -256,23 +258,10 @@ class _HomePageState extends State<HomePage> {
256258 iconSize: toolbarIconSize,
257259 controller: _controller! ,
258260 ),
259-
260261 for (final builder in embedButtons) builder (_controller! , toolbarIconSize, null , null ),
261262 ],
262263 );
263264
264- if (kIsWeb) {
265- toolbar = QuillToolbar .basic (
266- controller: _controller! ,
267- embedButtons: FlutterQuillEmbeds .buttons (
268- onImagePickCallback: _onImagePickCallback,
269- webImagePickImpl: _webImagePickImpl,
270- ),
271- showAlignmentButtons: true ,
272- afterButtonPressed: _focusNode.requestFocus,
273- );
274- }
275-
276265 return SafeArea (
277266 child: Column (
278267 mainAxisAlignment: MainAxisAlignment .spaceBetween,
@@ -301,22 +290,45 @@ class _HomePageState extends State<HomePage> {
301290 // You can also upload the picked image to any server (eg : AWS s3
302291 // or Firebase) and then return the uploaded image URL.
303292 Future <String > _onImagePickCallback (File file) async {
304- // Copies the picked file from temporary cache to applications directory
305- final appDocDir = await getApplicationDocumentsDirectory ();
306- final copiedFile = await file.copy ('${appDocDir .path }/${basename (file .path )}' );
307- return copiedFile.path.toString ();
293+ if (! kIsWeb) {
294+ // Copies the picked file from temporary cache to applications directory
295+ final appDocDir = await getApplicationDocumentsDirectory ();
296+ final copiedFile = await file.copy ('${appDocDir .path }/${basename (file .path )}' );
297+ return copiedFile.path.toString ();
298+ } else {
299+
300+
301+ // This will fail on web
302+ return file.path;
303+ }
308304 }
309305
310306 Future <String ?> _webImagePickImpl (OnImagePickCallback onImagePickCallback) async {
311- final result = await FilePicker .platform.pickFiles ();
312- if (result == null ) {
307+ // Lets the user pick one file; files with any file extension can be selected
308+ final result = await ImageFilePicker ().pickImage ();
309+
310+ // The result will be null, if the user aborted the dialog
311+ if (result == null || result.files.isEmpty) {
312+ return null ;
313+ }
314+
315+ // Read file as bytes (https://github.com/miguelpruivo/flutter_file_picker/wiki/FAQ#q-how-do-i-access-the-path-on-web)
316+ final platformFile = result.files.first;
317+ final bytes = platformFile.bytes;
318+
319+ if (bytes == null ) {
313320 return null ;
314321 }
315322
316- // Take first, because we don't allow picking multiple files.
317- final fileName = result.files.first.name;
318- final file = File (fileName);
323+ final file = File .fromRawPath (bytes);
319324
320325 return onImagePickCallback (file);
321326 }
322327}
328+
329+ // coverage:ignore-start
330+ /// Image file picker wrapper class
331+ class ImageFilePicker {
332+ Future <FilePickerResult ?> pickImage () => FilePicker .platform.pickFiles (type: FileType .image);
333+ }
334+ // coverage:ignore-end
0 commit comments