简体   繁体   English

如何更改 TextField 选择的颜色

[英]How to change the color of the TextField selection

How to change the color of the textfield 'select' like this image:如何更改文本字段“选择”的颜色,如下图所示:

Example例子

In this image have the background selection blue and the options grey, but in flutter, how can I change this color?在此图像中,背景选择为蓝色,选项为灰色,但在颤动中,我该如何更改此颜色?

In your ThemeData , you have the option for a TextSelectionTheme (recent versions have migrated to this, if you are using an older version the properties are individual properties on ThemeData . Here are the docs for it and an example from the migration docs :在您的ThemeData ,您可以选择TextSelectionTheme (最近的版本已迁移到此,如果您使用旧版本,则属性是ThemeData上的单个属性。 以下是它的文档和迁移文档中的示例:

ThemeData(
  textSelectionTheme: TextSelectionThemeData(
    cursorColor: Colors.red,
    selectionColor: Colors.green,
    selectionHandleColor: Colors.blue,
  )
)

EDIT: If you just want to change a single widget's theme, you can wrap your build function with the Theme widget like this:编辑:如果您只想更改单个小部件的主题,您可以使用Theme小部件包装您的构建功能,如下所示:

  Widget build(BuildContext context) {
    return Theme(
        child: MyWidget(),
        data: ThemeData(
            textSelectionTheme: TextSelectionThemeData(
          cursorColor: Colors.red,
          selectionColor: Colors.green,
          selectionHandleColor: Colors.blue,
        )));
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM