@@ -18,7 +18,7 @@ double iconSize = 18;
18
18
double kToolbarHeight = iconSize * 2 ;
19
19
20
20
typedef OnImagePickCallback = Future <String > Function (File file);
21
- typedef ImagePickImpl = Future <String > Function (ImageSource source);
21
+ typedef ImagePickImpl = Future <String ? > Function (ImageSource source);
22
22
23
23
class InsertEmbedButton extends StatelessWidget {
24
24
const InsertEmbedButton ({
@@ -252,7 +252,8 @@ class _ToggleStyleButtonState extends State<ToggleStyleButton> {
252
252
_selectionStyle.attributes.containsKey (Attribute .codeBlock.key);
253
253
final isEnabled =
254
254
! isInCodeBlock || widget.attribute.key == Attribute .codeBlock.key;
255
- return widget.childBuilder (context, widget.attribute, widget.icon, widget.fillColor, _isToggled, isEnabled ? _toggleAttribute : null );
255
+ return widget.childBuilder (context, widget.attribute, widget.icon,
256
+ widget.fillColor, _isToggled, isEnabled ? _toggleAttribute : null );
256
257
}
257
258
258
259
void _toggleAttribute () {
@@ -339,7 +340,8 @@ class _ToggleCheckListButtonState extends State<ToggleCheckListButton> {
339
340
_selectionStyle.attributes.containsKey (Attribute .codeBlock.key);
340
341
final isEnabled =
341
342
! isInCodeBlock || Attribute .list.key == Attribute .codeBlock.key;
342
- return widget.childBuilder (context, Attribute .unchecked, widget.icon, widget.fillColor, _isToggled, isEnabled ? _toggleAttribute : null );
343
+ return widget.childBuilder (context, Attribute .unchecked, widget.icon,
344
+ widget.fillColor, _isToggled, isEnabled ? _toggleAttribute : null );
343
345
}
344
346
345
347
void _toggleAttribute () {
@@ -364,8 +366,9 @@ Widget defaultToggleStyleButtonBuilder(
364
366
? theme.primaryIconTheme.color
365
367
: theme.iconTheme.color
366
368
: theme.disabledColor;
367
- final fill =
368
- isToggled == true ? theme.toggleableActiveColor : fillColor ?? theme.canvasColor;
369
+ final fill = isToggled == true
370
+ ? theme.toggleableActiveColor
371
+ : fillColor ?? theme.canvasColor;
369
372
return QuillIconButton (
370
373
highlightElevation: 0 ,
371
374
hoverElevation: 0 ,
@@ -522,87 +525,81 @@ class ImageButton extends StatefulWidget {
522
525
}
523
526
524
527
class _ImageButtonState extends State <ImageButton > {
525
- List <PlatformFile >? _paths;
526
- String ? _extension;
527
- final _picker = ImagePicker ();
528
- final FileType _pickingType = FileType .any;
528
+ @override
529
+ Widget build (BuildContext context) {
530
+ final theme = Theme .of (context);
529
531
530
- Future <String ?> _pickImage (ImageSource source) async {
531
- final pickedFile = await _picker.getImage (source: source);
532
- if (pickedFile == null ) return null ;
532
+ return QuillIconButton (
533
+ icon: Icon (
534
+ widget.icon,
535
+ size: iconSize,
536
+ color: theme.iconTheme.color,
537
+ ),
538
+ highlightElevation: 0 ,
539
+ hoverElevation: 0 ,
540
+ size: iconSize * 1.77 ,
541
+ fillColor: theme.canvasColor,
542
+ onPressed: _handleImageButtonTap,
543
+ );
544
+ }
533
545
534
- final file = File (pickedFile.path);
546
+ Future <void > _handleImageButtonTap () async {
547
+ final index = widget.controller.selection.baseOffset;
548
+ final length = widget.controller.selection.extentOffset - index;
535
549
536
- return widget.onImagePickCallback !(file);
550
+ String ? imageUrl;
551
+ if (widget.imagePickImpl != null ) {
552
+ imageUrl = await widget.imagePickImpl !(widget.imageSource);
553
+ } else {
554
+ if (kIsWeb) {
555
+ imageUrl = await _pickImageWeb ();
556
+ } else if (Platform .isAndroid || Platform .isIOS) {
557
+ imageUrl = await _pickImage (widget.imageSource);
558
+ } else {
559
+ imageUrl = await _pickImageDesktop ();
560
+ }
561
+ }
562
+
563
+ if (imageUrl != null ) {
564
+ widget.controller
565
+ .replaceText (index, length, BlockEmbed .image (imageUrl), null );
566
+ }
537
567
}
538
568
539
569
Future <String ?> _pickImageWeb () async {
540
- _paths = ( await FilePicker .platform.pickFiles (
541
- type : _pickingType,
542
- allowedExtensions : (_extension ? .isNotEmpty ?? false )
543
- ? _extension ? . replaceAll ( ' ' , '' ). split ( ',' )
544
- : null ,
545
- ))
546
- ? .files;
547
- final _fileName =
548
- _paths != null ? _paths ! . map ((e) => e.name). toString () : '...' ;
549
-
550
- if (_paths != null ) {
551
- final file = File (_fileName);
552
- // We simply return the absolute path to selected file.
553
- return widget. onImagePickCallback !(file );
554
- } else {
555
- // User canceled the picker
570
+ final result = await FilePicker .platform.pickFiles ();
571
+ if (result == null ) {
572
+ return null ;
573
+ }
574
+
575
+ // Take first, because we don't allow picking multiple files.
576
+ final fileName = result .files.first.name ! ;
577
+ final file = File (fileName);
578
+
579
+ return widget. onImagePickCallback !(file);
580
+ }
581
+
582
+ Future < String ?> _pickImage ( ImageSource source) async {
583
+ final pickedFile = await ImagePicker (). getImage (source : source );
584
+ if (pickedFile == null ) {
585
+ return null ;
556
586
}
557
- return null ;
587
+
588
+ return widget.onImagePickCallback !(File (pickedFile.path));
558
589
}
559
590
560
- Future <String > _pickImageDesktop () async {
591
+ Future <String ? > _pickImageDesktop () async {
561
592
final filePath = await FilesystemPicker .open (
562
593
context: context,
563
594
rootDirectory: await getApplicationDocumentsDirectory (),
564
595
fsType: FilesystemType .file,
565
596
fileTileSelectMode: FileTileSelectMode .wholeTile,
566
597
);
567
- if (filePath != null && filePath.isEmpty) return '' ;
598
+ if (filePath == null || filePath.isEmpty) return null ;
568
599
569
- final file = File (filePath! );
600
+ final file = File (filePath);
570
601
return widget.onImagePickCallback !(file);
571
602
}
572
-
573
- @override
574
- Widget build (BuildContext context) {
575
- final theme = Theme .of (context);
576
- final iconColor = theme.iconTheme.color;
577
- final fillColor = theme.canvasColor;
578
- return QuillIconButton (
579
- highlightElevation: 0 ,
580
- hoverElevation: 0 ,
581
- size: iconSize * 1.77 ,
582
- icon: Icon (widget.icon, size: iconSize, color: iconColor),
583
- fillColor: fillColor,
584
- onPressed: () {
585
- final index = widget.controller.selection.baseOffset;
586
- final length = widget.controller.selection.extentOffset - index;
587
- Future <String ?> image;
588
- if (widget.imagePickImpl != null ) {
589
- image = widget.imagePickImpl !(widget.imageSource);
590
- } else {
591
- if (kIsWeb) {
592
- image = _pickImageWeb ();
593
- } else if (Platform .isAndroid || Platform .isIOS) {
594
- image = _pickImage (widget.imageSource);
595
- } else {
596
- image = _pickImageDesktop ();
597
- }
598
- }
599
- image.then ((imageUploadUrl) => {
600
- widget.controller.replaceText (
601
- index, length, BlockEmbed .image (imageUploadUrl! ), null )
602
- });
603
- },
604
- );
605
- }
606
603
}
607
604
608
605
/// Controls color styles.
0 commit comments