Simplify class QuillFontSizeButton

pull/817/head
X Code 3 years ago
parent fb0be15e2e
commit 2815833889
  1. 8
      README.md
  2. 6
      lib/src/widgets/toolbar.dart
  3. 58
      lib/src/widgets/toolbar/quill_font_size_button.dart

@ -100,7 +100,7 @@ To add an Icon, we should use a new QuillCustomIcon class
``` ```
QuillCustomIcon( QuillCustomIcon(
icon:Icons.ac_unit, icon:Icons.ac_unit,
onTap: (){ onTap: () {
debugPrint('snowflake'); debugPrint('snowflake');
} }
), ),
@ -113,21 +113,21 @@ QuillToolbar.basic(
customIcons: [ customIcons: [
QuillCustomIcon( QuillCustomIcon(
icon:Icons.ac_unit, icon:Icons.ac_unit,
onTap: (){ onTap: () {
debugPrint('snowflake1'); debugPrint('snowflake1');
} }
), ),
QuillCustomIcon( QuillCustomIcon(
icon:Icons.ac_unit, icon:Icons.ac_unit,
onTap: (){ onTap: () {
debugPrint('snowflake2'); debugPrint('snowflake2');
} }
), ),
QuillCustomIcon( QuillCustomIcon(
icon:Icons.ac_unit, icon:Icons.ac_unit,
onTap: (){ onTap: () {
debugPrint('snowflake3'); debugPrint('snowflake3');
} }
), ),

@ -198,10 +198,8 @@ class QuillToolbar extends StatelessWidget implements PreferredSizeWidget {
), ),
], ],
onSelected: (newSize) { onSelected: (newSize) {
if (newSize != null) { controller.formatSelection(
controller.formatSelection( Attribute.fromKeyValue('size', getFontSize(newSize)));
Attribute.fromKeyValue('size', getFontSize(newSize)));
}
}, },
rawItemsMap: fontSizes, rawItemsMap: fontSizes,
), ),

@ -6,7 +6,7 @@ import '../../models/themes/quill_icon_theme.dart';
import '../../utils/font.dart'; import '../../utils/font.dart';
import '../controller.dart'; import '../controller.dart';
class QuillFontSizeButton<T> extends StatefulWidget { class QuillFontSizeButton extends StatefulWidget {
const QuillFontSizeButton({ const QuillFontSizeButton({
required this.items, required this.items,
required this.rawItemsMap, required this.rawItemsMap,
@ -25,18 +25,18 @@ class QuillFontSizeButton<T> extends StatefulWidget {
final Color? fillColor; final Color? fillColor;
final double hoverElevation; final double hoverElevation;
final double highlightElevation; final double highlightElevation;
final List<PopupMenuEntry<T>> items; final List<PopupMenuEntry<String>> items;
final Map<String, String> rawItemsMap; final Map<String, String> rawItemsMap;
final ValueChanged<T> onSelected; final ValueChanged<String> onSelected;
final QuillIconTheme? iconTheme; final QuillIconTheme? iconTheme;
final Attribute attribute; final Attribute attribute;
final QuillController controller; final QuillController controller;
@override @override
_QuillFontSizeButtonState<T> createState() => _QuillFontSizeButtonState<T>(); _QuillFontSizeButtonState createState() => _QuillFontSizeButtonState();
} }
class _QuillFontSizeButtonState<T> extends State<QuillFontSizeButton<T>> { class _QuillFontSizeButtonState extends State<QuillFontSizeButton> {
static const defaultDisplayText = 'Size'; static const defaultDisplayText = 'Size';
String _currentValue = defaultDisplayText; String _currentValue = defaultDisplayText;
Style get _selectionStyle => widget.controller.getSelectionStyle(); Style get _selectionStyle => widget.controller.getSelectionStyle();
@ -54,7 +54,7 @@ class _QuillFontSizeButtonState<T> extends State<QuillFontSizeButton<T>> {
} }
@override @override
void didUpdateWidget(covariant QuillFontSizeButton<T> oldWidget) { void didUpdateWidget(covariant QuillFontSizeButton oldWidget) {
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
if (oldWidget.controller != widget.controller) { if (oldWidget.controller != widget.controller) {
oldWidget.controller.removeListener(_didChangeEditingValue); oldWidget.controller.removeListener(_didChangeEditingValue);
@ -63,24 +63,27 @@ class _QuillFontSizeButtonState<T> extends State<QuillFontSizeButton<T>> {
} }
void _didChangeEditingValue() { void _didChangeEditingValue() {
setState(() => _currentValue = _getKeyName(_selectionStyle.attributes));
}
String _getKeyName(Map<String, Attribute> attrs) {
if (widget.attribute.key != Attribute.size.key) { if (widget.attribute.key != Attribute.size.key) {
return defaultDisplayText; return;
} }
final attribute = attrs[widget.attribute.key]; final attribute = _selectionStyle.attributes[widget.attribute.key];
if (attribute == null) { if (attribute == null) {
return defaultDisplayText; return;
} }
return widget.rawItemsMap.entries final keyName = _getKeyName(attribute.value);
.firstWhere( if (keyName == null) {
(element) => return;
getFontSize(element.value) == getFontSize(attribute.value), }
orElse: () => widget.rawItemsMap.entries.first) setState(() => _currentValue = keyName);
.key; }
String? _getKeyName(dynamic value) {
for (final entry in widget.rawItemsMap.entries) {
if (getFontSize(entry.value) == getFontSize(value)) {
return entry.key;
}
}
return null;
} }
@override @override
@ -115,7 +118,7 @@ class _QuillFontSizeButtonState<T> extends State<QuillFontSizeButton<T>> {
), ),
Offset.zero & overlay.size, Offset.zero & overlay.size,
); );
showMenu<T>( showMenu<String>(
context: context, context: context,
elevation: 4, elevation: 4,
items: widget.items, items: widget.items,
@ -123,15 +126,16 @@ class _QuillFontSizeButtonState<T> extends State<QuillFontSizeButton<T>> {
shape: popupMenuTheme.shape, shape: popupMenuTheme.shape,
color: popupMenuTheme.color, color: popupMenuTheme.color,
).then((newValue) { ).then((newValue) {
if (!mounted) return null; if (!mounted) return;
if (newValue == null) { if (newValue == null) {
return null; return;
}
final keyName = _getKeyName(newValue);
if (keyName == null) {
return;
} }
setState(() { setState(() {
_currentValue = widget.rawItemsMap.entries _currentValue = keyName;
.firstWhere((element) => element.value == newValue,
orElse: () => widget.rawItemsMap.entries.first)
.key;
widget.onSelected(newValue); widget.onSelected(newValue);
}); });
}); });

Loading…
Cancel
Save