如何动态更改文本字段中特定文本的颜色?

2024-04-15

Consider bellow image, I want to dynamically change the text color of part of the text based on the user input text (not the whole text) in a text field. How can i do that in flutter? enter image description here


对于这个例子,我们实际上不需要一个完整的富文本编辑器。

我在我的应用程序中有一个类似的目标来突出显示标签(@flutter) 或日期参考 (next week, on Friday等),我能够通过扩展内置来实现这一点EditableText小部件并将我的示例作为要点发布在这里:https://gist.github.com/pulyaevskiy/d7af7217c2e71f31dfb78699f91dfbb5 https://gist.github.com/pulyaevskiy/d7af7217c2e71f31dfb78699f91dfbb5

下面是我称之为这个小部件的完整实现AnnotatedEditableText。 有新楼盘了annotations它描述了需要突出显示的文本范围及其样式。

import 'package:flutter/widgets.dart';

class Annotation extends Comparable<Annotation> {
  Annotation({@required this.range, this.style});
  final TextRange range;
  final TextStyle style;

  @override
  int compareTo(Annotation other) {
    return range.start.compareTo(other.range.start);
  }

  @override
  String toString() {
    return 'Annotation(range:$range, style:$style)';
  }
}

class AnnotatedEditableText extends EditableText {
  AnnotatedEditableText({
    Key key,
    FocusNode focusNode,
    TextEditingController controller,
    TextStyle style,
    ValueChanged<String> onChanged,
    ValueChanged<String> onSubmitted,
    Color cursorColor,
    Color selectionColor,
    TextSelectionControls selectionControls,
    this.annotations,
  }) : super(
          key: key,
          focusNode: focusNode,
          controller: controller,
          cursorColor: cursorColor,
          style: style,
          keyboardType: TextInputType.text,
          autocorrect: true,
          autofocus: true,
          selectionColor: selectionColor,
          selectionControls: selectionControls,
          onChanged: onChanged,
          onSubmitted: onSubmitted,
        );

  final List<Annotation> annotations;

  @override
  AnnotatedEditableTextState createState() => new AnnotatedEditableTextState();
}

class AnnotatedEditableTextState extends EditableTextState {
  @override
  AnnotatedEditableText get widget => super.widget;

  List<Annotation> getRanges() {
    var source = widget.annotations;
    source.sort();
    var result = new List<Annotation>();
    Annotation prev;
    for (var item in source) {
      if (prev == null) {
        // First item, check if we need one before it.
        if (item.range.start > 0) {
          result.add(new Annotation(
            range: TextRange(start: 0, end: item.range.start),
          ));
        }
        result.add(item);
        prev = item;
        continue;
      } else {
        // Consequent item, check if there is a gap between.
        if (prev.range.end > item.range.start) {
          // Invalid ranges
          throw new StateError(
              'Invalid (intersecting) ranges for annotated field');
        } else if (prev.range.end < item.range.start) {
          result.add(Annotation(
            range: TextRange(start: prev.range.end, end: item.range.start),
          ));
        }
        // Also add current annotation
        result.add(item);
        prev = item;
      }
    }
    // Also check for trailing range
    final String text = textEditingValue.text;
    if (result.last.range.end < text.length) {
      result.add(Annotation(
        range: TextRange(start: result.last.range.end, end: text.length),
      ));
    }
    return result;
  }

