From 7419af62a0fc8fb854f5e66b2d293eaa3c3c46ec Mon Sep 17 00:00:00 2001 From: AtlasAutocode Date: Sun, 21 Apr 2024 07:45:33 -0600 Subject: [PATCH] Rename base class as QuillToolbarBaseValueButton --- ..._button_ex.dart => base_value_button.dart} | 29 ++++++++++---- .../toolbar/buttons/font_family_button.dart | 40 +++++++------------ .../toolbar/buttons/font_size_button.dart | 36 ++++++----------- .../buttons/toggle_check_list_button.dart | 36 +++++------------ .../toolbar/buttons/toggle_style_button.dart | 39 ++++++------------ 5 files changed, 69 insertions(+), 111 deletions(-) rename lib/src/widgets/toolbar/base_button/{stateful_base_button_ex.dart => base_value_button.dart} (75%) diff --git a/lib/src/widgets/toolbar/base_button/stateful_base_button_ex.dart b/lib/src/widgets/toolbar/base_button/base_value_button.dart similarity index 75% rename from lib/src/widgets/toolbar/base_button/stateful_base_button_ex.dart rename to lib/src/widgets/toolbar/base_button/base_value_button.dart index eebd808d..e3b52db2 100644 --- a/lib/src/widgets/toolbar/base_button/stateful_base_button_ex.dart +++ b/lib/src/widgets/toolbar/base_button/base_value_button.dart @@ -1,14 +1,13 @@ -import 'package:flutter/cupertino.dart'; 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 QuillToolbarStatefulBaseButton< +abstract class QuillToolbarBaseValueButton< T extends QuillToolbarBaseButtonOptions, E extends QuillToolbarBaseButtonExtraOptions> extends StatefulWidget { - const QuillToolbarStatefulBaseButton( + const QuillToolbarBaseValueButton( {required this.controller, required this.options, super.key}); final T options; @@ -17,21 +16,36 @@ abstract class QuillToolbarStatefulBaseButton< } /// The [W] is the widget that creates this State -abstract class QuillToolbarBaseButtonState< - W extends QuillToolbarStatefulBaseButton, +/// The [V] is the type of the currentValue +abstract class QuillToolbarBaseValueButtonState< + W extends QuillToolbarBaseValueButton, T extends QuillToolbarBaseButtonOptions, - E extends QuillToolbarBaseButtonExtraOptions> extends State { + 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); } - void didChangeEditingValue(); + @override + void didChangeDependencies() { + super.didChangeDependencies(); + currentValue = currentStateValue; + } + + void didChangeEditingValue() { + setState(() => currentValue = currentStateValue); + } @override void dispose() { @@ -45,6 +59,7 @@ abstract class QuillToolbarBaseButtonState< if (oldWidget.controller != controller) { oldWidget.controller.removeListener(didChangeEditingValue); controller.addListener(didChangeEditingValue); + currentValue = currentStateValue; } } diff --git a/lib/src/widgets/toolbar/buttons/font_family_button.dart b/lib/src/widgets/toolbar/buttons/font_family_button.dart index e3899b1b..ce593b08 100644 --- a/lib/src/widgets/toolbar/buttons/font_family_button.dart +++ b/lib/src/widgets/toolbar/buttons/font_family_button.dart @@ -4,10 +4,10 @@ import '../../../../extensions.dart'; import '../../../extensions/quill_configurations_ext.dart'; import '../../../l10n/extensions/localizations.dart'; import '../../../models/documents/attribute.dart'; -import '../base_button/stateful_base_button_ex.dart'; +import '../base_button/base_value_button.dart'; import '../base_toolbar.dart'; -class QuillToolbarFontFamilyButton extends QuillToolbarStatefulBaseButton< +class QuillToolbarFontFamilyButton extends QuillToolbarBaseValueButton< QuillToolbarFontFamilyButtonOptions, QuillToolbarFontFamilyButtonExtraOptions> { QuillToolbarFontFamilyButton({ @@ -28,16 +28,16 @@ class QuillToolbarFontFamilyButton extends QuillToolbarStatefulBaseButton< QuillToolbarFontFamilyButtonState(); } -class QuillToolbarFontFamilyButtonState extends QuillToolbarBaseButtonState< +class QuillToolbarFontFamilyButtonState extends QuillToolbarBaseValueButtonState< QuillToolbarFontFamilyButton, QuillToolbarFontFamilyButtonOptions, - QuillToolbarFontFamilyButtonExtraOptions> { - var _currentValue = ''; + 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 { @@ -47,18 +47,6 @@ class QuillToolbarFontFamilyButtonState extends QuillToolbarBaseButtonState< context.loc.font; } - @override - 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 get rawItemsMap { final rawItemsMap = context.quillSimpleToolbarConfigurations?.fontFamilyValues ?? @@ -109,7 +97,7 @@ class QuillToolbarFontFamilyButtonState extends QuillToolbarBaseButtonState< return childBuilder( options, QuillToolbarFontFamilyButtonExtraOptions( - currentValue: _currentValue, + currentValue: currentValue, defaultDisplayText: _defaultDisplayText, controller: controller, context: context, @@ -123,8 +111,8 @@ class QuillToolbarFontFamilyButtonState extends QuillToolbarBaseButtonState< 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); }, @@ -142,9 +130,9 @@ class QuillToolbarFontFamilyButtonState extends QuillToolbarBaseButtonState< final keyName = _getKeyName(newValue); setState(() { if (keyName != 'Clear') { - _currentValue = keyName ?? _defaultDisplayText; + currentValue = keyName ?? _defaultDisplayText; } else { - _currentValue = _defaultDisplayText; + currentValue = _defaultDisplayText; } if (keyName != null) { controller.formatSelection( @@ -208,7 +196,7 @@ class QuillToolbarFontFamilyButtonState extends QuillToolbarBaseButtonState< enabled: hasFinalWidth, wrapper: (child) => Expanded(child: child), child: Text( - _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 4cb80ae9..a4c74318 100644 --- a/lib/src/widgets/toolbar/buttons/font_size_button.dart +++ b/lib/src/widgets/toolbar/buttons/font_size_button.dart @@ -6,10 +6,10 @@ import '../../../extensions/quill_configurations_ext.dart'; import '../../../l10n/extensions/localizations.dart'; import '../../../models/documents/attribute.dart'; import '../../../utils/font.dart'; -import '../base_button/stateful_base_button_ex.dart'; +import '../base_button/base_value_button.dart'; import '../base_toolbar.dart'; -class QuillToolbarFontSizeButton extends QuillToolbarStatefulBaseButton< +class QuillToolbarFontSizeButton extends QuillToolbarBaseValueButton< QuillToolbarFontSizeButtonOptions, QuillToolbarFontSizeButtonExtraOptions> { QuillToolbarFontSizeButton({ required super.controller, @@ -28,12 +28,12 @@ class QuillToolbarFontSizeButton extends QuillToolbarStatefulBaseButton< QuillToolbarFontSizeButtonState(); } -class QuillToolbarFontSizeButtonState extends QuillToolbarBaseButtonState< +class QuillToolbarFontSizeButtonState extends QuillToolbarBaseValueButtonState< QuillToolbarFontSizeButton, QuillToolbarFontSizeButtonOptions, - QuillToolbarFontSizeButtonExtraOptions> { + QuillToolbarFontSizeButtonExtraOptions, + String> { final _menuController = MenuController(); - String _currentValue = ''; Map get rawItemsMap { final fontSizes = options.rawItemsMap ?? @@ -65,21 +65,9 @@ class QuillToolbarFontSizeButtonState extends QuillToolbarBaseButtonState< } @override - void didChangeDependencies() { - super.didChangeDependencies(); - _currentValue = _defaultDisplayText; - } - - @override - 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 get currentStateValue { + final attribute = controller.getSelectionStyle().attributes[options.attribute.key]; + return attribute == null ? _defaultDisplayText : (_getKeyName(attribute.value) ?? _defaultDisplayText); } String? _getKeyName(dynamic value) { @@ -113,7 +101,7 @@ class QuillToolbarFontSizeButtonState extends QuillToolbarBaseButtonState< options, QuillToolbarFontSizeButtonExtraOptions( controller: controller, - currentValue: _currentValue, + currentValue: currentValue, defaultDisplayText: _defaultDisplayText, context: context, onPressed: _onDropdownButtonPressed, @@ -131,9 +119,9 @@ class QuillToolbarFontSizeButtonState extends QuillToolbarBaseButtonState< 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( @@ -193,7 +181,7 @@ class QuillToolbarFontSizeButtonState extends QuillToolbarBaseButtonState< enabled: hasFinalWidth, wrapper: (child) => Expanded(child: child), child: Text( - 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 0106fd10..2fa2892b 100644 --- a/lib/src/widgets/toolbar/buttons/toggle_check_list_button.dart +++ b/lib/src/widgets/toolbar/buttons/toggle_check_list_button.dart @@ -4,10 +4,10 @@ import '../../../l10n/extensions/localizations.dart'; import '../../../models/documents/attribute.dart'; import '../../../models/documents/style.dart'; import '../../../utils/widgets.dart'; -import '../base_button/stateful_base_button_ex.dart'; +import '../base_button/base_value_button.dart'; import '../base_toolbar.dart'; -class QuillToolbarToggleCheckListButton extends QuillToolbarStatefulBaseButton< +class QuillToolbarToggleCheckListButton extends QuillToolbarBaseValueButton< QuillToolbarToggleCheckListButtonOptions, QuillToolbarToggleCheckListButtonExtraOptions> { const QuillToolbarToggleCheckListButton({ @@ -22,26 +22,16 @@ class QuillToolbarToggleCheckListButton extends QuillToolbarStatefulBaseButton< } class QuillToolbarToggleCheckListButtonState - extends QuillToolbarBaseButtonState< + extends QuillToolbarBaseValueButtonState< QuillToolbarToggleCheckListButton, QuillToolbarToggleCheckListButtonOptions, - QuillToolbarToggleCheckListButtonExtraOptions> { - bool? _isToggled; + QuillToolbarToggleCheckListButtonExtraOptions, + bool> { Style get _selectionStyle => controller.getSelectionStyle(); @override - void didChangeEditingValue() { - setState(() { - _isToggled = _getIsToggled(controller.getSelectionStyle().attributes); - }); - } - - @override - void initState() { - super.initState(); - _isToggled = _getIsToggled(_selectionStyle.attributes); - } + bool get currentStateValue => _getIsToggled(_selectionStyle.attributes); bool _getIsToggled(Map attrs) { var attribute = controller.toolbarButtonToggler[Attribute.list.key]; @@ -60,14 +50,6 @@ class QuillToolbarToggleCheckListButtonState attribute.value == Attribute.checked.value; } - @override - void didUpdateWidget(covariant QuillToolbarToggleCheckListButton oldWidget) { - super.didUpdateWidget(oldWidget); - if (oldWidget.controller != controller) { - _isToggled = _getIsToggled(_selectionStyle.attributes); - } - } - @override String get defaultTooltip => context.loc.checkedList; @@ -91,7 +73,7 @@ class QuillToolbarToggleCheckListButtonState _toggleAttribute(); afterButtonPressed?.call(); }, - isToggled: _isToggled ?? false, + isToggled: currentValue, ), ); } @@ -102,7 +84,7 @@ class QuillToolbarToggleCheckListButtonState Attribute.unchecked, iconData, options.fillColor, - _isToggled, + currentValue, _toggleAttribute, afterButtonPressed, iconSize, @@ -116,7 +98,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 eeb34c74..cffbce24 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 '../base_button/stateful_base_button_ex.dart'; +import '../base_button/base_value_button.dart'; import '../base_toolbar.dart'; typedef ToggleStyleButtonBuilder = Widget Function( @@ -21,7 +21,7 @@ typedef ToggleStyleButtonBuilder = Widget Function( QuillIconTheme? iconTheme, ]); -class QuillToolbarToggleStyleButton extends QuillToolbarStatefulBaseButton< +class QuillToolbarToggleStyleButton extends QuillToolbarBaseValueButton< QuillToolbarToggleStyleButtonOptions, QuillToolbarToggleStyleButtonExtraOptions> { const QuillToolbarToggleStyleButton({ @@ -38,19 +38,16 @@ class QuillToolbarToggleStyleButton extends QuillToolbarStatefulBaseButton< QuillToolbarToggleStyleButtonState(); } -class QuillToolbarToggleStyleButtonState extends QuillToolbarBaseButtonState< +class QuillToolbarToggleStyleButtonState extends QuillToolbarBaseValueButtonState< QuillToolbarToggleStyleButton, QuillToolbarToggleStyleButtonOptions, - QuillToolbarToggleStyleButtonExtraOptions> { - bool? _isToggled; + QuillToolbarToggleStyleButtonExtraOptions, + bool> { Style get _selectionStyle => controller.getSelectionStyle(); @override - void initState() { - super.initState(); - _isToggled = _getIsToggled(_selectionStyle.attributes); - } + bool get currentStateValue => _getIsToggled(_selectionStyle.attributes); (String, IconData) get _defaultTooltipAndIconData { switch (widget.attribute.key) { @@ -127,7 +124,7 @@ class QuillToolbarToggleStyleButtonState extends QuillToolbarBaseButtonState< context: context, controller: controller, onPressed: _onPressed, - isToggled: _isToggled ?? false, + isToggled: currentValue, ), ); } @@ -138,7 +135,7 @@ class QuillToolbarToggleStyleButtonState extends QuillToolbarBaseButtonState< widget.attribute, iconData, options.fillColor, - _isToggled, + currentValue, _toggleAttribute, afterButtonPressed, iconSize, @@ -148,19 +145,6 @@ class QuillToolbarToggleStyleButtonState extends QuillToolbarBaseButtonState< ); } - @override - void didUpdateWidget(covariant QuillToolbarToggleStyleButton oldWidget) { - super.didUpdateWidget(oldWidget); - if (oldWidget.controller != controller) { - _isToggled = _getIsToggled(_selectionStyle.attributes); - } - } - - @override - void didChangeEditingValue() { - setState(() => _isToggled = _getIsToggled(_selectionStyle.attributes)); - } - bool _getIsToggled(Map attrs) { if (widget.attribute.key == Attribute.list.key || widget.attribute.key == Attribute.script.key || @@ -175,11 +159,12 @@ class QuillToolbarToggleStyleButtonState extends QuillToolbarBaseButtonState< } void _toggleAttribute() { - controller.formatSelection( - (_isToggled ?? false) + controller..formatSelection( + currentValue ? Attribute.clone(widget.attribute, null) : widget.attribute, - ); + ) + ..selectStyle(widget.attribute, currentValue); } }