From e8af5da73371d70cde414f517cd0681b4410d99f Mon Sep 17 00:00:00 2001 From: coolswood Date: Mon, 22 Apr 2024 14:51:35 +0300 Subject: [PATCH] feat: Display the current font size --- .../toolbar/buttons/font_size_button.dart | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/src/widgets/toolbar/buttons/font_size_button.dart b/lib/src/widgets/toolbar/buttons/font_size_button.dart index 17807c47..db789596 100644 --- a/lib/src/widgets/toolbar/buttons/font_size_button.dart +++ b/lib/src/widgets/toolbar/buttons/font_size_button.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:flutter_quill/flutter_quill.dart'; import '../../../../extensions.dart'; @@ -37,6 +38,7 @@ class QuillToolbarFontSizeButton extends StatefulWidget { class QuillToolbarFontSizeButtonState extends State { final _menuController = MenuController(); + Style get _selectionStyle => controller.getSelectionStyle(); String _currentValue = ''; QuillToolbarFontSizeButtonOptions get options { @@ -78,8 +80,44 @@ class QuillToolbarFontSizeButtonState _currentValue = _defaultDisplayText; } + @override + void didUpdateWidget(covariant QuillToolbarFontSizeButton oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.controller != controller) { + oldWidget.controller.removeListener(_didChangeEditingValue); + controller.addListener(_didChangeEditingValue); + _currentValue = _defaultDisplayText; + } + } + + @override + void initState() { + super.initState(); + controller.addListener(_didChangeEditingValue); + } + + void _didChangeEditingValue() { + final selectedFontSize = + _selectionStyle.attributes[Attribute.size.key]?.value; + + if (selectedFontSize == null) { + setState(() { + _currentValue = _defaultDisplayText; + }); + + return; + } + + setState(() { + _currentValue = selectedFontSize is double + ? selectedFontSize.toInt().toString() + : selectedFontSize; + }); + } + @override void dispose() { + controller.removeListener(_didChangeEditingValue); super.dispose(); } @@ -224,9 +262,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(