change font size map value to string

pull/817/head
li3317 3 years ago
parent aec7fdaee5
commit 2b0bf1110c
  1. 2
      README.md
  2. 20
      lib/src/utils/font.dart
  3. 37
      lib/src/widgets/toolbar.dart
  4. 21
      lib/src/widgets/toolbar/quill_dropdown_button.dart

@ -88,7 +88,7 @@ The `QuillToolbar` class lets you customise which formatting options are availab
### Font Size ### Font Size
Within the editor toolbar, a drop-down with font-sizing capabilties is available. This can be enabled or disabled with `showFontSize`. 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<String, String>` 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<String, String>` 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'} fontSizeValues: const {'Small': '8', 'Medium': '24', 'Large': '46'}
``` ```

@ -1,5 +1,9 @@
double getFontSize(dynamic sizeValue) { dynamic getFontSize(dynamic sizeValue) {
if (sizeValue.value is double) { if (sizeValue is String && ['small', 'large', 'huge'].contains(sizeValue)) {
return sizeValue;
}
if (sizeValue is double) {
return sizeValue; return sizeValue;
} }
@ -7,12 +11,10 @@ double getFontSize(dynamic sizeValue) {
return sizeValue.toDouble(); return sizeValue.toDouble();
} }
double? fontSize; assert(sizeValue is String);
if (sizeValue is String) { final fontSize = double.tryParse(sizeValue);
fontSize = double.tryParse(sizeValue); if (fontSize == null) {
if (fontSize == null) { throw 'Invalid size $sizeValue';
throw 'Invalid size $sizeValue';
}
} }
return fontSize!; return fontSize;
} }

