简体   繁体   中英

How can you change the color of your input text inside a TextFormField or TextField using a common theme?

我知道如何使用TextStyle更改单个TextFormField的文本颜色,但我无法弄清楚如何使用主题在应用程序范围内应用它。

You need to wrap your root widget in Theme and apply data as follows. Both TextField and TextFormField will have same color, common theme.

Theme(
  data: Theme.of(context).copyWith(
    textTheme: Theme.of(context).textTheme.apply(bodyColor: Colors.green),
  ),
  child: Column(
    children: <Widget>[
      TextFormField(...),
      TextField(...),
    ],
  ),
);

If you don't want to wrap every form/text field with a Theme widget, you can set the subhead property of textTheme in your original ThemeData as such:

ThemeData(
textTheme: TextTheme(
  subhead: TextStyle(color: Colors.green),
),

Although, according to the docs subhead is used for the primary text in lists, so I imagine it might have some unwanted effects if you're using lists elsewhere

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