1 Star 0 Fork 0

king/flutter-quill

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
.github
doc
configurations
readme
code_introduction.md
custom_embed_blocks.md
custom_toolbar.md
development_notes.md
migration.md
todo.md
translation.md
example
flutter_quill_extensions
flutter_quill_test
lib
packages
scripts
test
.gitignore
.metadata
.pubignore
CHANGELOG.md
CONTRIBUTING.md
LICENSE
README.md
analysis_options.yaml
l10n.yaml
pubspec.yaml
pubspec_overrides.yaml.disabled
克隆/下载
custom_embed_blocks.md 4.31 KB
一键复制 编辑 原始数据 按行查看 历史
Ellet 提交于 2年前 . Update README.md

Custom Embed Blocks

Sometimes you want to add some custom content inside your text, custom widgets inside of them. An example is adding notes to the text, or anything custom that you want to add in your text editor.

The only thing that you need is to add a CustomBlockEmbed and provider a builder for it to the embedBuilders parameter, to transform the data inside of the Custom Block into a widget!

Here is an example:

Starting with the CustomBlockEmbed, here we extend it and add the methods that are useful for the 'Note' widget, that will be the Document, used by the flutter_quill to render the rich text.

class NotesBlockEmbed extends CustomBlockEmbed {
  const NotesBlockEmbed(String value) : super(noteType, value);

  static const String noteType = 'notes';

  static NotesBlockEmbed fromDocument(Document document) =>
      NotesBlockEmbed(jsonEncode(document.toDelta().toJson()));

  Document get document => Document.fromJson(jsonDecode(data));
}

After that, we need to map this "notes" type into a widget. In that case, I used a ListTile with a text to show the plain text resume of the note, and the onTap function to edit the note. Don't forget to add this method to the QuillEditor after that!

class NotesEmbedBuilder extends EmbedBuilder {
  NotesEmbedBuilder({required this.addEditNote});

  Future<void> Function(BuildContext context, {Document? document}) addEditNote;

  @override
  String get key => 'notes';

  @override
  Widget build(
    BuildContext context,
    QuillController controller,
    Embed node,
    bool readOnly,
    bool inline,
    TextStyle textStyle,
  ) {
    final notes = NotesBlockEmbed(node.value.data).document;

    return Material(
      color: Colors.transparent,
      child: ListTile(
        title: Text(
          notes.toPlainText().replaceAll('\n', ' '),
          maxLines: 3,
          overflow: TextOverflow.ellipsis,
        ),
        leading: const Icon(Icons.notes),
        onTap: () => addEditNote(context, document: notes),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10),
          side: const BorderSide(color: Colors.grey),
        ),
      ),
    );
  }
}

And finally, we write the function to add/edit this note. The showDialog function shows the QuillEditor to edit the note, after the user ends the edition, we check if the document has something, and if it has, we add or edit the NotesBlockEmbed inside of a BlockEmbed.custom (this is a little detail that will not work if you don't pass the CustomBlockEmbed inside of a BlockEmbed.custom).

Future<void> _addEditNote(BuildContext context, {Document? document}) async {
  final isEditing = document != null;
  final quillEditorController = QuillController(
    document: document ?? Document(),
    selection: const TextSelection.collapsed(offset: 0),
  );

  await showDialog(
    context: context,
    builder: (context) => AlertDialog(
      titlePadding: const EdgeInsets.only(left: 16, top: 8),
      title: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text('${isEditing ? 'Edit' : 'Add'} note'),
          IconButton(
            onPressed: () => Navigator.of(context).pop(),
            icon: const Icon(Icons.close),
          )
        ],
      ),
      content: QuillEditor.basic(
        controller: quillEditorController,
        readOnly: false,
      ),
    ),
  );

  if (quillEditorController.document.isEmpty()) return;

  final block = BlockEmbed.custom(
    NotesBlockEmbed.fromDocument(quillEditorController.document),
  );
  final controller = _controller!;
  final index = controller.selection.baseOffset;
  final length = controller.selection.extentOffset - index;

  if (isEditing) {
    final offset = getEmbedNode(controller, controller.selection.start).offset;
    controller.replaceText(
        offset, 1, block, TextSelection.collapsed(offset: offset));
  } else {
    controller.replaceText(index, length, block, null);
  }
}

And voila, we have a custom widget inside of the rich text editor!

1

  1. For more info and a video example, see the PR of this feature
  2. For more details, check out this YouTube video
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/longliving/flutter-quill.git
git@gitee.com:longliving/flutter-quill.git
longliving
flutter-quill
flutter-quill
master

搜索帮助