Rename base class as QuillToolbarBaseValueButton

pull/1829/head
AtlasAutocode 1 year ago
parent 04f086c3f8
commit 7419af62a0
  1. 29
      lib/src/widgets/toolbar/base_button/base_value_button.dart
  2. 40
      lib/src/widgets/toolbar/buttons/font_family_button.dart
  3. 36
      lib/src/widgets/toolbar/buttons/font_size_button.dart
  4. 36
      lib/src/widgets/toolbar/buttons/toggle_check_list_button.dart
  5. 39
      lib/src/widgets/toolbar/buttons/toggle_style_button.dart

@ -1,14 +1,13 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../../../../flutter_quill.dart'; import '../../../../flutter_quill.dart';
/// The [T] is the options for the button /// The [T] is the options for the button
/// The [E] is the extra options for the button /// The [E] is the extra options for the button
abstract class QuillToolbarStatefulBaseButton< abstract class QuillToolbarBaseValueButton<
T extends QuillToolbarBaseButtonOptions<T, E>, T extends QuillToolbarBaseButtonOptions<T, E>,
E extends QuillToolbarBaseButtonExtraOptions> extends StatefulWidget { E extends QuillToolbarBaseButtonExtraOptions> extends StatefulWidget {
const QuillToolbarStatefulBaseButton( const QuillToolbarBaseValueButton(
{required this.controller, required this.options, super.key}); {required this.controller, required this.options, super.key});
final T options; final T options;
@ -17,21 +16,36 @@ abstract class QuillToolbarStatefulBaseButton<
} }
/// The [W] is the widget that creates this State /// The [W] is the widget that creates this State
abstract class QuillToolbarBaseButtonState< /// The [V] is the type of the currentValue
W extends QuillToolbarStatefulBaseButton<T, E>, abstract class QuillToolbarBaseValueButtonState<
W extends QuillToolbarBaseValueButton<T, E>,
T extends QuillToolbarBaseButtonOptions<T, E>, T extends QuillToolbarBaseButtonOptions<T, E>,
E extends QuillToolbarBaseButtonExtraOptions> extends State<W> { E extends QuillToolbarBaseButtonExtraOptions,
V > extends State<W> {
T get options => widget.options; T get options => widget.options;
QuillController get controller => widget.controller; 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 @override
void initState() { void initState() {
super.initState(); super.initState();
controller.addListener(didChangeEditingValue); controller.addListener(didChangeEditingValue);
} }
void didChangeEditingValue(); @override
void didChangeDependencies() {
super.didChangeDependencies();
currentValue = currentStateValue;
}
void didChangeEditingValue() {
setState(() => currentValue = currentStateValue);
}
@override @override
void dispose() { void dispose() {
@ -45,6 +59,7 @@ abstract class QuillToolbarBaseButtonState<
if (oldWidget.controller != controller) { if (oldWidget.controller != controller) {
oldWidget.controller.removeListener(didChangeEditingValue); oldWidget.controller.removeListener(didChangeEditingValue);
controller.addListener(didChangeEditingValue); controller.addListener(didChangeEditingValue);
currentValue = currentStateValue;
} }
} }

@ -4,10 +4,10 @@ import '../../../../extensions.dart';
import '../../../extensions/quill_configurations_ext.dart'; import '../../../extensions/quill_configurations_ext.dart';
import '../../../l10n/extensions/localizations.dart'; import '../../../l10n/extensions/localizations.dart';
import '../../../models/documents/attribute.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'; import '../base_toolbar.dart';
class QuillToolbarFontFamilyButton extends QuillToolbarStatefulBaseButton< class QuillToolbarFontFamilyButton extends QuillToolbarBaseValueButton<
QuillToolbarFontFamilyButtonOptions, QuillToolbarFontFamilyButtonOptions,
QuillToolbarFontFamilyButtonExtraOptions> { QuillToolbarFontFamilyButtonExtraOptions> {
QuillToolbarFontFamilyButton({ QuillToolbarFontFamilyButton({
@ -28,16 +28,16 @@ class QuillToolbarFontFamilyButton extends QuillToolbarStatefulBaseButton<
QuillToolbarFontFamilyButtonState(); QuillToolbarFontFamilyButtonState();
} }
class QuillToolbarFontFamilyButtonState extends QuillToolbarBaseButtonState< class QuillToolbarFontFamilyButtonState extends QuillToolbarBaseValueButtonState<
QuillToolbarFontFamilyButton, QuillToolbarFontFamilyButton,
QuillToolbarFontFamilyButtonOptions, QuillToolbarFontFamilyButtonOptions,
QuillToolbarFontFamilyButtonExtraOptions> { QuillToolbarFontFamilyButtonExtraOptions,
var _currentValue = ''; String> {
@override @override
void didChangeDependencies() { String get currentStateValue {
super.didChangeDependencies(); final attribute = controller.getSelectionStyle().attributes[options.attribute.key];
_currentValue = _defaultDisplayText; return attribute == null ? _defaultDisplayText : (_getKeyName(attribute.value) ?? _defaultDisplayText);
} }
String get _defaultDisplayText { String get _defaultDisplayText {
@ -47,18 +47,6 @@ class QuillToolbarFontFamilyButtonState extends QuillToolbarBaseButtonState<
context.loc.font; 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<String, String> get rawItemsMap { Map<String, String> get rawItemsMap {
final rawItemsMap = final rawItemsMap =
context.quillSimpleToolbarConfigurations?.fontFamilyValues ?? context.quillSimpleToolbarConfigurations?.fontFamilyValues ??
@ -109,7 +97,7 @@ class QuillToolbarFontFamilyButtonState extends QuillToolbarBaseButtonState<
return childBuilder( return childBuilder(
options, options,
QuillToolbarFontFamilyButtonExtraOptions( QuillToolbarFontFamilyButtonExtraOptions(
currentValue: _currentValue, currentValue: currentValue,
defaultDisplayText: _defaultDisplayText, defaultDisplayText: _defaultDisplayText,
controller: controller, controller: controller,
context: context, context: context,
@ -123,8 +111,8 @@ class QuillToolbarFontFamilyButtonState extends QuillToolbarBaseButtonState<
var effectiveTooltip = tooltip; var effectiveTooltip = tooltip;
if (options.overrideTooltipByFontFamily) { if (options.overrideTooltipByFontFamily) {
effectiveTooltip = effectiveTooltip.isNotEmpty effectiveTooltip = effectiveTooltip.isNotEmpty
? '$effectiveTooltip: $_currentValue' ? '$effectiveTooltip: $currentValue'
: '${context.loc.font}: $_currentValue'; : '${context.loc.font}: $currentValue';
} }
return Tooltip(message: effectiveTooltip, child: child); return Tooltip(message: effectiveTooltip, child: child);
}, },
@ -142,9 +130,9 @@ class QuillToolbarFontFamilyButtonState extends QuillToolbarBaseButtonState<
final keyName = _getKeyName(newValue); final keyName = _getKeyName(newValue);
setState(() { setState(() {
if (keyName != 'Clear') { if (keyName != 'Clear') {
_currentValue = keyName ?? _defaultDisplayText; currentValue = keyName ?? _defaultDisplayText;
} else { } else {
_currentValue = _defaultDisplayText; currentValue = _defaultDisplayText;
} }
if (keyName != null) { if (keyName != null) {
controller.formatSelection( controller.formatSelection(
@ -208,7 +196,7 @@ class QuillToolbarFontFamilyButtonState extends QuillToolbarBaseButtonState<
enabled: hasFinalWidth, enabled: hasFinalWidth,
wrapper: (child) => Expanded(child: child), wrapper: (child) => Expanded(child: child),
child: Text( child: Text(
_currentValue, currentValue,
maxLines: 1, maxLines: 1,
overflow: options.labelOverflow, overflow: options.labelOverflow,
style: options.style ?? style: options.style ??

@ -6,10 +6,10 @@ import '../../../extensions/quill_configurations_ext.dart';
import '../../../l10n/extensions/localizations.dart'; import '../../../l10n/extensions/localizations.dart';
import '../../../models/documents/attribute.dart'; import '../../../models/documents/attribute.dart';
import '../../../utils/font.dart'; import '../../../utils/font.dart';
import '../base_button/stateful_base_button_ex.dart'; import '../base_button/base_value_button.dart';
import '../base_toolbar.dart'; import '../base_toolbar.dart';
class QuillToolbarFontSizeButton extends QuillToolbarStatefulBaseButton< class QuillToolbarFontSizeButton extends QuillToolbarBaseValueButton<
QuillToolbarFontSizeButtonOptions, QuillToolbarFontSizeButtonExtraOptions> { QuillToolbarFontSizeButtonOptions, QuillToolbarFontSizeButtonExtraOptions> {
QuillToolbarFontSizeButton({ QuillToolbarFontSizeButton({
required super.controller, required super.controller,
@ -28,12 +28,12 @@ class QuillToolbarFontSizeButton extends QuillToolbarStatefulBaseButton<
QuillToolbarFontSizeButtonState(); QuillToolbarFontSizeButtonState();
} }
class QuillToolbarFontSizeButtonState extends QuillToolbarBaseButtonState< class QuillToolbarFontSizeButtonState extends QuillToolbarBaseValueButtonState<
QuillToolbarFontSizeButton, QuillToolbarFontSizeButton,
QuillToolbarFontSizeButtonOptions, QuillToolbarFontSizeButtonOptions,
QuillToolbarFontSizeButtonExtraOptions> { QuillToolbarFontSizeButtonExtraOptions,
String> {
final _menuController = MenuController(); final _menuController = MenuController();
String _currentValue = '';
Map<String, String> get rawItemsMap { Map<String, String> get rawItemsMap {
final fontSizes = options.rawItemsMap ?? final fontSizes = options.rawItemsMap ??
@ -65,21 +65,9 @@ class QuillToolbarFontSizeButtonState extends QuillToolbarBaseButtonState<
} }
@override @override
void didChangeDependencies() { String get currentStateValue {
super.didChangeDependencies(); final attribute = controller.getSelectionStyle().attributes[options.attribute.key];
_currentValue = _defaultDisplayText; return attribute == null ? _defaultDisplayText : (_getKeyName(attribute.value) ?? _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? _getKeyName(dynamic value) { String? _getKeyName(dynamic value) {
@ -113,7 +101,7 @@ class QuillToolbarFontSizeButtonState extends QuillToolbarBaseButtonState<
options, options,
QuillToolbarFontSizeButtonExtraOptions( QuillToolbarFontSizeButtonExtraOptions(
controller: controller, controller: controller,
currentValue: _currentValue, currentValue: currentValue,
defaultDisplayText: _defaultDisplayText, defaultDisplayText: _defaultDisplayText,
context: context, context: context,
onPressed: _onDropdownButtonPressed, onPressed: _onDropdownButtonPressed,
@ -131,9 +119,9 @@ class QuillToolbarFontSizeButtonState extends QuillToolbarBaseButtonState<
final keyName = _getKeyName(newValue); final keyName = _getKeyName(newValue);
setState(() { setState(() {
if (keyName != context.loc.clear) { if (keyName != context.loc.clear) {
_currentValue = keyName ?? _defaultDisplayText; currentValue = keyName ?? _defaultDisplayText;
} else { } else {
_currentValue = _defaultDisplayText; currentValue = _defaultDisplayText;
} }
if (keyName != null) { if (keyName != null) {
controller.formatSelection( controller.formatSelection(
@ -193,7 +181,7 @@ class QuillToolbarFontSizeButtonState extends QuillToolbarBaseButtonState<
enabled: hasFinalWidth, enabled: hasFinalWidth,
wrapper: (child) => Expanded(child: child), wrapper: (child) => Expanded(child: child),
child: Text( child: Text(
getLabel(_currentValue) ?? '', getLabel(currentValue) ?? '',
overflow: options.labelOverflow, overflow: options.labelOverflow,
style: options.style ?? style: options.style ??
TextStyle( TextStyle(

@ -4,10 +4,10 @@ import '../../../l10n/extensions/localizations.dart';
import '../../../models/documents/attribute.dart'; import '../../../models/documents/attribute.dart';
import '../../../models/documents/style.dart'; import '../../../models/documents/style.dart';
import '../../../utils/widgets.dart'; import '../../../utils/widgets.dart';
import '../base_button/stateful_base_button_ex.dart'; import '../base_button/base_value_button.dart';
import '../base_toolbar.dart'; import '../base_toolbar.dart';
class QuillToolbarToggleCheckListButton extends QuillToolbarStatefulBaseButton< class QuillToolbarToggleCheckListButton extends QuillToolbarBaseValueButton<
QuillToolbarToggleCheckListButtonOptions, QuillToolbarToggleCheckListButtonOptions,
QuillToolbarToggleCheckListButtonExtraOptions> { QuillToolbarToggleCheckListButtonExtraOptions> {
const QuillToolbarToggleCheckListButton({ const QuillToolbarToggleCheckListButton({
@ -22,26 +22,16 @@ class QuillToolbarToggleCheckListButton extends QuillToolbarStatefulBaseButton<
} }
class QuillToolbarToggleCheckListButtonState class QuillToolbarToggleCheckListButtonState
extends QuillToolbarBaseButtonState< extends QuillToolbarBaseValueButtonState<
QuillToolbarToggleCheckListButton, QuillToolbarToggleCheckListButton,
QuillToolbarToggleCheckListButtonOptions, QuillToolbarToggleCheckListButtonOptions,
QuillToolbarToggleCheckListButtonExtraOptions> { QuillToolbarToggleCheckListButtonExtraOptions,
bool? _isToggled; bool> {
Style get _selectionStyle => controller.getSelectionStyle(); Style get _selectionStyle => controller.getSelectionStyle();
@override @override
void didChangeEditingValue() { bool get currentStateValue => _getIsToggled(_selectionStyle.attributes);
setState(() {
_isToggled = _getIsToggled(controller.getSelectionStyle().attributes);
});
}
@override
void initState() {
super.initState();
_isToggled = _getIsToggled(_selectionStyle.attributes);
}
bool _getIsToggled(Map<String, Attribute> attrs) { bool _getIsToggled(Map<String, Attribute> attrs) {
var attribute = controller.toolbarButtonToggler[Attribute.list.key]; var attribute = controller.toolbarButtonToggler[Attribute.list.key];
@ -60,14 +50,6 @@ class QuillToolbarToggleCheckListButtonState
attribute.value == Attribute.checked.value; attribute.value == Attribute.checked.value;
} }
@override
void didUpdateWidget(covariant QuillToolbarToggleCheckListButton oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.controller != controller) {
_isToggled = _getIsToggled(_selectionStyle.attributes);
}
}
@override @override
String get defaultTooltip => context.loc.checkedList; String get defaultTooltip => context.loc.checkedList;
@ -91,7 +73,7 @@ class QuillToolbarToggleCheckListButtonState
_toggleAttribute(); _toggleAttribute();
afterButtonPressed?.call(); afterButtonPressed?.call();
}, },
isToggled: _isToggled ?? false, isToggled: currentValue,
), ),
); );
} }
@ -102,7 +84,7 @@ class QuillToolbarToggleCheckListButtonState
Attribute.unchecked, Attribute.unchecked,
iconData, iconData,
options.fillColor, options.fillColor,
_isToggled, currentValue,
_toggleAttribute, _toggleAttribute,
afterButtonPressed, afterButtonPressed,
iconSize, iconSize,
@ -116,7 +98,7 @@ class QuillToolbarToggleCheckListButtonState
controller controller
..skipRequestKeyboard = !options.isShouldRequestKeyboard ..skipRequestKeyboard = !options.isShouldRequestKeyboard
..formatSelection( ..formatSelection(
_isToggled! currentValue
? Attribute.clone(Attribute.unchecked, null) ? Attribute.clone(Attribute.unchecked, null)
: Attribute.unchecked, : Attribute.unchecked,
); );

@ -6,7 +6,7 @@ import '../../../models/documents/attribute.dart';
import '../../../models/documents/style.dart'; import '../../../models/documents/style.dart';
import '../../../models/themes/quill_icon_theme.dart'; import '../../../models/themes/quill_icon_theme.dart';
import '../../../utils/widgets.dart'; import '../../../utils/widgets.dart';
import '../base_button/stateful_base_button_ex.dart'; import '../base_button/base_value_button.dart';
import '../base_toolbar.dart'; import '../base_toolbar.dart';
typedef ToggleStyleButtonBuilder = Widget Function( typedef ToggleStyleButtonBuilder = Widget Function(
@ -21,7 +21,7 @@ typedef ToggleStyleButtonBuilder = Widget Function(
QuillIconTheme? iconTheme, QuillIconTheme? iconTheme,
]); ]);
class QuillToolbarToggleStyleButton extends QuillToolbarStatefulBaseButton< class QuillToolbarToggleStyleButton extends QuillToolbarBaseValueButton<
QuillToolbarToggleStyleButtonOptions, QuillToolbarToggleStyleButtonOptions,
QuillToolbarToggleStyleButtonExtraOptions> { QuillToolbarToggleStyleButtonExtraOptions> {
const QuillToolbarToggleStyleButton({ const QuillToolbarToggleStyleButton({
@ -38,19 +38,16 @@ class QuillToolbarToggleStyleButton extends QuillToolbarStatefulBaseButton<
QuillToolbarToggleStyleButtonState(); QuillToolbarToggleStyleButtonState();
} }
class QuillToolbarToggleStyleButtonState extends QuillToolbarBaseButtonState< class QuillToolbarToggleStyleButtonState extends QuillToolbarBaseValueButtonState<
QuillToolbarToggleStyleButton, QuillToolbarToggleStyleButton,
QuillToolbarToggleStyleButtonOptions, QuillToolbarToggleStyleButtonOptions,
QuillToolbarToggleStyleButtonExtraOptions> { QuillToolbarToggleStyleButtonExtraOptions,
bool? _isToggled; bool> {
Style get _selectionStyle => controller.getSelectionStyle(); Style get _selectionStyle => controller.getSelectionStyle();
@override @override
void initState() { bool get currentStateValue => _getIsToggled(_selectionStyle.attributes);
super.initState();
_isToggled = _getIsToggled(_selectionStyle.attributes);
}
(String, IconData) get _defaultTooltipAndIconData { (String, IconData) get _defaultTooltipAndIconData {
switch (widget.attribute.key) { switch (widget.attribute.key) {
@ -127,7 +124,7 @@ class QuillToolbarToggleStyleButtonState extends QuillToolbarBaseButtonState<
context: context, context: context,
controller: controller, controller: controller,
onPressed: _onPressed, onPressed: _onPressed,
isToggled: _isToggled ?? false, isToggled: currentValue,
), ),
); );
} }
@ -138,7 +135,7 @@ class QuillToolbarToggleStyleButtonState extends QuillToolbarBaseButtonState<
widget.attribute, widget.attribute,
iconData, iconData,
options.fillColor, options.fillColor,
_isToggled, currentValue,
_toggleAttribute, _toggleAttribute,
afterButtonPressed, afterButtonPressed,
iconSize, 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<String, Attribute> attrs) { bool _getIsToggled(Map<String, Attribute> attrs) {
if (widget.attribute.key == Attribute.list.key || if (widget.attribute.key == Attribute.list.key ||
widget.attribute.key == Attribute.script.key || widget.attribute.key == Attribute.script.key ||
@ -175,11 +159,12 @@ class QuillToolbarToggleStyleButtonState extends QuillToolbarBaseButtonState<
} }
void _toggleAttribute() { void _toggleAttribute() {
controller.formatSelection( controller..formatSelection(
(_isToggled ?? false) currentValue
? Attribute.clone(widget.attribute, null) ? Attribute.clone(widget.attribute, null)
: widget.attribute, : widget.attribute,
); )
..selectStyle(widget.attribute, currentValue);
} }
} }

Loading…
Cancel
Save