【问题标题】:Get default theme for TextFormField's InputDecoration's label获取 TextFormField 的 InputDecoration 标签的默认主题
【发布时间】:2019-09-05 07:43:13
【问题描述】:

我需要创建一个位于TextFormFields 旁边的自定义小部件,因此我必须设置其标签的样式以匹配TextFormField 的标签样式。

这不起作用:

Theme.of(ctx).inputDecorationTheme.labelStyle

因为,根据其文档:

如果为 null,则默认为从基础 [TextStyle] 派生的值 输入字段和当前的 [主题]。

问题是,我不知道如何获得基本样式。

【问题讨论】:

  • Theme.of(ctx).inputDecorationTheme.labelStyle 对我来说完全没问题。你到底面临什么问题?
  • @KeertiPurswani 我需要获取字体颜色和大小。当您说“工作完全正常”时,您是否尝试放置断点并查看值是否为null?这是我的情况。
  • 我可以看到标签字体颜色和大小的文本。我要在哪里以及为什么要放置断点并检查?
  • @KeertiPurswani 试试errorStyle,它应该是红色的。它最终不会变成红色,因为errorStyle 是null,它默认为问题中所述的基本TextStyle
  • @KeertiPurswani ThemeData.fallback().inputDecorationTheme.labelStyle => null 以及 Theme.of(buildContext).inputDecorationTheme.labelStyle=> null 在 Android 上运行的 Flutter 2.2.3 上

标签: flutter


【解决方案1】:

您正确获取了基本样式。在我的情况下,labelStyle 和来自 InputDecorationTheme 的 hintStyle 无法通过 热重载 进行更新,但在 重新运行 应用程序时它们可以工作。

【讨论】:

    【解决方案2】:

    可能不是一个完整的答案,但我发现颜色基于 primarySwatch 颜色。如果您使用的是浅色主题,只需定义 primarySwatch 即可。对于深色主题:

    ThemeData(
          brightness: Brightness.dark, 
          colorScheme: ColorScheme.fromSwatch(
                  primarySwatch: Colors.lightBlue, // use your material color
                  brightness: Brightness.dark)
    ); 
    

    【讨论】:

      猜你喜欢
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 2015-06-17
      • 1970-01-01
      相关资源
      最近更新 更多