【问题标题】:Change the Highlight Color of selected text更改所选文本的突出显示颜色
【发布时间】:2019-07-06 11:19:36
【问题描述】:

当用户从 TextField 中选择文本时,默认突出显示颜色为蓝色。例如如何将其更改为绿色?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    2021年答案

    用 Theme 包装并使用 copyWith 来保留其他主题数据。

     Theme(data: Theme.of(context).copyWith(
       textSelectionTheme: TextSelectionThemeData(
         selectionColor: Colors.green)),
       child: TextFormField()
    )
    

    【讨论】:

    • 这只改变了高亮的颜色,知道如何改变文本选择器的颜色吗?
    • @BhupeshVarshney 如果您指的是文本字段中的光标,则它的 cursorColor 属性。如果您使用的是自定义主题,您可以定义 textSelectionTheme 并使用 TextSelectionThemeData 定义您喜欢的 cursorColor 和突出显示颜色。
    【解决方案2】:

    用主题包装您的文本小部件,并将颜色分配给 ThemeData

    内的 textSelectionColor 属性

    请参考以下代码:- 我已将文本选择颜色更改为绿色

     Theme(
         data: ThemeData(textSelectionColor: Colors.green),
              child: TextField(
                  controller: _inputController,
                  decoration: InputDecoration(hintText: "Input"),
               ),
      ),
    

    【讨论】:

    • 'textSelectionColor' 已弃用,不应使用。请改用 TextSelectionThemeData.selectionColor。此功能在 v1.23.0-4.0.pre 之后被弃用
    【解决方案3】:

    更改ThemeDatatextSelectionColor 的值,它会为您提供所需的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      相关资源
      最近更新 更多