|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
import '../../models/documents/attribute.dart';
|
|
|
|
import '../controller.dart';
|
|
|
|
import '../link_dialog.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));
|
|
|
|
}
|
|
|
|
}
|