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.
123 lines
2.9 KiB
123 lines
2.9 KiB
4 years ago
|
import 'package:flutter/material.dart';
|
||
|
|
||
|
import '../../models/documents/attribute.dart';
|
||
|
import '../controller.dart';
|
||
|
import '../toolbar.dart';
|
||
|
import 'quill_icon_button.dart';
|
||
|
|
||
|
class LinkStyleButton extends StatefulWidget {
|
||
|
const LinkStyleButton({
|
||
|
required this.controller,
|
||
|
this.iconSize = kDefaultIconSize,
|
||
|
this.icon,
|
||
|
Key? key,
|
||
|
}) : super(key: key);
|
||
|
|
||
|
final QuillController controller;
|
||
|
final IconData? icon;
|
||
|
final double iconSize;
|
||
|
|
||
|
@override
|
||
|
_LinkStyleButtonState createState() => _LinkStyleButtonState();
|
||
|
}
|
||
|
|
||
|
class _LinkStyleButtonState extends State<LinkStyleButton> {
|
||
|
void _didChangeSelection() {
|
||
|
setState(() {});
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void initState() {
|
||
|
super.initState();
|
||
|
widget.controller.addListener(_didChangeSelection);
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void didUpdateWidget(covariant LinkStyleButton oldWidget) {
|
||
|
super.didUpdateWidget(oldWidget);
|
||
|
if (oldWidget.controller != widget.controller) {
|
||
|
oldWidget.controller.removeListener(_didChangeSelection);
|
||
|
widget.controller.addListener(_didChangeSelection);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void dispose() {
|
||
|
super.dispose();
|
||
|
widget.controller.removeListener(_didChangeSelection);
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
final theme = Theme.of(context);
|
||
|
final isEnabled = !widget.controller.selection.isCollapsed;
|
||
|
final pressedHandler = isEnabled ? () => _openLinkDialog(context) : null;
|
||
|
return QuillIconButton(
|
||
|
highlightElevation: 0,
|
||
|
hoverElevation: 0,
|
||
|
size: widget.iconSize * kIconButtonFactor,
|
||
|
icon: Icon(
|
||
|
widget.icon ?? Icons.link,
|
||
|
size: widget.iconSize,
|
||
|
color: isEnabled ? theme.iconTheme.color : theme.disabledColor,
|
||
|
),
|
||
|
fillColor: Theme.of(context).canvasColor,
|
||
|
onPressed: pressedHandler,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
void _openLinkDialog(BuildContext context) {
|
||
|
showDialog<String>(
|
||
|
context: context,
|
||
|
builder: (ctx) {
|
||
|
return const _LinkDialog();
|
||
|
},
|
||
|
).then(_linkSubmitted);
|
||
|
}
|
||
|
|
||
|
void _linkSubmitted(String? value) {
|
||
|
if (value == null || value.isEmpty) {
|
||
|
return;
|
||
|
}
|
||
|
widget.controller.formatSelection(LinkAttribute(value));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class _LinkDialog extends StatefulWidget {
|
||
|
const _LinkDialog({Key? key}) : super(key: key);
|
||
|
|
||
|
@override
|
||
|
_LinkDialogState createState() => _LinkDialogState();
|
||
|
}
|
||
|
|
||
|
class _LinkDialogState extends State<_LinkDialog> {
|
||
|
String _link = '';
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return AlertDialog(
|
||
|
content: TextField(
|
||
|
decoration: const InputDecoration(labelText: 'Paste a link'),
|
||
|
autofocus: true,
|
||
|
onChanged: _linkChanged,
|
||
|
),
|
||
|
actions: [
|
||
|
TextButton(
|
||
|
onPressed: _link.isNotEmpty ? _applyLink : null,
|
||
|
child: const Text('Apply'),
|
||
|
),
|
||
|
],
|
||
|
);
|
||
|
}
|
||
|
|
||
|
void _linkChanged(String value) {
|
||
|
setState(() {
|
||
|
_link = value;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
void _applyLink() {
|
||
|
Navigator.pop(context, _link);
|
||
|
}
|
||
|
}
|