【问题标题】:change the selected text color of the TextField flutter更改 TextField 颤动的选定文本颜色
【发布时间】:2021-01-18 03:25:12
【问题描述】:

有什么方法可以在 Flutter 中更改 TextField 中选定的文本颜色? 我尝试使用控制器本身,但没有太多可以玩的属性.. 这是我想要的示例

有没有办法实现这种行为?

【问题讨论】:

    标签: flutter user-interface colors textfield


    【解决方案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',
            ),
          ]
        )
      )
    

    【讨论】:

    • 但是我希望它是可编辑的,选择一个特定的文本然后改变它的颜色..这怎么可能!
    【解决方案2】:

    在 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',
                ),
              ]
            )
          )
    

    【讨论】:

    • 但是我希望它是可编辑的,选择一个特定的文本然后改变它的颜色..这怎么可能!
    【解决方案3】:

    我认为您正在寻找的是富文本编辑器或文本字段,我认为 Flutter 不支持开箱即用。我发现 pub 上有几个库最好是 zyphercreamy_field zypher 正在重新编写预发布可能需要注意这一点

    【讨论】:

    • 非常感谢亲爱的
    猜你喜欢
    • 2021-04-30
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 2014-09-02
    • 2011-12-31
    • 1970-01-01
    相关资源
    最近更新 更多