【问题标题】:How can I theme the color the disabled text form field's label in Flutter?如何在 Flutter 中为禁用的文本表单字段标签的颜色设置主题?
【发布时间】:2019-11-23 21:05:37
【问题描述】:

我想在我的 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
),

【问题讨论】:

    标签: flutter dart material-design theming


    【解决方案1】:

    您可以使用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,
                ),
            ),
    )
              
    

    【讨论】:

    • 这不能回答问题。我只想更改禁用文本字段的标签,这会改变所有内容。
    • 为启用和其他禁用主题创建自定义主题,并在需要时通过 setState() 进行更改。 @VinceVarga
    • 好吧,谢谢,尽管这与我在问题中添加的解决方法相同。
    【解决方案2】:

    您可以使用Theme 环绕您的小部件设置属性disabledColor

    示例:Demo

    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 *',
        ),
      ),
    );
    

    或全球

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

    【讨论】:

    • 只有在没有配置 labelColor 时才有效。
    • 它为我工作
    猜你喜欢
    • 2020-04-30
    • 1970-01-01
    • 2011-04-11
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 2021-10-01
    相关资源
    最近更新 更多