Skip to content

Commit 2b0bf11

Browse files
committed
change font size map value to string
1 parent aec7fda commit 2b0bf11

File tree

4 files changed

+34
-46
lines changed

4 files changed

+34
-46
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ The `QuillToolbar` class lets you customise which formatting options are availab
8888
### Font Size
8989
Within the editor toolbar, a drop-down with font-sizing capabilties is available. This can be enabled or disabled with `showFontSize`.
9090

91-
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:
91+
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:
9292
```
9393
fontSizeValues: const {'Small': '8', 'Medium': '24', 'Large': '46'}
9494
```

lib/src/utils/font.dart

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
double getFontSize(dynamic sizeValue) {
2-
if (sizeValue.value is double) {
1+
dynamic getFontSize(dynamic sizeValue) {
2+
if (sizeValue is String && ['small', 'large', 'huge'].contains(sizeValue)) {
3+
return sizeValue;
4+
}
5+
6+
if (sizeValue is double) {
37
return sizeValue;
48
}
59

610
if (sizeValue is int) {
711
return sizeValue.toDouble();
812
}
913

10-
double? fontSize;
11-
if (sizeValue is String) {
12-
fontSize = double.tryParse(sizeValue);
13-
if (fontSize == null) {
14-
throw 'Invalid size $sizeValue';
15-
}
14+
assert(sizeValue is String);
15+
final fontSize = double.tryParse(sizeValue);
16+
if (fontSize == null) {
17+
throw 'Invalid size $sizeValue';
1618
}
17-
return fontSize!;
19+
return fontSize;
1820
}

lib/src/widgets/toolbar.dart

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import '../models/documents/attribute.dart';
77
import '../models/themes/quill_custom_icon.dart';
88
import '../models/themes/quill_dialog_theme.dart';
99
import '../models/themes/quill_icon_theme.dart';
10+
import '../utils/font.dart';
1011
import 'controller.dart';
1112
import 'toolbar/arrow_indicated_button_list.dart';
1213
import 'toolbar/camera_button.dart';
@@ -114,8 +115,8 @@ class QuillToolbar extends StatelessWidget implements PreferredSizeWidget {
114115
WebVideoPickImpl? webVideoPickImpl,
115116
List<QuillCustomIcon> customIcons = const [],
116117

117-
///Map of font sizes in [int]
118-
Map<String, int>? fontSizeValues,
118+
///Map of font sizes in string
119+
Map<String, String>? fontSizeValues,
119120
int? initialFontSizeValue,
120121

121122
///The theme to use for the icons in the toolbar, uses type [QuillIconTheme]
@@ -155,20 +156,8 @@ class QuillToolbar extends StatelessWidget implements PreferredSizeWidget {
155156
];
156157

157158
//default font size values
158-
final fontSizes = fontSizeValues ??
159-
{
160-
'Default': 0,
161-
'10': 10,
162-
'12': 12,
163-
'14': 14,
164-
'16': 16,
165-
'18': 18,
166-
'20': 20,
167-
'24': 24,
168-
'28': 28,
169-
'32': 32,
170-
'48': 48
171-
};
159+
final fontSizes =
160+
fontSizeValues ?? {'Small': 'small', 'Large': 'large', 'Huge': 'huge'};
172161

173162
return QuillToolbar(
174163
key: key,
@@ -202,24 +191,20 @@ class QuillToolbar extends StatelessWidget implements PreferredSizeWidget {
202191
attribute: Attribute.size,
203192
controller: controller,
204193
items: [
205-
for (MapEntry<String, int> fontSize in fontSizes.entries)
206-
PopupMenuItem<int>(
194+
for (MapEntry<String, String> fontSize in fontSizes.entries)
195+
PopupMenuItem<String>(
207196
key: ValueKey(fontSize.key),
208197
value: fontSize.value,
209198
child: Text(fontSize.key.toString()),
210199
),
211200
],
212201
onSelected: (newSize) {
213-
if ((newSize != null) && (newSize as int > 0)) {
214-
controller
215-
.formatSelection(Attribute.fromKeyValue('size', newSize));
216-
}
217-
if (newSize as int == 0) {
218-
controller
219-
.formatSelection(Attribute.fromKeyValue('size', null));
202+
if (newSize != null) {
203+
controller.formatSelection(
204+
Attribute.fromKeyValue('size', getFontSize(newSize)));
220205
}
221206
},
222-
rawitemsmap: fontSizes,
207+
rawItemsMap: fontSizes,
223208
initialValue: (initialFontSizeValue != null) &&
224209
(initialFontSizeValue <= fontSizes.length - 1)
225210
? initialFontSizeValue

lib/src/widgets/toolbar/quill_dropdown_button.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class QuillDropdownButton<T> extends StatefulWidget {
99
const QuillDropdownButton({
1010
required this.initialValue,
1111
required this.items,
12-
required this.rawitemsmap,
12+
required this.rawItemsMap,
1313
required this.attribute,
1414
required this.controller,
1515
required this.onSelected,
@@ -27,7 +27,7 @@ class QuillDropdownButton<T> extends StatefulWidget {
2727
final double highlightElevation;
2828
final T initialValue;
2929
final List<PopupMenuEntry<T>> items;
30-
final Map<String, int> rawitemsmap;
30+
final Map<String, String> rawItemsMap;
3131
final ValueChanged<T> onSelected;
3232
final QuillIconTheme? iconTheme;
3333
final Attribute attribute;
@@ -47,7 +47,7 @@ class _QuillDropdownButtonState<T> extends State<QuillDropdownButton<T>> {
4747
super.initState();
4848
widget.controller.addListener(_didChangeEditingValue);
4949
_currentValue =
50-
widget.rawitemsmap.keys.elementAt(widget.initialValue as int);
50+
widget.rawItemsMap.keys.elementAt(widget.initialValue as int);
5151
}
5252

5353
@override
@@ -75,17 +75,17 @@ class _QuillDropdownButtonState<T> extends State<QuillDropdownButton<T>> {
7575
final attribute = attrs[widget.attribute.key];
7676

7777
if (attribute == null) {
78-
return widget.rawitemsmap.keys
78+
return widget.rawItemsMap.keys
7979
.elementAt(widget.initialValue as int)
8080
.toString();
8181
} else {
82-
return widget.rawitemsmap.entries
82+
return widget.rawItemsMap.entries
8383
.firstWhere((element) => element.value == attribute.value,
84-
orElse: () => widget.rawitemsmap.entries.first)
84+
orElse: () => widget.rawItemsMap.entries.first)
8585
.key;
8686
}
8787
}
88-
return widget.rawitemsmap.keys
88+
return widget.rawItemsMap.keys
8989
.elementAt(widget.initialValue as int)
9090
.toString();
9191
}
@@ -126,7 +126,8 @@ class _QuillDropdownButtonState<T> extends State<QuillDropdownButton<T>> {
126126
context: context,
127127
elevation: 4,
128128
// widget.elevation ?? popupMenuTheme.elevation,
129-
initialValue: widget.initialValue,
129+
initialValue:
130+
widget.rawItemsMap.values.elementAt(widget.initialValue as int) as T,
130131
items: widget.items,
131132
position: position,
132133
shape: popupMenuTheme.shape,
@@ -140,9 +141,9 @@ class _QuillDropdownButtonState<T> extends State<QuillDropdownButton<T>> {
140141
return null;
141142
}
142143
setState(() {
143-
_currentValue = widget.rawitemsmap.entries
144+
_currentValue = widget.rawItemsMap.entries
144145
.firstWhere((element) => element.value == newValue,
145-
orElse: () => widget.rawitemsmap.entries.first)
146+
orElse: () => widget.rawItemsMap.entries.first)
146147
.key;
147148
widget.onSelected(newValue);
148149
});

0 commit comments

Comments
 (0)