diff --git a/example/lib/screens/quill/my_quill_toolbar.dart b/example/lib/screens/quill/my_quill_toolbar.dart index 5acfb2de..7e177956 100644 --- a/example/lib/screens/quill/my_quill_toolbar.dart +++ b/example/lib/screens/quill/my_quill_toolbar.dart @@ -225,7 +225,7 @@ class MyQuillToolbar extends StatelessWidget { // headerStyleType: HeaderStyleType.buttons, // buttonOptions: QuillSimpleToolbarButtonOptions( // base: QuillToolbarBaseButtonOptions( - // afterButtonPressed: focusNode.requestFocus, + // 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), diff --git a/lib/src/widgets/quill/quill_controller.dart b/lib/src/widgets/quill/quill_controller.dart index ebbaa7d6..1a2634e4 100644 --- a/lib/src/widgets/quill/quill_controller.dart +++ b/lib/src/widgets/quill/quill_controller.dart @@ -14,7 +14,6 @@ import '../../models/structs/doc_change.dart'; import '../../models/structs/image_url.dart'; import '../../models/structs/offset_value.dart'; import '../../utils/delta.dart'; -import '../toolbar/buttons/toggle_style_button.dart'; typedef ReplaceTextCallback = bool Function(int index, int len, Object? data); typedef DeleteCallback = void Function(int cursorPosition, bool forward); @@ -67,40 +66,6 @@ class QuillController extends ChangeNotifier { notifyListeners(); } - // Thoses are the values that the user selects and not the one - // from the current line - - /// The current font family, null to use the default one - MapEntry? _selectedFontFamily; - - /// The current font family, null to use the default one - MapEntry? get selectedFontFamily => _selectedFontFamily; - - void selectFontFamily(MapEntry? newFontFamily) { - _selectedFontFamily = newFontFamily; - } - - /// The current font size, null to use the default one - MapEntry? _selectedFontSize; - - /// The current font size, null to use the default one - MapEntry? get selectedFontSize => _selectedFontSize; - - void selectFontSize(MapEntry? newFontSize) { - _selectedFontSize = newFontSize; - } - - /// For the [QuillToolbarToggleStyleButton] - final Map _selectedStyles = {}; - - /// For the [QuillToolbarToggleStyleButton] - Map get selectedStyles => _selectedStyles; - - /// For the [QuillToolbarToggleStyleButton] - void selectStyle(Attribute attribute, bool value) { - _selectedStyles[attribute] = value; - } - /// Tells whether to keep or reset the [toggledStyle] /// when user adds a new line. final bool keepStyleOnNewLine; diff --git a/lib/src/widgets/toolbar/base_button/base_value_button.dart b/lib/src/widgets/toolbar/base_button/base_value_button.dart new file mode 100644 index 00000000..3c2b8046 --- /dev/null +++ b/lib/src/widgets/toolbar/base_button/base_value_button.dart @@ -0,0 +1,98 @@ +import 'package:flutter/material.dart'; + +import '../../../../flutter_quill.dart'; + +/// The [T] is the options for the button +/// The [E] is the extra options for the button +abstract class QuillToolbarBaseValueButton< + T extends QuillToolbarBaseButtonOptions, + E extends QuillToolbarBaseButtonExtraOptions> extends StatefulWidget { + const QuillToolbarBaseValueButton( + {required this.controller, required this.options, super.key}); + + final T options; + + final QuillController controller; +} + +/// The [W] is the widget that creates this State +/// The [V] is the type of the currentValue +abstract class QuillToolbarBaseValueButtonState< + W extends QuillToolbarBaseValueButton, + T extends QuillToolbarBaseButtonOptions, + E extends QuillToolbarBaseButtonExtraOptions, + V> extends State { + T get options => widget.options; + + QuillController get controller => widget.controller; + + late V currentValue; + + /// Callback to query the widget's state for the value to be assigned to currentState + V get currentStateValue; + + @override + void initState() { + super.initState(); + controller.addListener(didChangeEditingValue); + } + + @override + void didChangeDependencies() { + super.didChangeDependencies(); + currentValue = currentStateValue; + } + + void didChangeEditingValue() { + setState(() => currentValue = currentStateValue); + } + + @override + void dispose() { + controller.removeListener(didChangeEditingValue); + super.dispose(); + } + + @override + void didUpdateWidget(covariant W oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.controller != controller) { + oldWidget.controller.removeListener(didChangeEditingValue); + controller.addListener(didChangeEditingValue); + currentValue = currentStateValue; + } + } + + String get defaultTooltip; + + String get tooltip { + return options.tooltip ?? + context.quillToolbarBaseButtonOptions?.tooltip ?? + defaultTooltip; + } + + double get iconSize { + final baseFontSize = baseButtonExtraOptions?.iconSize; + final iconSize = options.iconSize; + return iconSize ?? baseFontSize ?? kDefaultIconSize; + } + + double get iconButtonFactor { + final baseIconFactor = baseButtonExtraOptions?.iconButtonFactor; + final iconButtonFactor = options.iconButtonFactor; + return iconButtonFactor ?? baseIconFactor ?? kDefaultIconButtonFactor; + } + + QuillIconTheme? get iconTheme { + return options.iconTheme ?? baseButtonExtraOptions?.iconTheme; + } + + QuillToolbarBaseButtonOptions? get baseButtonExtraOptions { + return context.quillToolbarBaseButtonOptions; + } + + VoidCallback? get afterButtonPressed { + return options.afterButtonPressed ?? + baseButtonExtraOptions?.afterButtonPressed; + } +} diff --git a/lib/src/widgets/toolbar/buttons/font_family_button.dart b/lib/src/widgets/toolbar/buttons/font_family_button.dart index a8ba667b..10cf8712 100644 --- a/lib/src/widgets/toolbar/buttons/font_family_button.dart +++ b/lib/src/widgets/toolbar/buttons/font_family_button.dart @@ -4,55 +4,43 @@ import '../../../../extensions.dart'; import '../../../extensions/quill_configurations_ext.dart'; import '../../../l10n/extensions/localizations.dart'; import '../../../models/documents/attribute.dart'; -import '../../../models/themes/quill_icon_theme.dart'; -import '../../quill/quill_controller.dart'; +import '../base_button/base_value_button.dart'; import '../base_toolbar.dart'; -class QuillToolbarFontFamilyButton extends StatefulWidget { +class QuillToolbarFontFamilyButton extends QuillToolbarBaseValueButton< + QuillToolbarFontFamilyButtonOptions, + QuillToolbarFontFamilyButtonExtraOptions> { QuillToolbarFontFamilyButton({ - required this.controller, + required super.controller, @Deprecated('Please use the default display text from the options') this.defaultDisplayText, - this.options = const QuillToolbarFontFamilyButtonOptions(), + super.options = const QuillToolbarFontFamilyButtonOptions(), super.key, }) : assert(options.rawItemsMap?.isNotEmpty ?? (true)), assert( options.initialValue == null || options.initialValue!.isNotEmpty, ); - final QuillToolbarFontFamilyButtonOptions options; - final String? defaultDisplayText; - /// Since we can't get the state from the instace of the widget for comparing - /// in [didUpdateWidget] then we will have to store reference here - final QuillController controller; - @override QuillToolbarFontFamilyButtonState createState() => QuillToolbarFontFamilyButtonState(); } class QuillToolbarFontFamilyButtonState - extends State { - var _currentValue = ''; - - QuillToolbarFontFamilyButtonOptions get options { - return widget.options; - } - - @override - void initState() { - super.initState(); - _initState(); - } - - void _initState() {} - + extends QuillToolbarBaseValueButtonState< + QuillToolbarFontFamilyButton, + QuillToolbarFontFamilyButtonOptions, + QuillToolbarFontFamilyButtonExtraOptions, + String> { @override - void didChangeDependencies() { - super.didChangeDependencies(); - _currentValue = _defaultDisplayText; + String get currentStateValue { + final attribute = + controller.getSelectionStyle().attributes[options.attribute.key]; + return attribute == null + ? _defaultDisplayText + : (_getKeyName(attribute.value) ?? _defaultDisplayText); } String get _defaultDisplayText { @@ -62,27 +50,6 @@ class QuillToolbarFontFamilyButtonState context.loc.font; } - // @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); - // } - Map get rawItemsMap { final rawItemsMap = context.quillSimpleToolbarConfigurations?.fontFamilyValues ?? @@ -110,38 +77,8 @@ class QuillToolbarFontFamilyButtonState return null; } - QuillController get controller { - return widget.controller; - } - - double get iconSize { - final baseFontSize = context.quillToolbarBaseButtonOptions?.iconSize; - final iconSize = options.iconSize; - return iconSize ?? baseFontSize ?? kDefaultIconSize; - } - - double get iconButtonFactor { - final baseIconFactor = - context.quillToolbarBaseButtonOptions?.iconButtonFactor; - final iconButtonFactor = widget.options.iconButtonFactor; - return iconButtonFactor ?? baseIconFactor ?? kDefaultIconButtonFactor; - } - - VoidCallback? get afterButtonPressed { - return options.afterButtonPressed ?? - context.quillToolbarBaseButtonOptions?.afterButtonPressed; - } - - QuillIconTheme? get iconTheme { - return options.iconTheme ?? - context.quillToolbarBaseButtonOptions?.iconTheme; - } - - String get tooltip { - return options.tooltip ?? - context.quillToolbarBaseButtonOptions?.tooltip ?? - context.loc.fontFamily; - } + @override + String get defaultTooltip => context.loc.fontFamily; void _onPressed() { if (_menuController.isOpen) { @@ -149,7 +86,7 @@ class QuillToolbarFontFamilyButtonState } else { _menuController.open(); } - options.afterButtonPressed?.call(); + afterButtonPressed?.call(); } final _menuController = MenuController(); @@ -163,7 +100,7 @@ class QuillToolbarFontFamilyButtonState return childBuilder( options, QuillToolbarFontFamilyButtonExtraOptions( - currentValue: _currentValue, + currentValue: currentValue, defaultDisplayText: _defaultDisplayText, controller: controller, context: context, @@ -177,8 +114,8 @@ class QuillToolbarFontFamilyButtonState var effectiveTooltip = tooltip; if (options.overrideTooltipByFontFamily) { effectiveTooltip = effectiveTooltip.isNotEmpty - ? '$effectiveTooltip: $_currentValue' - : '${context.loc.font}: $_currentValue'; + ? '$effectiveTooltip: $currentValue' + : '${context.loc.font}: $currentValue'; } return Tooltip(message: effectiveTooltip, child: child); }, @@ -196,9 +133,9 @@ class QuillToolbarFontFamilyButtonState final keyName = _getKeyName(newValue); setState(() { if (keyName != 'Clear') { - _currentValue = keyName ?? _defaultDisplayText; + currentValue = keyName ?? _defaultDisplayText; } else { - _currentValue = _defaultDisplayText; + currentValue = _defaultDisplayText; } if (keyName != null) { controller.formatSelection( @@ -210,12 +147,6 @@ class QuillToolbarFontFamilyButtonState options.onSelected?.call(newValue); } }); - - if (fontFamily.value == 'Clear') { - controller.selectFontFamily(null); - return; - } - controller.selectFontFamily(fontFamily); }, child: Text( fontFamily.key.toString(), @@ -262,7 +193,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 ?? diff --git a/lib/src/widgets/toolbar/buttons/font_size_button.dart b/lib/src/widgets/toolbar/buttons/font_size_button.dart index 17807c47..71a6bfcc 100644 --- a/lib/src/widgets/toolbar/buttons/font_size_button.dart +++ b/lib/src/widgets/toolbar/buttons/font_size_button.dart @@ -5,43 +5,35 @@ import '../../../../extensions.dart'; import '../../../extensions/quill_configurations_ext.dart'; import '../../../l10n/extensions/localizations.dart'; import '../../../models/documents/attribute.dart'; -import '../../../models/themes/quill_icon_theme.dart'; import '../../../utils/font.dart'; -import '../../quill/quill_controller.dart'; +import '../base_button/base_value_button.dart'; import '../base_toolbar.dart'; -class QuillToolbarFontSizeButton extends StatefulWidget { +class QuillToolbarFontSizeButton extends QuillToolbarBaseValueButton< + QuillToolbarFontSizeButtonOptions, QuillToolbarFontSizeButtonExtraOptions> { QuillToolbarFontSizeButton({ - required this.controller, + required super.controller, @Deprecated('Please use the default display text from the options') this.defaultDisplayText, - this.options = const QuillToolbarFontSizeButtonOptions(), + super.options = const QuillToolbarFontSizeButtonOptions(), super.key, }) : assert(options.rawItemsMap?.isNotEmpty ?? true), assert(options.initialValue == null || (options.initialValue?.isNotEmpty ?? true)); - final QuillToolbarFontSizeButtonOptions options; - final String? defaultDisplayText; - /// Since we can't get the state from the instace of the widget for comparing - /// in [didUpdateWidget] then we will have to store reference here - final QuillController controller; - @override QuillToolbarFontSizeButtonState createState() => QuillToolbarFontSizeButtonState(); } -class QuillToolbarFontSizeButtonState - extends State { +class QuillToolbarFontSizeButtonState extends QuillToolbarBaseValueButtonState< + QuillToolbarFontSizeButton, + QuillToolbarFontSizeButtonOptions, + QuillToolbarFontSizeButtonExtraOptions, + String> { final _menuController = MenuController(); - String _currentValue = ''; - - QuillToolbarFontSizeButtonOptions get options { - return widget.options; - } Map get rawItemsMap { final fontSizes = options.rawItemsMap ?? @@ -73,14 +65,12 @@ class QuillToolbarFontSizeButtonState } @override - void didChangeDependencies() { - super.didChangeDependencies(); - _currentValue = _defaultDisplayText; - } - - @override - void dispose() { - super.dispose(); + String get currentStateValue { + final attribute = + controller.getSelectionStyle().attributes[options.attribute.key]; + return attribute == null + ? _defaultDisplayText + : (_getKeyName(attribute.value) ?? _defaultDisplayText); } String? _getKeyName(dynamic value) { @@ -92,38 +82,8 @@ class QuillToolbarFontSizeButtonState return null; } - QuillController get controller { - return widget.controller; - } - - double get iconSize { - final baseFontSize = context.quillToolbarBaseButtonOptions?.iconSize; - final iconSize = options.iconSize; - return iconSize ?? baseFontSize ?? kDefaultIconSize; - } - - double get iconButtonFactor { - final baseIconFactor = - context.quillToolbarBaseButtonOptions?.iconButtonFactor; - final iconButtonFactor = options.iconButtonFactor; - return iconButtonFactor ?? baseIconFactor ?? kDefaultIconButtonFactor; - } - - VoidCallback? get afterButtonPressed { - return options.afterButtonPressed ?? - context.quillToolbarBaseButtonOptions?.afterButtonPressed; - } - - QuillIconTheme? get iconTheme { - return options.iconTheme ?? - context.quillToolbarBaseButtonOptions?.iconTheme; - } - - String get tooltip { - return options.tooltip ?? - context.quillToolbarBaseButtonOptions?.tooltip ?? - context.loc.fontSize; - } + @override + String get defaultTooltip => context.loc.fontSize; void _onDropdownButtonPressed() { if (_menuController.isOpen) { @@ -144,7 +104,7 @@ class QuillToolbarFontSizeButtonState options, QuillToolbarFontSizeButtonExtraOptions( controller: controller, - currentValue: _currentValue, + currentValue: currentValue, defaultDisplayText: _defaultDisplayText, context: context, onPressed: _onDropdownButtonPressed, @@ -162,9 +122,9 @@ class QuillToolbarFontSizeButtonState final keyName = _getKeyName(newValue); setState(() { if (keyName != context.loc.clear) { - _currentValue = keyName ?? _defaultDisplayText; + currentValue = keyName ?? _defaultDisplayText; } else { - _currentValue = _defaultDisplayText; + currentValue = _defaultDisplayText; } if (keyName != null) { controller.formatSelection( @@ -176,12 +136,6 @@ class QuillToolbarFontSizeButtonState options.onSelected?.call(newValue); } }); - - if (fontSize.value == '0') { - controller.selectFontSize(null); - return; - } - controller.selectFontSize(fontSize); }, child: Text( fontSize.key.toString(), @@ -224,9 +178,7 @@ class QuillToolbarFontSizeButtonState enabled: hasFinalWidth, wrapper: (child) => Expanded(child: child), child: Text( - getLabel(widget.controller.selectedFontSize?.key) ?? - getLabel(_currentValue) ?? - '', + getLabel(currentValue) ?? '', overflow: options.labelOverflow, style: options.style ?? TextStyle( diff --git a/lib/src/widgets/toolbar/buttons/toggle_check_list_button.dart b/lib/src/widgets/toolbar/buttons/toggle_check_list_button.dart index 83679798..00a46db3 100644 --- a/lib/src/widgets/toolbar/buttons/toggle_check_list_button.dart +++ b/lib/src/widgets/toolbar/buttons/toggle_check_list_button.dart @@ -1,48 +1,36 @@ import 'package:flutter/material.dart'; -import '../../../extensions/quill_configurations_ext.dart'; import '../../../l10n/extensions/localizations.dart'; import '../../../models/documents/attribute.dart'; import '../../../models/documents/style.dart'; -import '../../../models/themes/quill_icon_theme.dart'; import '../../../utils/widgets.dart'; -import '../../quill/quill_controller.dart'; +import '../base_button/base_value_button.dart'; import '../base_toolbar.dart'; -class QuillToolbarToggleCheckListButton extends StatefulWidget { +class QuillToolbarToggleCheckListButton extends QuillToolbarBaseValueButton< + QuillToolbarToggleCheckListButtonOptions, + QuillToolbarToggleCheckListButtonExtraOptions> { const QuillToolbarToggleCheckListButton({ - required this.controller, - this.options = const QuillToolbarToggleCheckListButtonOptions(), + required super.controller, + super.options = const QuillToolbarToggleCheckListButtonOptions(), super.key, }); - final QuillToolbarToggleCheckListButtonOptions options; - - final QuillController controller; - @override QuillToolbarToggleCheckListButtonState createState() => QuillToolbarToggleCheckListButtonState(); } class QuillToolbarToggleCheckListButtonState - extends State { - bool? _isToggled; - + extends QuillToolbarBaseValueButtonState< + QuillToolbarToggleCheckListButton, + QuillToolbarToggleCheckListButtonOptions, + QuillToolbarToggleCheckListButtonExtraOptions, + bool> { Style get _selectionStyle => controller.getSelectionStyle(); - void _didChangeEditingValue() { - setState(() { - _isToggled = _getIsToggled(controller.getSelectionStyle().attributes); - }); - } - @override - void initState() { - super.initState(); - _isToggled = _getIsToggled(_selectionStyle.attributes); - controller.addListener(_didChangeEditingValue); - } + bool get currentStateValue => _getIsToggled(_selectionStyle.attributes); bool _getIsToggled(Map attrs) { var attribute = controller.toolbarButtonToggler[Attribute.list.key]; @@ -62,53 +50,7 @@ class QuillToolbarToggleCheckListButtonState } @override - void didUpdateWidget(covariant QuillToolbarToggleCheckListButton oldWidget) { - super.didUpdateWidget(oldWidget); - if (oldWidget.controller != controller) { - oldWidget.controller.removeListener(_didChangeEditingValue); - controller.addListener(_didChangeEditingValue); - _isToggled = _getIsToggled(_selectionStyle.attributes); - } - } - - @override - void dispose() { - controller.removeListener(_didChangeEditingValue); - super.dispose(); - } - - QuillToolbarToggleCheckListButtonOptions get options { - return widget.options; - } - - QuillController get controller { - return widget.controller; - } - - double get iconSize { - final baseFontSize = baseButtonExtraOptions?.iconSize; - final iconSize = options.iconSize; - return iconSize ?? baseFontSize ?? kDefaultIconSize; - } - - double get iconButtonFactor { - final baseIconFactor = baseButtonExtraOptions?.iconButtonFactor; - final iconButtonFactor = options.iconButtonFactor; - return iconButtonFactor ?? baseIconFactor ?? kDefaultIconButtonFactor; - } - - VoidCallback? get afterButtonPressed { - return options.afterButtonPressed ?? - baseButtonExtraOptions?.afterButtonPressed; - } - - QuillIconTheme? get iconTheme { - return options.iconTheme ?? baseButtonExtraOptions?.iconTheme; - } - - QuillToolbarBaseButtonOptions? get baseButtonExtraOptions { - return context.quillToolbarBaseButtonOptions; - } + String get defaultTooltip => context.loc.checkedList; IconData get iconData { return options.iconData ?? @@ -116,12 +58,6 @@ class QuillToolbarToggleCheckListButtonState Icons.check_box; } - String get tooltip { - return options.tooltip ?? - baseButtonExtraOptions?.tooltip ?? - context.loc.checkedList; - } - @override Widget build(BuildContext context) { final childBuilder = @@ -136,7 +72,7 @@ class QuillToolbarToggleCheckListButtonState _toggleAttribute(); afterButtonPressed?.call(); }, - isToggled: _isToggled ?? false, + isToggled: currentValue, ), ); } @@ -146,7 +82,7 @@ class QuillToolbarToggleCheckListButtonState context, Attribute.unchecked, iconData, - _isToggled, + currentValue, _toggleAttribute, afterButtonPressed, iconSize, @@ -160,7 +96,7 @@ class QuillToolbarToggleCheckListButtonState controller ..skipRequestKeyboard = !options.isShouldRequestKeyboard ..formatSelection( - _isToggled! + currentValue ? Attribute.clone(Attribute.unchecked, null) : Attribute.unchecked, ); diff --git a/lib/src/widgets/toolbar/buttons/toggle_style_button.dart b/lib/src/widgets/toolbar/buttons/toggle_style_button.dart index 6447bffd..fa065190 100644 --- a/lib/src/widgets/toolbar/buttons/toggle_style_button.dart +++ b/lib/src/widgets/toolbar/buttons/toggle_style_button.dart @@ -6,7 +6,7 @@ import '../../../models/documents/attribute.dart'; import '../../../models/documents/style.dart'; import '../../../models/themes/quill_icon_theme.dart'; import '../../../utils/widgets.dart'; -import '../../quill/quill_controller.dart'; +import '../base_button/base_value_button.dart'; import '../base_toolbar.dart'; typedef ToggleStyleButtonBuilder = Widget Function( @@ -20,68 +20,33 @@ typedef ToggleStyleButtonBuilder = Widget Function( QuillIconTheme? iconTheme, ]); -class QuillToolbarToggleStyleButton extends StatefulWidget { +class QuillToolbarToggleStyleButton extends QuillToolbarBaseValueButton< + QuillToolbarToggleStyleButtonOptions, + QuillToolbarToggleStyleButtonExtraOptions> { const QuillToolbarToggleStyleButton({ - required this.controller, + required super.controller, required this.attribute, - this.options = const QuillToolbarToggleStyleButtonOptions(), + super.options = const QuillToolbarToggleStyleButtonOptions(), super.key, }); final Attribute attribute; - final QuillToolbarToggleStyleButtonOptions options; - - final QuillController controller; - @override QuillToolbarToggleStyleButtonState createState() => QuillToolbarToggleStyleButtonState(); } class QuillToolbarToggleStyleButtonState - extends State { - bool? _isToggled; - + extends QuillToolbarBaseValueButtonState< + QuillToolbarToggleStyleButton, + QuillToolbarToggleStyleButtonOptions, + QuillToolbarToggleStyleButtonExtraOptions, + bool> { Style get _selectionStyle => controller.getSelectionStyle(); - QuillToolbarToggleStyleButtonOptions get options { - return widget.options; - } - @override - void initState() { - super.initState(); - _isToggled = _getIsToggled(_selectionStyle.attributes); - controller.addListener(_didChangeEditingValue); - } - - QuillController get controller { - return widget.controller; - } - - double get iconSize { - final baseFontSize = context.quillToolbarBaseButtonOptions?.iconSize; - final iconSize = options.iconSize; - return iconSize ?? baseFontSize ?? kDefaultIconSize; - } - - double get iconButtonFactor { - final baseIconFactor = - context.quillToolbarBaseButtonOptions?.iconButtonFactor; - final iconButtonFactor = options.iconButtonFactor; - return iconButtonFactor ?? baseIconFactor ?? kDefaultIconButtonFactor; - } - - VoidCallback? get afterButtonPressed { - return options.afterButtonPressed ?? - context.quillToolbarBaseButtonOptions?.afterButtonPressed; - } - - QuillIconTheme? get iconTheme { - return options.iconTheme ?? - context.quillToolbarBaseButtonOptions?.iconTheme; - } + bool get currentStateValue => _getIsToggled(_selectionStyle.attributes); (String, IconData) get _defaultTooltipAndIconData { switch (widget.attribute.key) { @@ -133,11 +98,8 @@ class QuillToolbarToggleStyleButtonState } } - String? get tooltip { - return options.tooltip ?? - context.quillToolbarBaseButtonOptions?.tooltip ?? - _defaultTooltipAndIconData.$1; - } + @override + String get defaultTooltip => _defaultTooltipAndIconData.$1; IconData get iconData { return options.iconData ?? @@ -161,7 +123,7 @@ class QuillToolbarToggleStyleButtonState context: context, controller: controller, onPressed: _onPressed, - isToggled: _isToggled ?? false, + isToggled: currentValue, ), ); } @@ -171,7 +133,7 @@ class QuillToolbarToggleStyleButtonState context, widget.attribute, iconData, - _isToggled, + currentValue, _toggleAttribute, afterButtonPressed, iconSize, @@ -181,26 +143,6 @@ class QuillToolbarToggleStyleButtonState ); } - @override - void didUpdateWidget(covariant QuillToolbarToggleStyleButton oldWidget) { - super.didUpdateWidget(oldWidget); - if (oldWidget.controller != controller) { - oldWidget.controller.removeListener(_didChangeEditingValue); - controller.addListener(_didChangeEditingValue); - _isToggled = _getIsToggled(_selectionStyle.attributes); - } - } - - @override - void dispose() { - controller.removeListener(_didChangeEditingValue); - super.dispose(); - } - - void _didChangeEditingValue() { - setState(() => _isToggled = _getIsToggled(_selectionStyle.attributes)); - } - bool _getIsToggled(Map attrs) { if (widget.attribute.key == Attribute.list.key || widget.attribute.key == Attribute.script.key || @@ -216,12 +158,12 @@ class QuillToolbarToggleStyleButtonState void _toggleAttribute() { controller + ..skipRequestKeyboard = !widget.attribute.isInline ..formatSelection( - (_isToggled ?? false) + currentValue ? Attribute.clone(widget.attribute, null) : widget.attribute, - ) - ..selectStyle(widget.attribute, _isToggled ?? false); + ); } }