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

后端 未结 2 1728
你的背包
你的背包 2020-12-11 03:31

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 i

相关标签:
2条回答
  • 2020-12-11 04:03

    you can use simply style in Text Widget

    Text("value",style: TextStyle(color: Colors.purple)),
    

    or if you want to color a specific value in a line then use a row with multiple TextWidgets and give one of the color example

     Row(children: <Widgets>[
          Text("see this")),
          Text("@tammyDelgado",style: TextStyle(color: Colors.purple)),
          Text("Wonderfull")),
        ]),
    
    0 讨论(0)
  • 2020-12-11 04:05

    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

    Below is full implementation of this widget which I called AnnotatedEditableText. There is new property annotations which describes ranges of text that need to be highlighted and their style.

    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);
      }
    }
    
    0 讨论(0)
提交回复
热议问题