dartlangeditorflutterflutter-appsflutter-examplesflutter-packageflutter-widgetquillquill-deltaquilljsreactquillrich-textrich-text-editorwysiwygwysiwyg-editor
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
297 lines
8.9 KiB
297 lines
8.9 KiB
2 years ago
|
import 'package:flutter/material.dart';
|
||
|
|
||
|
import '../../../../extensions.dart';
|
||
1 year ago
|
import '../../../extensions/quill_configurations_ext.dart';
|
||
1 year ago
|
import '../../../l10n/extensions/localizations.dart';
|
||
1 year ago
|
import '../../../models/config/toolbar/buttons/font_family_configurations.dart';
|
||
2 years ago
|
import '../../../models/documents/attribute.dart';
|
||
|
import '../../../models/documents/style.dart';
|
||
2 years ago
|
import '../../../models/themes/quill_icon_theme.dart';
|
||
1 year ago
|
import '../../quill/quill_controller.dart';
|
||
2 years ago
|
|
||
|
class QuillToolbarFontFamilyButton extends StatefulWidget {
|
||
|
QuillToolbarFontFamilyButton({
|
||
2 years ago
|
required this.controller,
|
||
1 year ago
|
required this.defaultDispalyText,
|
||
1 year ago
|
this.options = const QuillToolbarFontFamilyButtonOptions(),
|
||
2 years ago
|
super.key,
|
||
|
}) : assert(options.rawItemsMap?.isNotEmpty ?? (true)),
|
||
|
assert(
|
||
|
options.initialValue == null || options.initialValue!.isNotEmpty,
|
||
|
);
|
||
|
|
||
|
final QuillToolbarFontFamilyButtonOptions options;
|
||
|
|
||
1 year ago
|
final String defaultDispalyText;
|
||
|
|
||
2 years ago
|
/// Since we can't get the state from the instace of the widget for comparing
|
||
|
/// in [didUpdateWidget] then we will have to store reference here
|
||
|
final QuillController controller;
|
||
|
|
||
2 years ago
|
@override
|
||
1 year ago
|
QuillToolbarFontFamilyButtonState createState() =>
|
||
|
QuillToolbarFontFamilyButtonState();
|
||
2 years ago
|
}
|
||
|
|
||
1 year ago
|
class QuillToolbarFontFamilyButtonState
|
||
2 years ago
|
extends State<QuillToolbarFontFamilyButton> {
|
||
2 years ago
|
var _currentValue = '';
|
||
2 years ago
|
|
||
|
QuillToolbarFontFamilyButtonOptions get options {
|
||
|
return widget.options;
|
||
|
}
|
||
|
|
||
|
Style get _selectionStyle => controller.getSelectionStyle();
|
||
|
|
||
|
@override
|
||
|
void initState() {
|
||
|
super.initState();
|
||
|
_initState();
|
||
|
}
|
||
|
|
||
2 years ago
|
void _initState() {
|
||
|
_currentValue = _defaultDisplayText;
|
||
2 years ago
|
controller.addListener(_didChangeEditingValue);
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void dispose() {
|
||
2 years ago
|
controller.removeListener(_didChangeEditingValue);
|
||
2 years ago
|
super.dispose();
|
||
|
}
|
||
|
|
||
2 years ago
|
String get _defaultDisplayText {
|
||
1 year ago
|
return options.initialValue ?? widget.defaultDispalyText;
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
@override
|
||
|
void didUpdateWidget(covariant QuillToolbarFontFamilyButton oldWidget) {
|
||
|
super.didUpdateWidget(oldWidget);
|
||
2 years ago
|
if (oldWidget.controller == controller) {
|
||
2 years ago
|
return;
|
||
2 years ago
|
}
|
||
2 years ago
|
controller
|
||
|
..removeListener(_didChangeEditingValue)
|
||
|
..addListener(_didChangeEditingValue);
|
||
2 years ago
|
}
|
||
|
|
||
|
void _didChangeEditingValue() {
|
||
|
final attribute = _selectionStyle.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 {
|
||
1 year ago
|
final rawItemsMap =
|
||
|
context.quillSimpleToolbarConfigurations?.fontFamilyValues ??
|
||
|
options.rawItemsMap ??
|
||
|
{
|
||
|
'Sans Serif': 'sans-serif',
|
||
|
'Serif': 'serif',
|
||
|
'Monospace': 'monospace',
|
||
|
'Ibarra Real Nova': 'ibarra-real-nova',
|
||
|
'SquarePeg': 'square-peg',
|
||
|
'Nunito': 'nunito',
|
||
|
'Pacifico': 'pacifico',
|
||
|
'Roboto Mono': 'roboto-mono',
|
||
|
context.loc.clear: 'Clear'
|
||
|
};
|
||
2 years ago
|
return rawItemsMap;
|
||
|
}
|
||
|
|
||
|
String? _getKeyName(String value) {
|
||
|
for (final entry in rawItemsMap.entries) {
|
||
|
if (entry.value == value) {
|
||
|
return entry.key;
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
2 years ago
|
QuillController get controller {
|
||
2 years ago
|
return widget.controller;
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
double get iconSize {
|
||
2 years ago
|
final baseFontSize =
|
||
|
context.requireQuillToolbarBaseButtonOptions.globalIconSize;
|
||
2 years ago
|
final iconSize = options.iconSize;
|
||
2 years ago
|
return iconSize ?? baseFontSize;
|
||
|
}
|
||
|
|
||
|
VoidCallback? get afterButtonPressed {
|
||
|
return options.afterButtonPressed ??
|
||
|
context.requireQuillToolbarBaseButtonOptions.afterButtonPressed;
|
||
|
}
|
||
|
|
||
|
QuillIconTheme? get iconTheme {
|
||
|
return options.iconTheme ??
|
||
|
context.requireQuillToolbarBaseButtonOptions.iconTheme;
|
||
|
}
|
||
|
|
||
|
String get tooltip {
|
||
|
return options.tooltip ??
|
||
|
context.requireQuillToolbarBaseButtonOptions.tooltip ??
|
||
1 year ago
|
context.loc.fontFamily;
|
||
2 years ago
|
}
|
||
|
|
||
|
void _onPressed() {
|
||
|
_showMenu();
|
||
|
options.afterButtonPressed?.call();
|
||
2 years ago
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
final baseButtonConfigurations =
|
||
|
context.requireQuillToolbarBaseButtonOptions;
|
||
|
final childBuilder =
|
||
|
options.childBuilder ?? baseButtonConfigurations.childBuilder;
|
||
|
if (childBuilder != null) {
|
||
|
return childBuilder(
|
||
2 years ago
|
options.copyWith(
|
||
|
iconSize: iconSize,
|
||
|
rawItemsMap: rawItemsMap,
|
||
|
iconTheme: iconTheme,
|
||
|
tooltip: tooltip,
|
||
|
afterButtonPressed: afterButtonPressed,
|
||
|
),
|
||
2 years ago
|
QuillToolbarFontFamilyButtonExtraOptions(
|
||
|
currentValue: _currentValue,
|
||
|
defaultDisplayText: _defaultDisplayText,
|
||
2 years ago
|
controller: controller,
|
||
|
context: context,
|
||
|
onPressed: _onPressed,
|
||
2 years ago
|
),
|
||
|
);
|
||
|
}
|
||
|
return ConstrainedBox(
|
||
|
constraints: BoxConstraints.tightFor(
|
||
|
height: iconSize * 1.81,
|
||
|
width: options.width,
|
||
|
),
|
||
|
child: UtilityWidgets.maybeWidget(
|
||
2 years ago
|
enabled: tooltip.isNotEmpty || options.overrideTooltipByFontFamily,
|
||
2 years ago
|
wrapper: (child) {
|
||
2 years ago
|
var effectiveTooltip = tooltip;
|
||
2 years ago
|
if (options.overrideTooltipByFontFamily) {
|
||
|
effectiveTooltip = effectiveTooltip.isNotEmpty
|
||
|
? '$effectiveTooltip: $_currentValue'
|
||
1 year ago
|
: '${context.loc.font}: $_currentValue';
|
||
2 years ago
|
}
|
||
|
return Tooltip(message: effectiveTooltip, child: child);
|
||
|
},
|
||
|
child: RawMaterialButton(
|
||
|
visualDensity: VisualDensity.compact,
|
||
|
shape: RoundedRectangleBorder(
|
||
2 years ago
|
borderRadius: BorderRadius.circular(iconTheme?.borderRadius ?? 2),
|
||
2 years ago
|
),
|
||
|
fillColor: options.fillColor,
|
||
|
elevation: 0,
|
||
|
hoverElevation: options.hoverElevation,
|
||
|
highlightElevation: options.hoverElevation,
|
||
2 years ago
|
onPressed: _onPressed,
|
||
2 years ago
|
child: _buildContent(context),
|
||
|
),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
Future<void> _showMenu() async {
|
||
|
final popupMenuTheme = PopupMenuTheme.of(context);
|
||
|
final button = context.findRenderObject() as RenderBox;
|
||
|
final overlay = Overlay.of(context).context.findRenderObject() as RenderBox;
|
||
|
final position = RelativeRect.fromRect(
|
||
|
Rect.fromPoints(
|
||
|
button.localToGlobal(Offset.zero, ancestor: overlay),
|
||
|
button.localToGlobal(button.size.bottomLeft(Offset.zero),
|
||
|
ancestor: overlay),
|
||
|
),
|
||
|
Offset.zero & overlay.size,
|
||
|
);
|
||
|
final newValue = await showMenu<String>(
|
||
|
context: context,
|
||
|
elevation: 4,
|
||
|
items: [
|
||
|
for (final MapEntry<String, String> fontFamily in rawItemsMap.entries)
|
||
|
PopupMenuItem<String>(
|
||
|
key: ValueKey(fontFamily.key),
|
||
|
value: fontFamily.value,
|
||
|
height: options.itemHeight ?? kMinInteractiveDimension,
|
||
|
padding: options.itemPadding,
|
||
|
child: Text(
|
||
|
fontFamily.key.toString(),
|
||
|
style: TextStyle(
|
||
|
fontFamily:
|
||
|
options.renderFontFamilies ? fontFamily.value : null,
|
||
|
color: fontFamily.value == 'Clear'
|
||
|
? options.defaultItemColor
|
||
|
: null,
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
],
|
||
|
position: position,
|
||
|
shape: popupMenuTheme.shape,
|
||
|
color: popupMenuTheme.color,
|
||
|
);
|
||
2 years ago
|
if (!mounted) {
|
||
|
return;
|
||
|
}
|
||
2 years ago
|
if (newValue == null) {
|
||
|
return;
|
||
|
}
|
||
|
final keyName = _getKeyName(newValue);
|
||
|
setState(() {
|
||
|
_currentValue = keyName ?? _defaultDisplayText;
|
||
|
if (keyName != null) {
|
||
|
controller.formatSelection(
|
||
2 years ago
|
Attribute.fromKeyValue(
|
||
|
'font',
|
||
|
newValue == 'Clear' ? null : newValue,
|
||
|
),
|
||
2 years ago
|
);
|
||
|
options.onSelected?.call(newValue);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
Widget _buildContent(BuildContext context) {
|
||
|
final theme = Theme.of(context);
|
||
|
final hasFinalWidth = options.width != null;
|
||
|
return Padding(
|
||
|
padding: options.padding ?? const EdgeInsets.fromLTRB(10, 0, 0, 0),
|
||
|
child: Row(
|
||
|
mainAxisSize: !hasFinalWidth ? MainAxisSize.min : MainAxisSize.max,
|
||
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||
|
children: [
|
||
|
UtilityWidgets.maybeWidget(
|
||
|
enabled: hasFinalWidth,
|
||
|
wrapper: (child) => Expanded(child: child),
|
||
|
child: Text(
|
||
|
_currentValue,
|
||
|
maxLines: 1,
|
||
|
overflow: options.labelOverflow,
|
||
|
style: options.style ??
|
||
|
TextStyle(
|
||
|
fontSize: iconSize / 1.15,
|
||
2 years ago
|
color:
|
||
|
iconTheme?.iconUnselectedColor ?? theme.iconTheme.color,
|
||
2 years ago
|
),
|
||
|
),
|
||
|
),
|
||
|
const SizedBox(width: 3),
|
||
|
Icon(
|
||
|
Icons.arrow_drop_down,
|
||
|
size: iconSize / 1.15,
|
||
2 years ago
|
color: iconTheme?.iconUnselectedColor ?? theme.iconTheme.color,
|
||
2 years ago
|
)
|
||
|
],
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|