Fix FontFamily and Size button actions

pull/1829/head
AtlasAutocode 1 year ago
parent ae4c82896e
commit b0d92d1d12
  1. 10
      example/lib/screens/quill/my_quill_toolbar.dart
  2. 50
      lib/src/widgets/toolbar/buttons/font_family_button.dart
  3. 33
      lib/src/widgets/toolbar/buttons/font_size_button.dart

@ -223,9 +223,9 @@ class MyQuillToolbar extends StatelessWidget {
'40': '40.0'
},
// headerStyleType: HeaderStyleType.buttons,
// buttonOptions: QuillSimpleToolbarButtonOptions(
// base: QuillToolbarBaseButtonOptions(
// afterButtonPressed: focusNode.requestFocus,
buttonOptions: QuillSimpleToolbarButtonOptions(
base: QuillToolbarBaseButtonOptions(
afterButtonPressed: focusNode.requestFocus,
// // iconSize: 20,
// iconTheme: QuillIconTheme(
// iconButtonSelectedData: IconButtonData(
@ -239,8 +239,8 @@ class MyQuillToolbar extends StatelessWidget {
// ),
// ),
// ),
// ),
// ),
),
),
customButtons: [
QuillToolbarCustomButtonOptions(
icon: const Icon(Icons.add_alarm_rounded),

@ -45,6 +45,8 @@ class QuillToolbarFontFamilyButtonState
void initState() {
super.initState();
_initState();
controller
.addListener(_didChangeEditingValue);
}
void _initState() {}
@ -62,26 +64,32 @@ class QuillToolbarFontFamilyButtonState
context.loc.font;
}
// @override
// void didUpdateWidget(covariant QuillToolbarFontFamilyButton oldWidget) {
// super.didUpdateWidget(oldWidget);
// if (oldWidget.controller == controller) {
// return;
// }
// controller
// ..removeListener(_didChangeEditingValue)
// ..addListener(_didChangeEditingValue);
// }
@override
void didUpdateWidget(covariant QuillToolbarFontFamilyButton oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.controller == controller) {
return;
}
controller
..removeListener(_didChangeEditingValue)
..addListener(_didChangeEditingValue);
}
// void _didChangeEditingValue() {
// final attribute = _selectionStyle.attributes[options.attribute.key];
// if (attribute == null) {
// setState(() => _currentValue = _defaultDisplayText);
// return;
// }
// final keyName = _getKeyName(attribute.value);
// setState(() => _currentValue = keyName ?? _defaultDisplayText);
// }
@override
void dispose() {
controller.removeListener(_didChangeEditingValue);
super.dispose();
}
void _didChangeEditingValue() {
final attribute = controller.getSelectionStyle().attributes[options.attribute.key];
if (attribute == null) {
setState(() => _currentValue = _defaultDisplayText);
return;
}
final keyName = _getKeyName(attribute.value);
setState(() => _currentValue = keyName ?? _defaultDisplayText);
}
Map<String, String> get rawItemsMap {
final rawItemsMap =
@ -142,7 +150,7 @@ class QuillToolbarFontFamilyButtonState
} else {
_menuController.open();
}
options.afterButtonPressed?.call();
afterButtonPressed?.call();
}
final _menuController = MenuController();
@ -262,7 +270,7 @@ class QuillToolbarFontFamilyButtonState
enabled: hasFinalWidth,
wrapper: (child) => Expanded(child: child),
child: Text(
widget.controller.selectedFontFamily?.key ?? _currentValue,
_currentValue,
maxLines: 1,
overflow: options.labelOverflow,
style: options.style ??

@ -72,17 +72,49 @@ class QuillToolbarFontSizeButtonState
context.loc.fontSize;
}
@override
void initState() {
super.initState();
_initState();
controller
.addListener(_didChangeEditingValue);
}
void _initState() {}
@override
void didChangeDependencies() {
super.didChangeDependencies();
_currentValue = _defaultDisplayText;
}
@override
void didUpdateWidget(covariant QuillToolbarFontSizeButton oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.controller == controller) {
return;
}
controller
..removeListener(_didChangeEditingValue)
..addListener(_didChangeEditingValue);
}
@override
void dispose() {
controller.removeListener(_didChangeEditingValue);
super.dispose();
}
void _didChangeEditingValue() {
final attribute = controller.getSelectionStyle().attributes[options.attribute.key];
if (attribute == null) {
setState(() => _currentValue = _defaultDisplayText);
return;
}
final keyName = _getKeyName(attribute.value);
setState(() => _currentValue = keyName ?? _defaultDisplayText);
}
String? _getKeyName(dynamic value) {
for (final entry in rawItemsMap.entries) {
if (getFontSize(entry.value) == getFontSize(value)) {
@ -231,7 +263,6 @@ class QuillToolbarFontSizeButtonState
enabled: hasFinalWidth,
wrapper: (child) => Expanded(child: child),
child: Text(
getLabel(widget.controller.selectedFontSize?.key) ??
getLabel(_currentValue) ??
'',
overflow: options.labelOverflow,

Loading…
Cancel
Save