如何在 Flutter 中为禁用的文本表单字段的标签设置主题颜色?

Vin*_*rga 7 theming dart material-design flutter

我想在我的 Flutter 应用程序中禁用文本字段的标签上应用一个主题,因为我现在的灰色很难阅读。

我想将它应用到我的整个应用程序,所以我想使用主题,但是,我没有找到任何可以自定义标签文本样式的解决方案 仅在禁用文本表单字段时

如何在 Flutter 中为禁用的文本表单字段的标签设置主题和全局颜色?

我知道如何有条件地更改标签的文本样式,但是,我需要记住始终使用相同的样式(或者我可以包装小部件,但这听起来也不理想)。我可以通过decoration命名参数自定义标签的颜色,如下所示:

TextFormField(
  enabled: isEnabled,
  decoration: InputDecoration(
    labelText: 'Value',
    labelStyle: TextStyle(color: isEnabled ? Colors.green : Colors.red),
  ),
  // .... other fields, like controller might come here
),
Run Code Online (Sandbox Code Playgroud)

Von*_*uot 17

您可以使用Theme来包裹您的小部件,设置属性disabledColor

示例:演示

final customText = Theme(
  data: ThemeData(
    disabledColor: Colors.blue,
  ),
  child: TextFormField(
    enabled: false,
    decoration: const InputDecoration(
        icon: Icon(Icons.person),
        hintText: 'What do people call you?',
        labelText: 'Name *',
    ),
  ),
);
Run Code Online (Sandbox Code Playgroud)

或全球范围内

Widget build(BuildContext context) {
  return MaterialApp(
    title: 'Flutter Demo',
    theme: ThemeData(
      disabledColor: Colors.blue,
    ),
    home: MyHomePage(title: 'Flutter Demo Home Page'),
  );
}
Run Code Online (Sandbox Code Playgroud)


Rub*_*elo 7

您可以使用InputDecorationTheme

MaterialApp 有一个属性theme,您可以在其中设置自定义ThemeData

ThemeData 有一个属性inputDecorationTheme,您可以在其中设置InputDecorationTheme

InputDecorationTheme 有很多属性可以用来自定义文本字段。

 MaterialApp(
        theme: ThemeData(
          inputDecorationTheme: InputDecorationTheme(
            border: OutlineInputBorder(),
            contentPadding: EdgeInsets.symmetric(
              vertical: 22,
              horizontal: 26,
            ),
            labelStyle: TextStyle(
              fontSize: 35,
              decorationColor: Colors.red,
            ),
        ),
)
          
Run Code Online (Sandbox Code Playgroud)

  • 这并不能回答问题。我只想更改禁用的文本字段的标签,这会更改所有内容。 (11认同)
  • 创建一个自定义主题以启用和其他禁用主题,并在需要时通过 setState() 进行更改。@文斯瓦尔加 (2认同)