简体   繁体   中英

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: 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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