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

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

Flutter - Use contextMenuBuilder instead of toolbarOptions to disable Textfield interactions

问题

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

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

  1. toolbarOptions: ToolbarOptions(
  2. copy: false,
  3. cut: false,
  4. paste: false,
  5. selectAll: false,
  6. ),

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

  1. contextMenuBuilder: (context, editable) {
  2. return AdaptiveTextSelectionToolbar(
  3. anchors: editable.contextMenuAnchors,
  4. children: null
  5. );
  6. },
英文:

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

  1. toolbarOptions: ToolbarOptions(
  2. copy: false,
  3. cut: false,
  4. paste: false,
  5. selectAll: false,
  6. ),

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

  1. contextMenuBuilder: (context, editable) {
  2. return AdaptiveTextSelectionToolbar(
  3. anchors: editable.contextMenuAnchors,
  4. children: null
  5. );
  6. },

答案1

得分: 2

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

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

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

  1. TextField(
  2. contextMenuBuilder: (context, editableTextState) {
  3. final List<ContextMenuButtonItem> buttonItems =
  4. editableTextState.contextMenuButtonItems;
  5. buttonItems.removeWhere((ContextMenuButtonItem buttonItem) {
  6. return buttonItem.type == ContextMenuButtonType.cut;
  7. });
  8. return AdaptiveTextSelectionToolbar.buttonItems(
  9. anchors: editableTextState.contextMenuAnchors,
  10. buttonItems: buttonItems,
  11. );
  12. },
  13. )
英文:

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

  1. TextField(
  2. contextMenuBuilder: (context, editableTextState) {
  3. final List&lt;ContextMenuButtonItem&gt; buttonItems =
  4. editableTextState.contextMenuButtonItems;
  5. buttonItems.removeWhere((ContextMenuButtonItem buttonItem) {
  6. return buttonItem.type == ContextMenuButtonType.cut;
  7. });
  8. return AdaptiveTextSelectionToolbar.buttonItems(
  9. anchors: editableTextState.contextMenuAnchors,
  10. buttonItems: buttonItems,
  11. );
  12. },
  13. )

答案2

得分: 1

只需执行以下操作:

  1. TextField(
  2. contextMenuBuilder: null
  3. )

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

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

英文:

Just do this:

  1. TextField(
  2. contextMenuBuilder: null
  3. )

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

  1. 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:

确定