Convert FontSize to a Map to allow for named Font Size (#794)

pull/796/head
mark8044 3 years ago committed by GitHub
parent 62bc09d19a
commit b30fee9ae4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      lib/src/widgets/toolbar.dart
  2. 22
      lib/src/widgets/toolbar/quill_dropdown_button.dart

@ -110,8 +110,8 @@ class QuillToolbar extends StatelessWidget implements PreferredSizeWidget {
WebImagePickImpl? webImagePickImpl, WebImagePickImpl? webImagePickImpl,
WebVideoPickImpl? webVideoPickImpl, WebVideoPickImpl? webVideoPickImpl,
///List of font sizes in [int] ///Map of font sizes in [int]
List? fontSizeValues, Map<String, int>? 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]
@ -152,7 +152,7 @@ class QuillToolbar extends StatelessWidget implements PreferredSizeWidget {
//default font size values //default font size values
final fontSizes = final fontSizes =
fontSizeValues ?? [10, 12, 14, 16, 18, 20, 24, 28, 32, 48]; fontSizeValues ?? {'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,
@ -181,12 +181,13 @@ class QuillToolbar extends StatelessWidget implements PreferredSizeWidget {
if (showFontSize) if (showFontSize)
QuillDropdownButton( QuillDropdownButton(
iconTheme: iconTheme, iconTheme: iconTheme,
height: toolbarIconSize * 2, height: (toolbarIconSize * 2)-(toolbarIconSize / 3),
items: [ items: [
for (var fontSize in fontSizes) for(MapEntry<String, int> fontSize in fontSizes.entries)
PopupMenuItem<int>( PopupMenuItem<int>(
value: fontSize, key: ValueKey(fontSize.key),
child: Text(fontSize.toString()), value: fontSize.value,
child: Text(fontSize.key.toString()),
), ),
], ],
onSelected: (newSize) { onSelected: (newSize) {
@ -195,7 +196,8 @@ class QuillToolbar extends StatelessWidget implements PreferredSizeWidget {
.formatSelection(Attribute.fromKeyValue('size', newSize)); .formatSelection(Attribute.fromKeyValue('size', newSize));
} }
}, },
initialValue: fontSizes[initialFontSizeValue ?? 0], rawitemsmap: fontSizes,
initialValue: (initialFontSizeValue != null) && (initialFontSizeValue <= fontSizes.length - 1) ? initialFontSizeValue : 0,
), ),
if (showBoldButton) if (showBoldButton)
ToggleStyleButton( ToggleStyleButton(

@ -5,6 +5,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.onSelected, required this.onSelected,
this.height = 40, this.height = 40,
this.fillColor, this.fillColor,
@ -20,6 +21,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 ValueChanged<T> onSelected; final ValueChanged<T> onSelected;
final QuillIconTheme? iconTheme; final QuillIconTheme? iconTheme;
@ -29,12 +31,12 @@ class QuillDropdownButton<T> extends StatefulWidget {
// ignore: deprecated_member_use_from_same_package // ignore: deprecated_member_use_from_same_package
class _QuillDropdownButtonState<T> extends State<QuillDropdownButton<T>> { class _QuillDropdownButtonState<T> extends State<QuillDropdownButton<T>> {
int _currentValue = 0; String _currentValue = '';
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_currentValue = widget.initialValue as int; _currentValue = widget.rawitemsmap.keys.elementAt(widget.initialValue as int);
} }
@override @override
@ -87,25 +89,23 @@ class _QuillDropdownButtonState<T> extends State<QuillDropdownButton<T>> {
return null; return null;
} }
setState(() { setState(() {
_currentValue = newValue as int; _currentValue = widget.rawitemsmap.entries.firstWhere((element) => element.value==newValue, orElse: () => widget.rawitemsmap.entries.first).key;
widget.onSelected(newValue); widget.onSelected(newValue);
}); });
}); });
} }
Widget _buildContent(BuildContext context) { Widget _buildContent(BuildContext context) {
return ConstrainedBox( return Padding(
constraints: const BoxConstraints.tightFor(width: 60), padding: const EdgeInsets.symmetric(horizontal: 10),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Row( child: Row(
mainAxisSize: MainAxisSize.min,
children: [ children: [
Text(_currentValue.toString()), Text(_currentValue.toString()),
Expanded(child: Container()), SizedBox(width:6),
const Icon(Icons.arrow_drop_down, size: 15) const Icon(Icons.arrow_drop_down, size: 17)
], ],
), ),
), );
);
} }
} }

Loading…
Cancel
Save