• flutter开发实战-自定义长按TextField输入框剪切、复制、选择全部菜单AdaptiveTextSelectionToolba样式UI效果


    flutter开发实战-自定义长按TextField输入框剪切、复制、选择全部菜单样式UI效果

    在开发过程中,需要长按TextField输入框cut、copy设置为中文“复制、粘贴”,我首先查看了TextField中的源码,看到了ToolbarOptions、AdaptiveTextSelectionToolbar,这时候我们可以在剪切、复制、选择全部菜单样式UI效果上显示icon的按钮了。

    在这里插入图片描述

    一、TextField源码中的代码contextMenuBuilder

    我这里在中TextField中的源码,看到了ToolbarOptions、AdaptiveTextSelectionToolbar,可以继承AdaptiveTextSelectionToolbar来实现更改剪切、复制、选择全部菜单样式效果

    将自定义的AdaptiveTextSelectionToolbar,设置到contextMenuBuilder即可。

    TextField源码一段

      /// {@macro flutter.widgets.EditableText.contextMenuBuilder}
      ///
      /// If not provided, will build a default menu based on the platform.
      ///
      /// See also:
      ///
      ///  * [AdaptiveTextSelectionToolbar], which is built by default.
      final EditableTextContextMenuBuilder? contextMenuBuilder;
    
      static Widget _defaultContextMenuBuilder(BuildContext context, EditableTextState editableTextState) {
        return AdaptiveTextSelectionToolbar.editableText(
          editableTextState: editableTextState,
        );
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    二、自定义AdaptiveTextSelectionToolbar

    继承AdaptiveTextSelectionToolbar实现自定义的toolbar样式CustomTextSelectionToolbar

    自定义后需要实现按钮列表

    List<Widget> resultChildren = <Widget>[];
    for (int i = 0; i < buttonItems!.length; i++) {
      final ContextMenuButtonItem buttonItem = buttonItems![i];
      resultChildren.add(SelectionToolBarButton(
        width: 100,
        height: 50,
        icon: (i == 0)?Icon(
          Icons.cut,
          color: Colors.white,
          size: 14,
        ):Icon(
          Icons.copy,
          color: Colors.white,
          size: 16,
        ),
        title: getButtonLabelString(context, buttonItem),
        onPressed: buttonItem.onPressed,
      ));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    自定义按钮SelectionToolBarButton,设置icon+title的按钮样式

    class SelectionToolBarButton extends StatelessWidget {
      const SelectionToolBarButton({
        super.key,
        required this.width,
        required this.height,
        required this.icon,
        required this.title,
        required this.onPressed,
      });
    
      final double width;
      final double height;
      final Icon icon;
      final String title;
      final VoidCallback onPressed;
    
      
      Widget build(BuildContext context) {
        return GestureDetector(
          onTap: () {
            onPressed();
          },
          child: Container(
            color: Colors.black87,
            width: width,
            height: height,
            alignment: Alignment.center,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                icon,
                SizedBox(
                  width: 5,
                ),
                Text(
                  title,
                  style: TextStyle(
                    fontSize: 15,
                    color: Colors.white,
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    CustomTextSelectionToolbar完整代码如下

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    class CustomTextSelectionToolbar extends AdaptiveTextSelectionToolbar {
      const CustomTextSelectionToolbar(
          {super.key, required super.children, required super.anchors});
    
      CustomTextSelectionToolbar.editableText({
        super.key,
        required EditableTextState editableTextState,
      }) : super.editableText(editableTextState: editableTextState);
    
      
      Widget build(BuildContext context) {
        // If there aren't any buttons to build, build an empty toolbar.
        if ((children != null && children!.isEmpty) ||
            (buttonItems != null && buttonItems!.isEmpty)) {
          return const SizedBox.shrink();
        }
    
        List<Widget> resultChildren = <Widget>[];
        for (int i = 0; i < buttonItems!.length; i++) {
          final ContextMenuButtonItem buttonItem = buttonItems![i];
          resultChildren.add(SelectionToolBarButton(
            width: 100,
            height: 50,
            icon: (i == 0)?Icon(
              Icons.cut,
              color: Colors.white,
              size: 14,
            ):Icon(
              Icons.copy,
              color: Colors.white,
              size: 16,
            ),
            title: getButtonLabelString(context, buttonItem),
            onPressed: buttonItem.onPressed,
          ));
        }
    
        switch (Theme.of(context).platform) {
          case TargetPlatform.iOS:
            return CupertinoTextSelectionToolbar(
              anchorAbove: anchors.primaryAnchor,
              anchorBelow: anchors.secondaryAnchor == null
                  ? anchors.primaryAnchor
                  : anchors.secondaryAnchor!,
              children: resultChildren,
            );
          case TargetPlatform.android:
            return TextSelectionToolbar(
              anchorAbove: anchors.primaryAnchor,
              anchorBelow: anchors.secondaryAnchor == null
                  ? anchors.primaryAnchor
                  : anchors.secondaryAnchor!,
              children: resultChildren,
            );
          case TargetPlatform.fuchsia:
          case TargetPlatform.linux:
          case TargetPlatform.windows:
            return DesktopTextSelectionToolbar(
              anchor: anchors.primaryAnchor,
              children: resultChildren,
            );
          case TargetPlatform.macOS:
            return CupertinoDesktopTextSelectionToolbar(
              anchor: anchors.primaryAnchor,
              children: resultChildren,
            );
        }
      }
      
      /// Returns the default button label String for the button of the given
      /// [ContextMenuButtonType] on any platform.
      static String getButtonLabelString(BuildContext context, ContextMenuButtonItem buttonItem) {
        if (buttonItem.label != null) {
          return buttonItem.label!;
        }
    
        switch (Theme.of(context).platform) {
          case TargetPlatform.iOS:
          case TargetPlatform.macOS:
          case TargetPlatform.android:
          case TargetPlatform.fuchsia:
          case TargetPlatform.linux:
          case TargetPlatform.windows:
            assert(debugCheckHasMaterialLocalizations(context));
            switch (buttonItem.type) {
              case ContextMenuButtonType.cut:
                return "剪切";
              case ContextMenuButtonType.copy:
                return "复制";
              case ContextMenuButtonType.paste:
                return "粘贴";
              case ContextMenuButtonType.selectAll:
                return "选择全部";
              case ContextMenuButtonType.custom:
                return '';
            }
        }
      }
    }
    
    class SelectionToolBarButton extends StatelessWidget {
      const SelectionToolBarButton({
        super.key,
        required this.width,
        required this.height,
        required this.icon,
        required this.title,
        required this.onPressed,
      });
    
      final double width;
      final double height;
      final Icon icon;
      final String title;
      final VoidCallback onPressed;
    
      
      Widget build(BuildContext context) {
        return GestureDetector(
          onTap: () {
            onPressed();
          },
          child: Container(
            color: Colors.black87,
            width: width,
            height: height,
            alignment: Alignment.center,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                icon,
                SizedBox(
                  width: 5,
                ),
                Text(
                  title,
                  style: TextStyle(
                    fontSize: 15,
                    color: Colors.white,
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151

    三、TextField中使用CustomTextSelectionToolbar

    在TextField输入框中设置contextMenuBuilder

    static Widget _textFieldContextMenuBuilder(BuildContext context, EditableTextState editableTextState) {
        return CustomTextSelectionToolbar.editableText(
          editableTextState: editableTextState,
        );
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    最后在TextField输入框中设置contextMenuBuilder

    TextField(
            contextMenuBuilder: _textFieldContextMenuBuilder,
            minLines: 1,
            maxLines: null,
            keyboardType: TextInputType.multiline,
            textAlignVertical: TextAlignVertical.center,
            autofocus: widget.autofocus,
            focusNode: editFocusNode,
            controller: widget.textEditingController,
            textInputAction: TextInputAction.send,
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    至此可以自定义长按TextField输入框剪切、复制、选择全部菜单样式UI效果
    在这里插入图片描述
    在这里插入图片描述
    使用系统全局剪切、复制、选择全部设置为中文,可以查看:https://blog.csdn.net/gloryFlow/article/details/132966717

    四、小结

    flutter开发实战-自定义长按TextField输入框剪切、复制、选择全部菜单样式UI效果。自定义AdaptiveTextSelectionToolbar,在TextField输入框中设置contextMenuBuilder,实现功能。
    内容较多,描述可能不准确,请见谅。

    本文地址:https://blog.csdn.net/gloryFlow/article/details/132970840

    学习记录,每天不停进步。

  • 相关阅读:
    代码随想录算法训练营第五十七天 | 动态规划 part 15 | 392.判断子序列、115.不同的子序列
    机器学习(七):线性判别分析(LDA)
    gnome-terminal用法解析
    测试工程师如何帮助开发域的质量变好
    Nature methods|新型微生物富集方法,可有效解决高宿主污染样本问题
    Spring Cloud 构建面向企业的大型分布式微服务快速开发框架+技术栈介绍
    Gartner“客户之声”最高分,用户体验成中国数据库一大突破口
    18.基本数据类型对应的包装类
    ClickHouse与Elasticsearch比较总结
    C#语法糖系列 —— 第二篇:聊聊 ref,in 修饰符底层玩法
  • 原文地址:https://blog.csdn.net/gloryFlow/article/details/132970840