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.
534 lines
16 KiB
534 lines
16 KiB
2 years ago
|
// ignore_for_file: use_build_context_synchronously
|
||
|
|
||
2 years ago
|
import 'dart:math' as math;
|
||
|
import 'dart:ui';
|
||
|
|
||
|
import 'package:flutter/foundation.dart';
|
||
|
import 'package:flutter/material.dart';
|
||
|
import 'package:flutter_quill/extensions.dart';
|
||
2 years ago
|
import 'package:flutter_quill/flutter_quill.dart';
|
||
2 years ago
|
import 'package:flutter_quill/translations.dart';
|
||
|
import 'package:image_picker/image_picker.dart';
|
||
|
|
||
2 years ago
|
import '../../models/config/toolbar/buttons/media_button.dart';
|
||
2 years ago
|
import '../embed_types.dart';
|
||
2 years ago
|
import 'utils/image_video_utils.dart';
|
||
2 years ago
|
|
||
|
/// Widget which combines [ImageButton] and [VideButton] widgets. This widget
|
||
|
/// has more customization and uses dialog similar to one which is used
|
||
|
/// on [http://quilljs.com].
|
||
2 years ago
|
class QuillToolbarMediaButton extends StatelessWidget {
|
||
|
QuillToolbarMediaButton({
|
||
2 years ago
|
required this.controller,
|
||
2 years ago
|
required this.options,
|
||
|
super.key,
|
||
|
}) : assert(options.type == QuillMediaType.image,
|
||
|
'Video selection is not supported yet');
|
||
2 years ago
|
|
||
|
final QuillController controller;
|
||
2 years ago
|
final QuillToolbarMediaButtonOptions options;
|
||
2 years ago
|
|
||
2 years ago
|
double _iconSize(BuildContext context) {
|
||
|
final baseFontSize = baseButtonExtraOptions(context).globalIconSize;
|
||
|
final iconSize = options.iconSize;
|
||
|
return iconSize ?? baseFontSize;
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
VoidCallback? _afterButtonPressed(BuildContext context) {
|
||
|
return options.afterButtonPressed ??
|
||
|
baseButtonExtraOptions(context).afterButtonPressed;
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
QuillIconTheme? _iconTheme(BuildContext context) {
|
||
|
return options.iconTheme ?? baseButtonExtraOptions(context).iconTheme;
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
QuillToolbarBaseButtonOptions baseButtonExtraOptions(BuildContext context) {
|
||
|
return context.requireQuillToolbarBaseButtonOptions;
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
(IconData, String) get _defaultData {
|
||
|
switch (options.type) {
|
||
|
case QuillMediaType.image:
|
||
|
return (Icons.perm_media, 'Photo media button');
|
||
|
case QuillMediaType.video:
|
||
|
throw UnsupportedError('The video is not supported yet.');
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
IconData _iconData(BuildContext context) {
|
||
|
return options.iconData ??
|
||
|
baseButtonExtraOptions(context).iconData ??
|
||
|
_defaultData.$1;
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
String _tooltip(BuildContext context) {
|
||
|
return options.tooltip ??
|
||
|
baseButtonExtraOptions(context).tooltip ??
|
||
|
_defaultData.$2;
|
||
|
// ('Camera'.i18n);
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
void _sharedOnPressed(BuildContext context) {
|
||
|
_onPressedHandler(context);
|
||
|
_afterButtonPressed(context);
|
||
|
}
|
||
2 years ago
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
2 years ago
|
final tooltip = _tooltip(context);
|
||
|
final iconSize = _iconSize(context);
|
||
|
final iconData = _iconData(context);
|
||
|
final childBuilder =
|
||
|
options.childBuilder ?? baseButtonExtraOptions(context).childBuilder;
|
||
|
final iconTheme = _iconTheme(context);
|
||
|
|
||
|
if (childBuilder != null) {
|
||
|
return childBuilder(
|
||
|
QuillToolbarMediaButtonOptions(
|
||
|
type: options.type,
|
||
|
onMediaPickedCallback: options.onMediaPickedCallback,
|
||
|
onImagePickCallback: options.onImagePickCallback,
|
||
|
onVideoPickCallback: options.onVideoPickCallback,
|
||
|
iconData: iconData,
|
||
|
afterButtonPressed: _afterButtonPressed(context),
|
||
|
autovalidateMode: options.autovalidateMode,
|
||
|
childrenSpacing: options.childrenSpacing,
|
||
|
dialogBarrierColor: options.dialogBarrierColor,
|
||
|
dialogTheme: options.dialogTheme,
|
||
|
filePickImpl: options.filePickImpl,
|
||
|
fillColor: options.fillColor,
|
||
|
galleryButtonText: options.galleryButtonText,
|
||
|
iconTheme: iconTheme,
|
||
|
iconSize: iconSize,
|
||
|
hintText: options.hintText,
|
||
|
labelText: options.labelText,
|
||
|
submitButtonSize: options.submitButtonSize,
|
||
|
linkButtonText: options.linkButtonText,
|
||
|
mediaFilePicker: options.mediaFilePicker,
|
||
|
submitButtonText: options.submitButtonText,
|
||
|
validationMessage: options.validationMessage,
|
||
|
webImagePickImpl: options.webImagePickImpl,
|
||
|
webVideoPickImpl: options.webVideoPickImpl,
|
||
|
tooltip: options.tooltip,
|
||
|
),
|
||
|
QuillToolbarMediaButtonExtraOptions(
|
||
|
context: context,
|
||
|
controller: controller,
|
||
|
onPressed: () => _sharedOnPressed(context),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
|
||
2 years ago
|
final theme = Theme.of(context);
|
||
2 years ago
|
|
||
|
final iconColor =
|
||
|
options.iconTheme?.iconUnselectedColor ?? theme.iconTheme.color;
|
||
|
final iconFillColor = options.iconTheme?.iconUnselectedFillColor ??
|
||
|
options.fillColor ??
|
||
|
theme.canvasColor;
|
||
2 years ago
|
|
||
2 years ago
|
return QuillToolbarIconButton(
|
||
2 years ago
|
icon: Icon(iconData, size: iconSize, color: iconColor),
|
||
2 years ago
|
tooltip: tooltip,
|
||
|
highlightElevation: 0,
|
||
|
hoverElevation: 0,
|
||
|
size: iconSize * 1.77,
|
||
|
fillColor: iconFillColor,
|
||
|
borderRadius: iconTheme?.borderRadius ?? 2,
|
||
2 years ago
|
onPressed: () => _sharedOnPressed(context),
|
||
2 years ago
|
);
|
||
|
}
|
||
|
|
||
|
Future<void> _onPressedHandler(BuildContext context) async {
|
||
2 years ago
|
if (options.onMediaPickedCallback == null) {
|
||
2 years ago
|
_inputLink(context);
|
||
2 years ago
|
return;
|
||
|
}
|
||
|
final mediaSource = await showDialog<MediaPickSetting>(
|
||
|
context: context,
|
||
|
builder: (_) => MediaSourceSelectorDialog(
|
||
2 years ago
|
dialogTheme: options.dialogTheme,
|
||
|
galleryButtonText: options.galleryButtonText,
|
||
|
linkButtonText: options.linkButtonText,
|
||
2 years ago
|
),
|
||
|
);
|
||
|
if (mediaSource == null) {
|
||
|
return;
|
||
|
}
|
||
|
switch (mediaSource) {
|
||
2 years ago
|
case MediaPickSetting.gallery:
|
||
2 years ago
|
await _pickImage();
|
||
|
break;
|
||
2 years ago
|
case MediaPickSetting.link:
|
||
2 years ago
|
_inputLink(context);
|
||
|
break;
|
||
2 years ago
|
case MediaPickSetting.camera:
|
||
2 years ago
|
await ImageVideoUtils.handleImageButtonTap(
|
||
|
context,
|
||
|
controller,
|
||
|
ImageSource.camera,
|
||
2 years ago
|
options.onImagePickCallback,
|
||
|
filePickImpl: options.filePickImpl,
|
||
|
webImagePickImpl: options.webImagePickImpl,
|
||
2 years ago
|
);
|
||
|
break;
|
||
2 years ago
|
case MediaPickSetting.video:
|
||
2 years ago
|
await ImageVideoUtils.handleVideoButtonTap(
|
||
|
context,
|
||
|
controller,
|
||
|
ImageSource.camera,
|
||
2 years ago
|
options.onVideoPickCallback,
|
||
|
filePickImpl: options.filePickImpl,
|
||
|
webVideoPickImpl: options.webVideoPickImpl,
|
||
2 years ago
|
);
|
||
|
break;
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
|
Future<void> _pickImage() async {
|
||
|
if (!(kIsWeb || isMobile() || isDesktop())) {
|
||
|
throw UnsupportedError(
|
||
2 years ago
|
'Unsupported target platform: ${defaultTargetPlatform.name}',
|
||
|
);
|
||
2 years ago
|
}
|
||
|
|
||
|
final mediaFileUrl = await _pickMediaFileUrl();
|
||
|
|
||
|
if (mediaFileUrl != null) {
|
||
|
final index = controller.selection.baseOffset;
|
||
|
final length = controller.selection.extentOffset - index;
|
||
|
controller.replaceText(
|
||
2 years ago
|
index,
|
||
|
length,
|
||
|
BlockEmbed.image(mediaFileUrl),
|
||
|
null,
|
||
|
);
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
|
Future<MediaFileUrl?> _pickMediaFileUrl() async {
|
||
2 years ago
|
final mediaFile = await options.mediaFilePicker?.call(options.type);
|
||
|
return mediaFile != null
|
||
|
? options.onMediaPickedCallback?.call(mediaFile)
|
||
|
: null;
|
||
2 years ago
|
}
|
||
|
|
||
|
void _inputLink(BuildContext context) {
|
||
|
showDialog<String>(
|
||
|
context: context,
|
||
|
builder: (_) => MediaLinkDialog(
|
||
2 years ago
|
dialogTheme: options.dialogTheme,
|
||
|
labelText: options.labelText,
|
||
|
hintText: options.hintText,
|
||
|
buttonText: options.submitButtonText,
|
||
|
buttonSize: options.submitButtonSize,
|
||
|
childrenSpacing: options.childrenSpacing,
|
||
|
autovalidateMode: options.autovalidateMode,
|
||
|
validationMessage: options.validationMessage,
|
||
2 years ago
|
),
|
||
|
).then(_linkSubmitted);
|
||
|
}
|
||
|
|
||
|
void _linkSubmitted(String? value) {
|
||
|
if (value != null && value.isNotEmpty) {
|
||
|
final index = controller.selection.baseOffset;
|
||
|
final length = controller.selection.extentOffset - index;
|
||
2 years ago
|
final data = options.type.isImage
|
||
|
? BlockEmbed.image(value)
|
||
|
: BlockEmbed.video(value);
|
||
2 years ago
|
controller.replaceText(index, length, data, null);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Provides a dialog for input link to media resource.
|
||
|
class MediaLinkDialog extends StatefulWidget {
|
||
|
const MediaLinkDialog({
|
||
2 years ago
|
super.key,
|
||
2 years ago
|
this.link,
|
||
|
this.dialogTheme,
|
||
|
this.childrenSpacing = 16.0,
|
||
|
this.labelText,
|
||
|
this.hintText,
|
||
|
this.buttonText,
|
||
|
this.buttonSize,
|
||
|
this.autovalidateMode = AutovalidateMode.disabled,
|
||
|
this.validationMessage,
|
||
2 years ago
|
}) : assert(childrenSpacing > 0);
|
||
2 years ago
|
|
||
|
final String? link;
|
||
|
final QuillDialogTheme? dialogTheme;
|
||
|
|
||
|
/// The margin between child widgets in the dialog.
|
||
|
final double childrenSpacing;
|
||
|
|
||
|
/// The text of label in link add mode.
|
||
|
final String? labelText;
|
||
|
|
||
|
/// The hint text for link [TextField].
|
||
|
final String? hintText;
|
||
|
|
||
|
/// The text of the submit button.
|
||
|
final String? buttonText;
|
||
|
|
||
|
/// The size of dialog buttons.
|
||
|
final Size? buttonSize;
|
||
|
|
||
|
final AutovalidateMode autovalidateMode;
|
||
|
final String? validationMessage;
|
||
|
|
||
|
@override
|
||
|
State<MediaLinkDialog> createState() => _MediaLinkDialogState();
|
||
|
}
|
||
|
|
||
|
class _MediaLinkDialogState extends State<MediaLinkDialog> {
|
||
|
final _linkFocus = FocusNode();
|
||
|
final _linkController = TextEditingController();
|
||
|
|
||
|
@override
|
||
|
void dispose() {
|
||
|
_linkFocus.dispose();
|
||
|
_linkController.dispose();
|
||
|
super.dispose();
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
final constraints = widget.dialogTheme?.linkDialogConstraints ??
|
||
|
() {
|
||
2 years ago
|
final size = MediaQuery.sizeOf(context);
|
||
|
final maxWidth = kIsWeb ? size.width / 4 : size.width - 80;
|
||
2 years ago
|
return BoxConstraints(maxWidth: maxWidth, maxHeight: 80);
|
||
|
}();
|
||
|
|
||
|
final buttonStyle = widget.buttonSize != null
|
||
|
? Theme.of(context)
|
||
|
.elevatedButtonTheme
|
||
|
.style
|
||
|
?.copyWith(fixedSize: MaterialStatePropertyAll(widget.buttonSize))
|
||
|
: widget.dialogTheme?.buttonStyle;
|
||
|
|
||
|
final isWrappable = widget.dialogTheme?.isWrappable ?? false;
|
||
|
|
||
|
final children = [
|
||
|
Text(widget.labelText ?? 'Enter media'.i18n),
|
||
|
UtilityWidgets.maybeWidget(
|
||
|
enabled: !isWrappable,
|
||
|
wrapper: (child) => Expanded(
|
||
|
child: child,
|
||
|
),
|
||
|
child: Padding(
|
||
|
padding: EdgeInsets.symmetric(horizontal: widget.childrenSpacing),
|
||
|
child: TextFormField(
|
||
|
controller: _linkController,
|
||
|
focusNode: _linkFocus,
|
||
|
style: widget.dialogTheme?.inputTextStyle,
|
||
|
keyboardType: TextInputType.url,
|
||
|
textInputAction: TextInputAction.done,
|
||
|
decoration: InputDecoration(
|
||
|
labelStyle: widget.dialogTheme?.labelTextStyle,
|
||
|
hintText: widget.hintText,
|
||
|
),
|
||
|
autofocus: true,
|
||
|
autovalidateMode: widget.autovalidateMode,
|
||
|
validator: _validateLink,
|
||
|
onChanged: _linkChanged,
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
ElevatedButton(
|
||
|
onPressed: _canPress() ? _submitLink : null,
|
||
|
style: buttonStyle,
|
||
|
child: Text(widget.buttonText ?? 'Ok'.i18n),
|
||
|
),
|
||
|
];
|
||
|
|
||
|
return Dialog(
|
||
|
backgroundColor: widget.dialogTheme?.dialogBackgroundColor,
|
||
|
shape: widget.dialogTheme?.shape ??
|
||
|
DialogTheme.of(context).shape ??
|
||
|
RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
|
||
|
child: ConstrainedBox(
|
||
|
constraints: constraints,
|
||
|
child: Padding(
|
||
|
padding:
|
||
|
widget.dialogTheme?.linkDialogPadding ?? const EdgeInsets.all(16),
|
||
2 years ago
|
child: Form(
|
||
|
child: isWrappable
|
||
|
? Wrap(
|
||
|
alignment: WrapAlignment.center,
|
||
|
crossAxisAlignment: WrapCrossAlignment.center,
|
||
|
runSpacing: widget.dialogTheme?.runSpacing ?? 0.0,
|
||
|
children: children,
|
||
|
)
|
||
|
: Row(
|
||
|
children: children,
|
||
|
),
|
||
|
),
|
||
2 years ago
|
),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
bool _canPress() => _validateLink(_linkController.text) == null;
|
||
|
|
||
|
void _linkChanged(String value) {
|
||
|
setState(() {
|
||
|
_linkController.text = value;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
void _submitLink() => Navigator.pop(context, _linkController.text);
|
||
|
|
||
|
String? _validateLink(String? value) {
|
||
|
if ((value?.isEmpty ?? false) ||
|
||
2 years ago
|
!AutoFormatMultipleLinksRule.oneLineLinkRegExp.hasMatch(value!)) {
|
||
2 years ago
|
return widget.validationMessage ?? 'That is not a valid URL';
|
||
|
}
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Media souce selector.
|
||
|
class MediaSourceSelectorDialog extends StatelessWidget {
|
||
|
const MediaSourceSelectorDialog({
|
||
2 years ago
|
super.key,
|
||
2 years ago
|
this.dialogTheme,
|
||
|
this.galleryButtonText,
|
||
|
this.linkButtonText,
|
||
2 years ago
|
});
|
||
2 years ago
|
|
||
|
final QuillDialogTheme? dialogTheme;
|
||
|
|
||
|
/// The text of the gallery button [MediaSourceSelectorDialog].
|
||
|
final String? galleryButtonText;
|
||
|
|
||
|
/// The text of the link button [MediaSourceSelectorDialog].
|
||
|
final String? linkButtonText;
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
final constraints = dialogTheme?.mediaSelectorDialogConstraints ??
|
||
|
() {
|
||
2 years ago
|
final size = MediaQuery.sizeOf(context);
|
||
2 years ago
|
double maxWidth, maxHeight;
|
||
|
if (kIsWeb) {
|
||
2 years ago
|
maxWidth = size.width / 7;
|
||
|
maxHeight = size.height / 7;
|
||
2 years ago
|
} else {
|
||
2 years ago
|
maxWidth = size.width - 80;
|
||
2 years ago
|
maxHeight = maxWidth / 2;
|
||
|
}
|
||
|
return BoxConstraints(maxWidth: maxWidth, maxHeight: maxHeight);
|
||
|
}();
|
||
|
|
||
|
final shape = dialogTheme?.shape ??
|
||
|
DialogTheme.of(context).shape ??
|
||
|
RoundedRectangleBorder(borderRadius: BorderRadius.circular(4));
|
||
|
|
||
|
return Dialog(
|
||
|
backgroundColor: dialogTheme?.dialogBackgroundColor,
|
||
|
shape: shape,
|
||
|
child: ConstrainedBox(
|
||
|
constraints: constraints,
|
||
|
child: Padding(
|
||
|
padding: dialogTheme?.mediaSelectorDialogPadding ??
|
||
|
const EdgeInsets.all(16),
|
||
|
child: Row(
|
||
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||
|
children: [
|
||
|
Expanded(
|
||
|
child: TextButtonWithIcon(
|
||
|
icon: Icons.collections,
|
||
|
label: galleryButtonText ?? 'Gallery'.i18n,
|
||
|
onPressed: () =>
|
||
2 years ago
|
Navigator.pop(context, MediaPickSetting.gallery),
|
||
2 years ago
|
),
|
||
|
),
|
||
|
const SizedBox(width: 10),
|
||
|
Expanded(
|
||
|
child: TextButtonWithIcon(
|
||
|
icon: Icons.link,
|
||
|
label: linkButtonText ?? 'Link'.i18n,
|
||
|
onPressed: () =>
|
||
2 years ago
|
Navigator.pop(context, MediaPickSetting.link),
|
||
2 years ago
|
),
|
||
|
)
|
||
|
],
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class TextButtonWithIcon extends StatelessWidget {
|
||
|
const TextButtonWithIcon({
|
||
|
required this.label,
|
||
|
required this.icon,
|
||
|
required this.onPressed,
|
||
|
this.textStyle,
|
||
2 years ago
|
super.key,
|
||
|
});
|
||
2 years ago
|
|
||
|
final String label;
|
||
|
final IconData icon;
|
||
|
final VoidCallback onPressed;
|
||
|
final TextStyle? textStyle;
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
final theme = Theme.of(context);
|
||
|
final scale = MediaQuery.maybeOf(context)?.textScaleFactor ?? 1;
|
||
|
final gap = scale <= 1 ? 8.0 : lerpDouble(8, 4, math.min(scale - 1, 1))!;
|
||
|
final buttonStyle = TextButtonTheme.of(context).style;
|
||
|
final shape = buttonStyle?.shape?.resolve({}) ??
|
||
|
const RoundedRectangleBorder(
|
||
2 years ago
|
borderRadius: BorderRadius.all(
|
||
|
Radius.circular(4),
|
||
|
),
|
||
|
);
|
||
2 years ago
|
return Material(
|
||
|
shape: shape,
|
||
|
textStyle: textStyle ??
|
||
|
theme.textButtonTheme.style?.textStyle?.resolve({}) ??
|
||
|
theme.textTheme.labelLarge,
|
||
|
elevation: buttonStyle?.elevation?.resolve({}) ?? 0,
|
||
|
child: InkWell(
|
||
|
customBorder: shape,
|
||
|
onTap: onPressed,
|
||
|
child: Padding(
|
||
|
padding: const EdgeInsets.all(16),
|
||
|
child: Column(
|
||
|
mainAxisSize: MainAxisSize.min,
|
||
|
children: <Widget>[
|
||
|
Icon(icon),
|
||
|
SizedBox(height: gap),
|
||
|
Flexible(child: Text(label)),
|
||
|
],
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Default file picker.
|
||
2 years ago
|
// Future<QuillFile?> _defaultMediaPicker(QuillMediaType mediaType) async {
|
||
|
// final pickedFile = mediaType.isImage
|
||
|
// ? await ImagePicker().pickImage(source: ImageSource.gallery)
|
||
|
// : await ImagePicker().pickVideo(source: ImageSource.gallery);
|
||
|
|
||
|
// if (pickedFile != null) {
|
||
|
// return QuillFile(
|
||
|
// name: pickedFile.name,
|
||
|
// path: pickedFile.path,
|
||
|
// bytes: await pickedFile.readAsBytes(),
|
||
|
// );
|
||
|
// }
|
||
|
|
||
|
// return null;
|
||
|
// }
|