@ -7,6 +7,7 @@ import '../models/documents/attribute.dart';
import '../models/themes/quill_custom_icon.dart'; import '../models/themes/quill_custom_icon.dart';
import '../models/themes/quill_dialog_theme.dart'; import '../models/themes/quill_dialog_theme.dart';
import '../models/themes/quill_icon_theme.dart'; import '../models/themes/quill_icon_theme.dart';
import '../utils/font.dart';
import 'controller.dart'; import 'controller.dart';
import 'toolbar/arrow_indicated_button_list.dart'; import 'toolbar/arrow_indicated_button_list.dart';
import 'toolbar/camera_button.dart'; import 'toolbar/camera_button.dart';
@ -114,8 +115,8 @@ class QuillToolbar extends StatelessWidget implements PreferredSizeWidget {
WebVideoPickImpl? webVideoPickImpl, WebVideoPickImpl? webVideoPickImpl,
List<QuillCustomIcon> customIcons = const [], List<QuillCustomIcon> customIcons = const [],
///Map of font sizes in [int] ///Map of font sizes in string
Map<String, int>? fontSizeValues, Map<String, String>? fontSizeValues,
int? initialFontSizeValue, int? initialFontSizeValue,
///The theme to use for the icons in the toolbar, uses type [QuillIconTheme] ///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 //default font size values
final fontSizes = fontSizeValues ?? final fontSizes =
{ fontSizeValues ?? {'Small': 'small', 'Large': 'large', 'Huge': 'huge'};
'Default': 0,
'10': 10,
'12': 12,
'14': 14,
'16': 16,
'18': 18,
'20': 20,
'24': 24,
'28': 28,
'32': 32,
'48': 48
};
return QuillToolbar( return QuillToolbar(
key: key, key: key,
@ -202,24 +191,20 @@ class QuillToolbar extends StatelessWidget implements PreferredSizeWidget {
attribute: Attribute.size, attribute: Attribute.size,
controller: controller, controller: controller,
items: [ items: [
for (MapEntry<String, int> fontSize in fontSizes.entries) for (MapEntry<String, String> fontSize in fontSizes.entries)
PopupMenuItem<int>( PopupMenuItem<String>(
key: ValueKey(fontSize.key), key: ValueKey(fontSize.key),
value: fontSize.value, value: fontSize.value,
child: Text(fontSize.key.toString()), child: Text(fontSize.key.toString()),
), ),
], ],
onSelected: (newSize) { onSelected: (newSize) {
if ((newSize != null) && (newSize as int > 0)) { if (newSize != null) {
controller controller.formatSelection(
.formatSelection(Attribute.fromKeyValue('size', newSize)); Attribute.fromKeyValue('size', getFontSize(newSize)));
}
if (newSize as int == 0) {
controller
.formatSelection(Attribute.fromKeyValue('size', null));
} }
}, },
rawitemsmap: fontSizes, rawItemsMap: fontSizes,
initialValue: (initialFontSizeValue != null) && initialValue: (initialFontSizeValue != null) &&
(initialFontSizeValue <= fontSizes.length - 1) (initialFontSizeValue <= fontSizes.length - 1)
? initialFontSizeValue ? initialFontSizeValue

@ -9,7 +9,7 @@ class QuillDropdownButton<T> extends StatefulWidget {
const QuillDropdownButton({ const QuillDropdownButton({
required this.initialValue, required this.initialValue,
required this.items, required this.items,
required this.rawitemsmap, required this.rawItemsMap,
required this.attribute, required this.attribute,
required this.controller, required this.controller,
required this.onSelected, required this.onSelected,
@ -27,7 +27,7 @@ class QuillDropdownButton<T> extends StatefulWidget {
final double highlightElevation; final double highlightElevation;
final T initialValue; final T initialValue;
final List<PopupMenuEntry<T>> items; final List<PopupMenuEntry<T>> items;
final Map<String, int> rawitemsmap; final Map<String, String> rawItemsMap;
final ValueChanged<T> onSelected; final ValueChanged<T> onSelected;
final QuillIconTheme? iconTheme; final QuillIconTheme? iconTheme;
final Attribute attribute; final Attribute attribute;
@ -47,7 +47,7 @@ class _QuillDropdownButtonState<T> extends State<QuillDropdownButton<T>> {
super.initState(); super.initState();
widget.controller.addListener(_didChangeEditingValue); widget.controller.addListener(_didChangeEditingValue);
_currentValue = _currentValue =
widget.rawitemsmap.keys.elementAt(widget.initialValue as int); widget.rawItemsMap.keys.elementAt(widget.initialValue as int);
} }
@override @override
@ -75,17 +75,17 @@ class _QuillDropdownButtonState<T> extends State<QuillDropdownButton<T>> {
final attribute = attrs[widget.attribute.key]; final attribute = attrs[widget.attribute.key];
if (attribute == null) { if (attribute == null) {
return widget.rawitemsmap.keys return widget.rawItemsMap.keys
.elementAt(widget.initialValue as int) .elementAt(widget.initialValue as int)
.toString(); .toString();
} else { } else {
return widget.rawitemsmap.entries return widget.rawItemsMap.entries
.firstWhere((element) => element.value == attribute.value, .firstWhere((element) => element.value == attribute.value,
orElse: () => widget.rawitemsmap.entries.first) orElse: () => widget.rawItemsMap.entries.first)
.key; .key;
} }
} }
return widget.rawitemsmap.keys return widget.rawItemsMap.keys
.elementAt(widget.initialValue as int) .elementAt(widget.initialValue as int)
.toString(); .toString();
} }
@ -126,7 +126,8 @@ class _QuillDropdownButtonState<T> extends State<QuillDropdownButton<T>> {
context: context, context: context,
elevation: 4, elevation: 4,
// widget.elevation ?? popupMenuTheme.elevation, // widget.elevation ?? popupMenuTheme.elevation,
initialValue: widget.initialValue, initialValue:
widget.rawItemsMap.values.elementAt(widget.initialValue as int) as T,
items: widget.items, items: widget.items,
position: position, position: position,
shape: popupMenuTheme.shape, shape: popupMenuTheme.shape,
@ -140,9 +141,9 @@ class _QuillDropdownButtonState<T> extends State<QuillDropdownButton<T>> {
return null; return null;
} }
setState(() { setState(() {
_currentValue = widget.rawitemsmap.entries _currentValue = widget.rawItemsMap.entries
.firstWhere((element) => element.value == newValue, .firstWhere((element) => element.value == newValue,
orElse: () => widget.rawitemsmap.entries.first) orElse: () => widget.rawItemsMap.entries.first)
.key; .key;
widget.onSelected(newValue); widget.onSelected(newValue);
}); });

Loading…
Cancel
Save