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(
icon:Icons.ac_unit,
onTap: (){
onTap: () {
debugPrint('snowflake');
}
),
@ -113,21 +113,21 @@ QuillToolbar.basic(
customIcons: [
QuillCustomIcon(
icon:Icons.ac_unit,
onTap: (){
onTap: () {
debugPrint('snowflake1');
}
),
QuillCustomIcon(
icon:Icons.ac_unit,
onTap: (){
onTap: () {
debugPrint('snowflake2');
}
),
QuillCustomIcon(
icon:Icons.ac_unit,
onTap: (){
onTap: () {
debugPrint('snowflake3');
}
),

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

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

Loading…
Cancel
Save