【问题标题】:Flutter-Quill editor not working inside a scrollable formFlutter-Quill 编辑器无法在可滚动表单中工作
【发布时间】:2021-09-21 20:37:20
【问题描述】:

我想将我的QuillEditor 放在我的表单中,而不是在我的应用程序的专用页面上。

我尝试在SingleChildScrollView 中使用ColumnListView 小部件,但结果是一样的:

  • 由于没有文本,编辑器很小,我希望它被“扩展”(我知道它不能在卷轴内,但你明白了);
  • 文本溢出不会使页面相应滚动,因此用户看不到他们正在输入的内容。

这里有一些代码:

return Scaffold(
  appBar: AppBar(
    title: Text("Reply form"),
    actions: [
      IconButton(
        onPressed: () {},
        icon: Icon(Icons.send),
        tooltip: "Send",
      )
    ],
  ),
  body: SafeArea(
    child: Form(
      child: ListView(
        children: [
          Text("From: test@my.org"),
          SizedBox(height: 8),
          chipInputField(
            label: "To",
            onChanged: (List<Object?> o) {},
            initialValue: ["first@email.it"],
          ),
          chipInputField(
            label: "Cc",
            onChanged: (List<Object?> o) {},
            initialValue: ["second@email.it"],
          ),
          chipInputField(
            label: "Bcc",
            onChanged: (List<Object?> o) {},
            initialValue: ["third@email.it"],
          ),
          QuillEditor(
            controller: quillController,
            scrollable: true,
            scrollController: ScrollController(),
            focusNode: FocusNode(),
            padding: EdgeInsets.all(5),
            autoFocus: true,
            readOnly: false,
            expands: false,
            placeholder: "compose_email",
          ),
          QuillToolbar.basic(
            controller: quillController,
            showUnderLineButton: false,
            showStrikeThrough: false,
            showColorButton: false,
            showBackgroundColorButton: false,
            showListCheck: false,
            showIndent: false,
          ),
        ],
      ),
    ),
  ),
);

第一个问题:撰写邮件部分没有在应用页面内展开

第二个问题:只要我写一些文字,小部件就不会滚动

编辑:我使用的是 Flutter 2.2.3 和 flutter_quill: 1.3.3

【问题讨论】:

  • 请指定您尝试实现此目的的 Flutter 和 Quill 版本?
  • 嗨@RaguSwaminathan 我已经更新了问题
  • @niccord 你还在寻找答案吗?
  • 是的,我是。任何帮助将不胜感激。

标签: flutter flutter-layout flutter-dependencies


【解决方案1】:

因此,在多次尝试解决这个问题之后,我意识到以下问题并提出了一个可能对您有所帮助的解决方案。

具有maxlines: null 的列表视图中的textField 将自动滚动列表视图而没有问题,但由于某种原因,flutter_quill 有一些问题

现在我的工作如下(完整代码将在底部):

  1. 首先我们在具有这种形式的小部件的状态类中定义几个变量
final quill.QuillController quillController = quill.QuillController.basic();
final FocusNode editorFocusNode = FocusNode();
bool isToolBarVisible = true;