  @override
  TextSpan buildTextSpan() {
    final String text = textEditingValue.text;

    if (widget.annotations != null) {
      var items = getRanges();
      var children = <TextSpan>[];
      for (var item in items) {
        children.add(
          TextSpan(style: item.style, text: item.range.textInside(text)),
        );
      }
      return new TextSpan(style: widget.style, children: children);
    }

    return new TextSpan(style: widget.style, text: text);
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何动态更改文本字段中特定文本的颜色? 的相关文章

随机推荐

  • java:如何检查整个ArrayList的类型

    我知道如何硬编码一种算法来检查 arraylist 中每个对象的类型 但是有没有其他方法可以一次性检查 ArrayList 的类型 我的意思是我的应用程序只有三种类型的 arraylist 假设我有一个返回 ArrayList 的函数 它的
  • 为什么当我将 js 函数分配给局部变量时会失败?

    在 clojurescript 1 9 89 和 Chrome 50 0 2661 102 中 我可以创建如下日志语句 js console log works 但我无法创建这样的一个 def brokenlog js console lo
  • 在透明 JPanel 上绘画时留下痕迹

    我是一个相对较新的 Java 图形程序员 这是我正在尝试的一个简单程序 这是完整的代码 分为 3 个类 Class 1 import javax swing import java awt import java awt event pub
  • 捆绑包安装在 Nokogiri 停止

    我尝试在 Rails 应用程序上进行捆绑安装 但它卡在 安装 nokogiri 处 错误消息底部的说明显示 Make sure that gem install nokogiri v 1 5 5 succeeds before bundli
  • 用于提前退出循环的功能代码

    如何以函数式风格重构此代码 scala 惯用 def findFirst T objects List T T for obj lt objects if expensiveFunc obj null return obj null asI
  • NSString 地址问题

    我正在尝试将地址打印到字符串 但我在第一个 NSLog 中得到不同的地址 而在第二个 NSLog 中得到相同的地址 那么你能告诉我这是怎么回事吗 这真的让我很困惑 预先非常感谢您的努力 NSString str1 NSString allo
  • VS 2012 调试处理 时出现意外的符号读取器错误

    我在 Visual Studio 2012 中调试程序时遇到问题 当我尝试通过插入一些断点来调试代码时 它们似乎不起作用 当我将鼠标悬停在它们上时 会弹出一条消息 The breakpoint will not currently be h
  • Angular 6 不向 http 请求添加 X-XSRF-TOKEN 标头

    我读了the docs https angular io guide http security xsrf protection以及有关 SO 的所有相关问题 但 Angular CSRF 机制仍然对我不起作用 我无论如何都无法发出自动附加
  • PHP 相当于 JavaScript 的 substring()

    JavaScript 有两个方便的子字符串函数 substring from to and substr start length 这意味着我可以决定 当我从位置 X 开始获取子字符串时 我是否要指定结束的字符串位置或子字符串的长度 一个很
  • Android:无需可怕的(用户)权限即可获取用户ID?

    为了管理用户偏好 目前我正在获取谷歌用户名 实际上是他们在设备上注册的电子邮件地址 并使用它 的散列 作为 用户ID 来区分不同的用户 与所描述的内容相符的东西here https stackoverflow com a 4039018 4
  • 通过蓝牙检测附近的另一台 Android 设备

    好吧 我这里有一个有点奇怪的问题 我正在开发一款 Android 游戏 我希望 Android 手机能够检测到彼此的存在 搜索其他玩家的设备将知道其他玩家设备的蓝牙 MAC 地址 来自游戏数据库 但是设备不会配对 并且设备不会处于可发现模式
  • linux:禁用使用环回并通过一个组件的 2 个 eth 卡之间的线路发送数据[关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 我有一个带有 2 个 eth 卡的计算机 通过跳线连接 从第一个到第二个的直接以太网电缆 Linux已安装 我想从第一个网卡向第二个网卡发送数据 我想
  • 是否可以替换 64 位编码图像中的颜色?

    有没有办法获取 64 进制字符串 例如 copyIcon background url data image png base64 iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8 9hAAAAW0lEQV
  • Windows 8 桌面上的 Chrome 浏览器:触摸支持错误

    Windows 8 桌面上的 Chrome 浏览器 版本 23 0 1271 97 被错误地识别为支持触摸事件 实际上它是一个桌面 也就是说 它不支持触摸事件 请参阅 Modernizr Touch 测试示例 http modernizr
  • IE8 中使用 Fancybox 的 jQuery 问题

    我最近收购了一个旧网站 我们谈论的是表格 图像地图 凡是你能想到的 我被要求将 Flash 画廊的旧链接放入灯箱中 我只是用了一个javascript openLightbox 在画廊链接 右上角 上 我知道我知道 但是这看起来并不不合时宜
  • tcp_max_syn_backlog 和 somaxconn 有什么区别?

    我一直在阅读一些关于 Linux 上的 TCP 实现的文章 我很困惑 它们之间有什么区别net ipv4 tcp max syn backlog and net core somaxconn和backlog作为参数传递给listen 系统调
  • 在Excel中通过VBA调用COM加载项

    我有一个用 VSTO 编写的 COM 加载项 我想使用 VBA 宏来调用它 对于加载项 我需要启动它 输入一些文本 自动 然后运行应用程序 该加载项都是按钮 我无法轻松访问代码 因为其中一些代码是加密的 它是第三方 遗憾的是 当单击此加载项
  • 如何隐藏我的源代码以免被复制

    最近有人通知我 我的网站被复制了 当我查看他给我的链接时 我发现除了徽标和文本之外 该网站与我的网站相同 有没有办法隐藏我的代码 或者使我的页面无法右键单击 我在一些网站上看到 如果您访问http example com images ht
  • unique_ptr 的初始化有什么问题?

    有人可以告诉我 以下 unique ptr 初始化有什么问题吗 int main unique ptr
  • 如何动态更改文本字段中特定文本的颜色?

    Consider bellow image I want to dynamically change the text color of part of the text based on the user input text not t