颤动-如何为文本的一部分加上颜色(即在句子中间)

我是扑朔迷离的新手,我想看看任何有关如何为文本的一部分上色的简单示例。即在句子中间。

让我感到困惑的是,文本的来源是动态,但是我掌握了要为哪个文本/字母上色的数据。

例如:

这是我的文字,此文字应为蓝色,好,此文字应为绿色,谢谢

注意: 字符总长: 94

彩色文本数据:

  • 蓝色: 19至28

  • 绿色: 58至84

有什么想法吗?

djxzh0 回答:颤动-如何为文本的一部分加上颜色(即在句子中间)

您可以使用RichText

RichText(
  text: TextSpan(
    style: DefaultTextStyle.of(context).style,children: <TextSpan>[
      TextSpan(text: 'This is My Text and ',),TextSpan(text: 'this text',style: TextStyle(color: Colors.lightBlue)),TextSpan(text: ' should be in Blue Okay and ',TextSpan(text: 'this text should be green',style: TextStyle(color: Colors.green)),TextSpan(text: ',thank you!'),],)
,

使用带有TextSpans的RichText窗口小部件作为子级来使用不同的样式:

RichText(
          text: new TextSpan(
            style: new TextStyle(
              fontWeight: FontWeight.w300,fontSize: 20.0,children: [
              new TextSpan(text: 'This is My Text and '),new TextSpan(text: 'this text',style: new TextStyle(
                      color: Colors.blue)),new TextSpan(text: ' should be in Blue Okay and '),new TextSpan(text: 'this text should be green',style: new TextStyle(
                      color: Colors.green)),new TextSpan(text: ',thank you'),
,

您可以简单地使用RichText

参考:LINK

RichText(
          text: new TextSpan(
            style: new TextStyle(
              fontSize: 20.0,children: [
              new TextSpan(text: 'This text is blue',new TextSpan(text: 'This text is green',
,

此参考文献Link Here可以帮助您尝试这一点。您应该使用RichText而不是Text

本文链接:https://www.f2er.com/3161379.html

大家都在问