您已经定义了控制器,但对于其他 2 个属性,您需要它们根据焦点节点显示/隐藏工具栏。

  1. 我们不使用简单的 ListView,而是使用 customScrollView,这使我们可以创建更复杂的列表视图,它使用 slivers(sliver 是可滚动的小部件,您可以了解更多信息about it
Form(
          child: CustomScrollView(
            slivers: [
              SliverAppBar(
                pinned: true,
                toolbarHeight: isToolBarVisible ? 50 : 0,
                title:
                    Visibility(visible: isToolBarVisible, child: getToolBar()),
              ),
              SliverToBoxAdapter(
                child: Column(
                  children: getTextFields(),
                ),
              ),
              SliverFillRemaining(
                hasScrollBody: true,
                child: getEditor(),
              ),
            ],
          ),

使用 sliver 应用栏我们可以固定 quill 工具栏,使用 sliver 到框适配器我们可以添加普通文本字段,然后我们使用 sliverFillRemaining 来扩展 quill 编辑器。

以下是子功能:

 Widget getToolBar() {
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: quill.QuillToolbar.basic(
        controller: quillController,
        showUnderLineButton: false,
        showStrikeThrough: false,
        showColorButton: false,
        showBackgroundColorButton: false,
        showListCheck: false,
        showIndent: false,
      ),
    );
  }

  List<Widget> getTextFields() {
    return [
      Text("From: test@my.org"),
      SizedBox(height: 8),
      TextField(
        decoration: InputDecoration(labelText: "To"),
      ),
      TextField(
        decoration: InputDecoration(labelText: "Cc"),
      ),
      TextField(
        decoration: InputDecoration(labelText: "Bcc"),
      ),
    ];
  }

  Widget getEditor() {
    return QuillEditor(
      controller: quillController,
      scrollable: true,
      scrollController: ScrollController(),
      focusNode: editorFocusNode,
      padding: EdgeInsets.all(5),
      autoFocus: true,
      readOnly: false,
      expands: false,
      placeholder: "compose_email",
    );
  }

用你的chipInputField替换普通的textFields

最后是完整的代码:

import 'package:flutter/material.dart';
import 'package:flutter_quill/flutter_quill.dart' as quill;
import 'package:flutter_quill/widgets/editor.dart';

class FlutterQuillForm extends StatefulWidget {
  @override
  _FlutterQuillFormState createState() => _FlutterQuillFormState();
}

class _FlutterQuillFormState extends State<FlutterQuillForm> {
  final quill.QuillController quillController = quill.QuillController.basic();
  final FocusNode editorFocusNode = FocusNode();
  bool isToolBarVisible = true;
  @override
  void initState() {
    editorFocusNode.addListener(() {
      setState(() {
        isToolBarVisible = editorFocusNode.hasFocus;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Reply form"),
        actions: [
          IconButton(
            onPressed: () {},
            icon: Icon(Icons.send),
            tooltip: "Send",
          )
        ],
      ),
      body: SafeArea(
        child: Form(
          child: CustomScrollView(
            slivers: [
              SliverAppBar(
                pinned: true,
                toolbarHeight: isToolBarVisible ? 50 : 0,
                title:
                    Visibility(visible: isToolBarVisible, child: getToolBar()),
              ),
              SliverToBoxAdapter(
                child: Column(
                  children: getTextFields(),
                ),
              ),
              SliverFillRemaining(
                hasScrollBody: true,
                child: getEditor(),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget getToolBar() {
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: quill.QuillToolbar.basic(
        controller: quillController,
        showUnderLineButton: false,
        showStrikeThrough: false,
        showColorButton: false,
        showBackgroundColorButton: false,
        showListCheck: false,
        showIndent: false,
      ),
    );
  }

  List<Widget> getTextFields() {
    return [
      Text("From: test@my.org"),
      SizedBox(height: 8),
      TextField(
        decoration: InputDecoration(labelText: "To"),
      ),
      TextField(
        decoration: InputDecoration(labelText: "Cc"),
      ),
      TextField(
        decoration: InputDecoration(labelText: "Bcc"),
      ),
    ];
  }

  Widget getEditor() {
    return QuillEditor(
      controller: quillController,
      scrollable: true,
      scrollController: ScrollController(),
      focusNode: editorFocusNode,
      padding: EdgeInsets.all(5),
      autoFocus: true,
      readOnly: false,
      expands: false,
      placeholder: "compose_email",
    );
  }

  @override
  void dispose() {
    super.dispose();
    quillController.dispose();
  }
}

在焦点节点监听器中,我们可以将工具栏的状态设置为可见或不可见,不要忘记处置控制器和焦点节点。

截图:

【讨论】:

  • 我在尝试时控制台中有很多错误消息。这并不完全有效。可能是因为这些错误,它没有显示工具栏。无论如何感谢您的回答:)
  • 我已将flutter_quill 版本更新为1.6.4,它似乎可以工作了!谢谢!!
  • 很高兴能帮上忙!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
  • 2011-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多