@@ -13,6 +13,7 @@ import 'package:flutter_quill/flutter_quill.dart' hide Text;
13
13
import 'package:flutter_quill_extensions/flutter_quill_extensions.dart' ;
14
14
import 'package:path/path.dart' ;
15
15
import 'package:path_provider/path_provider.dart' ;
16
+ import 'package:mime/mime.dart' ;
16
17
17
18
import 'web_embeds.dart' ;
18
19
@@ -45,9 +46,9 @@ class _HomePageState extends State<HomePage> {
45
46
46
47
Future <void > _initializeText () async {
47
48
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
+ });
51
52
}
52
53
53
54
@override
@@ -204,11 +205,12 @@ class _HomePageState extends State<HomePage> {
204
205
showFormulaButton: false ,
205
206
showVideoButton: false ,
206
207
showImageButton: true ,
207
-
208
+
208
209
// provide a callback to enable picking images from device.
209
210
// if omit, "image" button only allows adding images from url.
210
211
// same goes for videos.
211
212
onImagePickCallback: _onImagePickCallback,
213
+ webImagePickImpl: _webImagePickImpl,
212
214
mediaPickSettingSelector: (context) {
213
215
return Future .value (MediaPickSetting .Gallery );
214
216
},
@@ -217,7 +219,7 @@ class _HomePageState extends State<HomePage> {
217
219
// uncomment to provide a custom "pick from" dialog.
218
220
// cameraPickSettingSelector: _selectCameraPickSetting,
219
221
);
220
- var toolbar = QuillToolbar (
222
+ final toolbar = QuillToolbar (
221
223
afterButtonPressed: _focusNode.requestFocus,
222
224
children: [
223
225
HistoryButton (
@@ -256,23 +258,10 @@ class _HomePageState extends State<HomePage> {
256
258
iconSize: toolbarIconSize,
257
259
controller: _controller! ,
258
260
),
259
-
260
261
for (final builder in embedButtons) builder (_controller! , toolbarIconSize, null , null ),
261
262
],
262
263
);
263
264
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
-
276
265
return SafeArea (
277
266
child: Column (
278
267
mainAxisAlignment: MainAxisAlignment .spaceBetween,
@@ -301,22 +290,45 @@ class _HomePageState extends State<HomePage> {
301
290
// You can also upload the picked image to any server (eg : AWS s3
302
291
// or Firebase) and then return the uploaded image URL.
303
292
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
+ }
308
304
}
309
305
310
306
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 ) {
313
320
return null ;
314
321
}
315
322
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);
319
324
320
325
return onImagePickCallback (file);
321
326
}
322
327
}
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