更改TextField颤动的选定文本颜色

时间:2020-10-02 12:42:47

标签: flutter user-interface colors textfield

是否可以在Flutter中的 TextField 中更改所选文本的颜色? 我尝试使用控制器本身,但没有太多可玩的属性。 这是我想要的样品

enter image description here

有什么办法可以实现这种行为?

3 个答案:

答案 0 :(得分:1)

在完美再现示例图片的下方使用TextSpan小部件

RichText(
    text: TextSpan(
      // set the default style for the children TextSpans
      style: Theme.of(context).textTheme.body1.copyWith(fontSize: 30),
      children: [
        TextSpan(
            text: 'Hello, ',
        ),
        TextSpan(
          text: 'Wor',
          style: TextStyle(
            color: Colors.blue
          )
        ),
        TextSpan(
            text: 'ld',
        ),
      ]
    )
  )

答案 1 :(得分:0)

在TextField中,这已经变得很困难,因为您不知道用户输入了什么。 如果您正在谈论“文本”字段,则可以使用RichText

RichText(
        text: TextSpan(
          // set the default style for the children TextSpans
          style: Theme.of(context).textTheme.body1.copyWith(fontSize: 30),
          children: [
            TextSpan(
                text: 'Styling ',
            ),
            TextSpan(
              text: 'text',
              style: TextStyle(
                color: Colors.blue
              )
            ),
            TextSpan(
                text: ' in Flutter',
            ),
          ]
        )
      )

答案 2 :(得分:0)

我认为您正在寻找的是富文本编辑器或文本字段,我认为flutter不支持开箱即用。我发现最好的一些酒吧是zyphercreamy_field 在预发行版上重新编写了zypher,可能需要注意这一点

相关问题