diff --git a/README.md b/README.md index 4693fed1..c2f38695 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ The `QuillToolbar` class lets you customise which formatting options are availab ### Font Size Within the editor toolbar, a drop-down with font-sizing capabilties is available. This can be enabled or disabled with `showFontSize`. -When enabled, the default font-size values can be modified via _optional_ `fontSizeValues`. `fontSizeValues` accepts a `Map` consisting of a `String` title for the font size and an `int` value for the font size. Example: +When enabled, the default font-size values can be modified via _optional_ `fontSizeValues`. `fontSizeValues` accepts a `Map` consisting of a `String` title for the font size and an `String` value for the font size. Example: ``` fontSizeValues: const {'Small': '8', 'Medium': '24', 'Large': '46'} ``` diff --git a/lib/src/utils/font.dart b/lib/src/utils/font.dart index cf69a676..4962a9a2 100644 --- a/lib/src/utils/font.dart +++ b/lib/src/utils/font.dart @@ -1,5 +1,9 @@ -double getFontSize(dynamic sizeValue) { - if (sizeValue.value is double) { +dynamic getFontSize(dynamic sizeValue) { + if (sizeValue is String && ['small', 'large', 'huge'].contains(sizeValue)) { + return sizeValue; + } + + if (sizeValue is double) { return sizeValue; } @@ -7,12 +11,10 @@ double getFontSize(dynamic sizeValue) { return sizeValue.toDouble(); } - double? fontSize; - if (sizeValue is String) { - fontSize = double.tryParse(sizeValue); - if (fontSize == null) { - throw 'Invalid size $sizeValue'; - } + assert(sizeValue is String); + final fontSize = double.tryParse(sizeValue); + if (fontSize == null) { + throw 'Invalid size $sizeValue'; } - return fontSize!; + return fontSize; } diff --git a/lib/src/widgets/toolbar.dart b/lib/src/widgets/toolbar.dart index 70883572..0fd561f9 100644 --- a/lib/src/widgets/toolbar.dart +++ b/lib/src/widgets/toolbar.dart @@ -7,6 +7,7 @@ import '../models/documents/attribute.dart'; import '../models/themes/quill_custom_icon.dart'; import '../models/themes/quill_dialog_theme.dart'; import '../models/themes/quill_icon_theme.dart'; +import '../utils/font.dart'; import 'controller.dart'; import 'toolbar/arrow_indicated_button_list.dart'; import 'toolbar/camera_button.dart'; @@ -114,8 +115,8 @@ class QuillToolbar extends StatelessWidget implements PreferredSizeWidget { WebVideoPickImpl? webVideoPickImpl, List customIcons = const [], - ///Map of font sizes in [int] - Map? fontSizeValues, + ///Map of font sizes in string + Map? fontSizeValues, int? initialFontSizeValue, ///The theme to use for the icons in the toolbar, uses type [QuillIconTheme] @@ -155,20 +156,8 @@ class QuillToolbar extends StatelessWidget implements PreferredSizeWidget { ]; //default font size values - final fontSizes = fontSizeValues ?? - { - 'Default': 0, - '10': 10, - '12': 12, - '14': 14, - '16': 16, - '18': 18, - '20': 20, - '24': 24, - '28': 28, - '32': 32, - '48': 48 - }; + final fontSizes = + fontSizeValues ?? {'Small': 'small', 'Large': 'large', 'Huge': 'huge'}; return QuillToolbar( key: key, @@ -202,24 +191,20 @@ class QuillToolbar extends StatelessWidget implements PreferredSizeWidget { attribute: Attribute.size, controller: controller, items: [ - for (MapEntry fontSize in fontSizes.entries) - PopupMenuItem( + for (MapEntry fontSize in fontSizes.entries) + PopupMenuItem( key: ValueKey(fontSize.key), value: fontSize.value, child: Text(fontSize.key.toString()), ), ], onSelected: (newSize) { - if ((newSize != null) && (newSize as int > 0)) { - controller - .formatSelection(Attribute.fromKeyValue('size', newSize)); - } - if (newSize as int == 0) { - controller - .formatSelection(Attribute.fromKeyValue('size', null)); + if (newSize != null) { + controller.formatSelection( + Attribute.fromKeyValue('size', getFontSize(newSize))); } }, - rawitemsmap: fontSizes, + rawItemsMap: fontSizes, initialValue: (initialFontSizeValue != null) && (initialFontSizeValue <= fontSizes.length - 1) ? initialFontSizeValue diff --git a/lib/src/widgets/toolbar/quill_dropdown_button.dart b/lib/src/widgets/toolbar/quill_dropdown_button.dart index 0b4671b0..3c480396 100644 --- a/lib/src/widgets/toolbar/quill_dropdown_button.dart +++ b/lib/src/widgets/toolbar/quill_dropdown_button.dart @@ -9,7 +9,7 @@ class QuillDropdownButton extends StatefulWidget { const QuillDropdownButton({ required this.initialValue, required this.items, - required this.rawitemsmap, + required this.rawItemsMap, required this.attribute, required this.controller, required this.onSelected, @@ -27,7 +27,7 @@ class QuillDropdownButton extends StatefulWidget { final double highlightElevation; final T initialValue; final List> items; - final Map rawitemsmap; + final Map rawItemsMap; final ValueChanged onSelected; final QuillIconTheme? iconTheme; final Attribute attribute; @@ -47,7 +47,7 @@ class _QuillDropdownButtonState extends State> { super.initState(); widget.controller.addListener(_didChangeEditingValue); _currentValue = - widget.rawitemsmap.keys.elementAt(widget.initialValue as int); + widget.rawItemsMap.keys.elementAt(widget.initialValue as int); } @override @@ -75,17 +75,17 @@ class _QuillDropdownButtonState extends State> { final attribute = attrs[widget.attribute.key]; if (attribute == null) { - return widget.rawitemsmap.keys + return widget.rawItemsMap.keys .elementAt(widget.initialValue as int) .toString(); } else { - return widget.rawitemsmap.entries + return widget.rawItemsMap.entries .firstWhere((element) => element.value == attribute.value, - orElse: () => widget.rawitemsmap.entries.first) + orElse: () => widget.rawItemsMap.entries.first) .key; } } - return widget.rawitemsmap.keys + return widget.rawItemsMap.keys .elementAt(widget.initialValue as int) .toString(); } @@ -126,7 +126,8 @@ class _QuillDropdownButtonState extends State> { context: context, elevation: 4, // widget.elevation ?? popupMenuTheme.elevation, - initialValue: widget.initialValue, + initialValue: + widget.rawItemsMap.values.elementAt(widget.initialValue as int) as T, items: widget.items, position: position, shape: popupMenuTheme.shape, @@ -140,9 +141,9 @@ class _QuillDropdownButtonState extends State> { return null; } setState(() { - _currentValue = widget.rawitemsmap.entries + _currentValue = widget.rawItemsMap.entries .firstWhere((element) => element.value == newValue, - orElse: () => widget.rawitemsmap.entries.first) + orElse: () => widget.rawItemsMap.entries.first) .key; widget.onSelected(newValue); });