flutter - 有没有办法为输入装饰前缀图标设置主题?

标签 flutter dart

我有一个带有前缀 Icon 的输入字段,但我似乎无法弄清楚如何在未选择该字段时设置主题。这是未选中时的样子

enter image description here

如您所见,图标具有深色,而它应该具有边框的浅色。当我选择该字段时,我得到了这个

enter image description here

哪个好得多,因为现在我实际上可以看到图标了。文档说

The size and color of the prefix icon is configured automatically using an [IconTheme] and therefore does not need to be explicitly given in the icon widget.

但是当我设置图标主题时
iconTheme: IconThemeData(color: _primaryColorLight),
primaryIconTheme: IconThemeData(color: _primaryColorLight),
accentIconTheme: IconThemeData(color: _primaryColorLight),

没有什么改变?
  Widget _buildNameField(BuildContext context) {
    final ThemeData theme = Theme.of(context);
    return TextFormField(
      onSaved: (String value) => _phoneNumber = value,
      keyboardType: TextInputType.text,
      style: theme.textTheme.body1,
      decoration: InputDecoration(
        prefixIcon: Icon(Icons.person),
        hintText: 'Name',
      ),
      validator: (String value) {
        if (value.isEmpty || value.length < 2) return 'Please enter a name with two letters or more';
      },
    );
  }

那是我的名字字段。现在我当然可以给图标一个颜色。但我碰巧使用了一个包,它有一个选择菜单,它也使用 TextFormField 并且也面临同样的问题。所以我当然可以 fork 那个 repo 并更改那里的图标颜色。但这似乎很费力,而且相当笨拙。

那么如何设置主题呢?

编辑

文档说 TextFormField 的前缀图标主题来自 IconTheme。
return IconTheme(
  data: theme.iconTheme,
  child: ListTileTheme(
    iconColor: theme.iconTheme.color,
    child: MaterialApp(
      home: TheRootPage(),
      theme: theme,
      debugShowCheckedModeBanner: false,
    ),
  ),
);

这适用于 ListTileTheme 但不适用于 IconTheme 为什么会这样?

最佳答案

答案是将其添加到您的主题数据 brightness: Brightness.dark,brightness: Brightness.light,取决于你的背景颜色。

关于flutter - 有没有办法为输入装饰前缀图标设置主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61611987/

相关文章:

android - 检查现有社交媒体帐户的智能手机

android - 如何以编程方式更改Android中的应用程序托盘图标?

angular - AngularDart无法在 'setAttribute'上执行 'Element': ')'不是有效的属性名称

Flutter 如何创建顶部带有箭头的盒子容器?

java - 如何使用包从 flutter 中的图像读取 QR 码?

flutter - 我试图在 flutter 中添加一个轮播 slider ,但我面临缺少所需的参数错误

flutter - 如何在flutter中的 ListView 中展示广告?

flutter - 使用定位时如何添加BoxDecoration

flutter - 在 Flutter 上保存列表的本地数据

firebase - 如何访问自定义声明?