Flutter – 使用contextMenuBuilder而不是toolbarOptions来禁用Textfield交互

huangapple go评论81阅读模式
英文:

Flutter - Use contextMenuBuilder instead of toolbarOptions to disable Textfield interactions

问题

自从升级到 Flutter 3.x 之后,toolbarOptions 的使用已被弃用,并引入了 contextMenuBuilder,我找到了一些关于它的使用文档,但找不到关于如何使用它来阻止交互的文档。

问题是,我已经将一些参数设置为 toolbarOptions,我想将这些参数迁移到 contextMenuBuilder,但我无法做到这一点。

toolbarOptions: ToolbarOptions(
    copy: false,
    cut: false,
    paste: false,
    selectAll: false,
),

我尝试将 children 设置为 null,但它仍然显示复制/粘贴按钮。

contextMenuBuilder: (context, editable) {
    return AdaptiveTextSelectionToolbar(
        anchors: editable.contextMenuAnchors, 
        children: null
    );
},
英文:

Since the upgrade to Flutter 3.x the use of toolbarOptions has been deprecated and contextMenuBuilder was introduced, I found some documentations regarding its use but can't find any docs concerning how to use it to prevent interactions.

The question is I have some parameters set to toolbarOptions and I want to migrate those params to contextMenuBuilder but I couldn't make it so

toolbarOptions: ToolbarOptions(
    copy: false,
    cut: false,
    paste: false,
    selectAll: false,
),

I tried setting null to children but it still shows the copy/paste buttons

contextMenuBuilder: (context, editable) {
    return AdaptiveTextSelectionToolbar(
        anchors: editable.contextMenuAnchors, 
        children: null
    );
},

答案1

得分: 2

这是如何移除“Cut”选项。

类似的,您可以调整其他选项。

这是来自官方文档的信息:
https://docs.flutter.dev/release/breaking-changes/context-menus

TextField(
  contextMenuBuilder: (context, editableTextState) {
    final List<ContextMenuButtonItem> buttonItems =
        editableTextState.contextMenuButtonItems;
    buttonItems.removeWhere((ContextMenuButtonItem buttonItem) {
      return buttonItem.type == ContextMenuButtonType.cut;
    });
    return AdaptiveTextSelectionToolbar.buttonItems(
      anchors: editableTextState.contextMenuAnchors,
      buttonItems: buttonItems,
    );
  },
)
英文:

This is how you can remove Cut option.

Similar you can adjust for other options.
This is from official documentation:
https://docs.flutter.dev/release/breaking-changes/context-menus

TextField(
          contextMenuBuilder: (context, editableTextState) {
            final List&lt;ContextMenuButtonItem&gt; buttonItems =
                editableTextState.contextMenuButtonItems;
            buttonItems.removeWhere((ContextMenuButtonItem buttonItem) {
              return buttonItem.type == ContextMenuButtonType.cut;
            });
            return AdaptiveTextSelectionToolbar.buttonItems(
              anchors: editableTextState.contextMenuAnchors,
              buttonItems: buttonItems,
            );
          },
        )

答案2

得分: 1

只需执行以下操作:

TextField(
  contextMenuBuilder: null
)

这在我的实验中与旧日的toolbarOptions: ToolbarOptions(...everything false)产生相同的效果。

请注意,提供null与从未提供它确实有所不同,因为后者将产生默认的非null值。

英文:

Just do this:

TextField(
  contextMenuBuilder: null
)

which (in my experiments) have the same effect as toolbarOptions: ToolbarOptions(...everything false) in the old days.

Note that providing a null is indeed different from never providing it, since the latter will yield a default non-null value.

答案3

得分: 0

只使用

enableInteractiveSelection: false

而不向textField添加任何上下文菜单。

英文:

Currently, I am only using

enableInteractiveSelection: false

without adding any contextmenu to the textField

huangapple
  • 本文由 发表于 2023年3月8日 17:30:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定