简体   繁体   English

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

[英]How to change color of particular text in a text field dynamically?

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?我怎么能在颤振中做到这一点? 在此处输入图片说明

For this example we actually don't need a full blown rich-text editor.对于这个例子,我们实际上不需要一个完整的富文本编辑器。

I had a similar goal in my app to highlight tags ( @flutter ) or date references ( next week , on Friday , etc) and I was able to implement this by extending built-in EditableText widget and posted my example as a Gist here: https://gist.github.com/pulyaevskiy/d7af7217c2e71f31dfb78699f91dfbb5我在我的应用程序中有一个类似的目标来突出显示标签( @flutter )或日期引用( next weekon Friday等),我能够通过扩展内置的EditableText小部件来实现这一点,并将我的示例作为 Gist 发布在这里: https://gist.github.com/pulyaevskiy/d7af7217c2e71f31dfb78699f91dfbb5

Below is full implementation of this widget which I called AnnotatedEditableText .下面是这个小部件的完整实现,我称之为AnnotatedEditableText There is new property annotations which describes ranges of text that need to be highlighted and their style.有新的属性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);
  }
}

Rich text controller works fine!富文本控制器工作正常!

See more on https://pub.dev/packages/rich_text_controllerhttps://pub.dev/packages/rich_text_controller上查看更多信息

使用富文本控制器的示例

First you choose your RegExp首先你选择你的 RegExp

RichTextController _controller;

  Map<RegExp, TextStyle> patternUser = {
    RegExp(r"\B@[a-zA-Z0-9]+\b"):
        TextStyle(color: Colors.amber, fontWeight: FontWeight.bold)
  };

on initState()在 initState() 上

 _controller = RichTextController(
      patternMap: patternUser,

    );

Add controller on your TextFormField在 TextFormField 上添加控制器

  TextFormField(
     controller: _controller,
     style: TextStyle(color: Colors.white),
   )

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?我该怎么办? 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM