flutter 不推荐使用文本选择控件,改为用户contextMenuBuilder

q9yhzks0  于 2023-03-31  发布在  Flutter
关注(0)|答案(2)|浏览(153)

bounty将在3天后过期。回答此问题可获得+50声望奖励。user123希望引起更多人关注此问题。

我实现了一个自定义文本选择控件,在高亮显示的文本上的默认复制/粘贴/selectAll旁边添加一个打开Map按钮,如下所示:

class MapTextSelectionControls extends MaterialTextSelectionControls {
  // Padding between the toolbar and the anchor.
  static const double _toolbarContentDistanceBelow = 20.0;
  static const double _toolbarContentDistance = 8.0;

  MapTextSelectionControls();

  @override
  Widget buildToolbar(
      BuildContext context,
      Rect globalEditableRegion,
      double textLineHeight,
      Offset selectionMidpoint,
      List<TextSelectionPoint> endpoints,
      TextSelectionDelegate delegate,
      ClipboardStatusNotifier? clipboardStatus,
      Offset? lastSecondaryTapDownPosition,
      ) {
    //.. some code here

    return MapSelectionToolbar(
      anchorAbove: anchorAbove,
      anchorBelow: anchorBelow,
      clipboardStatus: clipboardStatus,
      handleCopy: canCopy(delegate)
          ? () => handleCopy(delegate, clipboardStatus)
          : null,
      handleMap: selectedText.isNotEmpty
          ? () {
        openLink(generateLocationLinkFromQuery(selectedText));
        delegate.hideToolbar();
      }
          : null,
      handleCut: canCut(delegate) ? () => handleCut(delegate) : null,
      handlePaste: canPaste(delegate) ? () => handlePaste(delegate) : null,
      handleSelectAll:
      canSelectAll(delegate) ? () => handleSelectAll(delegate) : null,
    );
  }
}

class MapSelectionToolbar extends StatefulWidget { ....

SelectableRegion(
        selectionControls: MapTextSelectionControls(),
        focusNode: _selectableRegionFocusNode, ....)

然而,Flutter警告说,我现在实现它的方式即将被弃用,但我无法找出或找到足够的资源来实现相同的结果。

guicsvcw

guicsvcw1#

这些只是来自Flutter的警告,尽管如此,这些更改的发生是有原因的,并且包/插件/库请求您将参数名替换为新的。考虑到参数的类型没有更改,您只需要替换参数的名称。

fnvucqvd

fnvucqvd2#

Flutter 3.7有一种新的方式来实现上下文菜单。下面是细节和示例:A new way to customize context menus你可以这样做:

SelectableText(
  "Lorem ipsum dolor sit amet",
  contextMenuBuilder: (context, editableTextState) {
    final TextEditingValue value = editableTextState.textEditingValue;
    final List<ContextMenuButtonItem> buttonItems = editableTextState.contextMenuButtonItems;
    buttonItems.insert(
      0,
      ContextMenuButtonItem(
        label: 'Send email',
        onPressed: () {
          // your "send email" code    
        },
      ),
    );
    return AdaptiveTextSelectionToolbar.buttonItems(
      anchors: editableTextState.contextMenuAnchors,
      buttonItems: buttonItems,
    );
  },
)

相关问题