在运行时颤动更改抽屉标题图像

时间:2020-03-27 09:17:28

标签: android iphone flutter dart

当我从应用程序内部切换应用程序主题时,我正在寻找一种更改抽屉标题图像的方法。

我的项目中有此代码,可将图像加载到抽屉标题中。 但是必须要改变图像,就像可以使用Theme.of(context).backgroundColor

改变颜色一样
child: ListView(
  padding: EdgeInsets.zero,
  children: <Widget>[
    UserAccountsDrawerHeader(
    accountName: const Text(_AccountName),
    accountEmail: const Text(_AccountEmail),
    currentAccountPicture: CircleAvatar(
      backgroundColor: Colors.brown,
      child: Text(_AccountAbbr),
    ),
    decoration: BoxDecoration(
    color: Colors.blue,
    image: DecorationImage(
      image: AssetImage('assets/md_drawer_header.jpg'),
      fit: BoxFit.cover,
      ),
    ),
  ),
),

2 个答案:

答案 0 :(得分:0)

无论您使用BLoCProvider更改主题来更改主题,都可以从其他来源提供图像,就像将主题提供给MaterialApp小部件的方式一样

答案 1 :(得分:0)

我能够自己解决此问题。 我不知道我的解决方案是否是最好的解决方案,但是它对我有用。

这就是我的方法。

bool isDarkTheme = false;

class MdHeader extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return UserAccountsDrawerHeader(
      ...

      decoration: BoxDecoration(
        color: Theme.of(context).backgroundColor,
        image: DecorationImage(
          image: isDarkTheme
              ? AssetImage('assets/images/md_dh_dark.jpg')
              : AssetImage('assets/images/md_dh_light.jpg'),
          fit: BoxFit.cover,
        ),
      ),
    );
  }

  static headerImageIsDark(bool isDark) {
    isDarkTheme = isDark;
  }
}

然后在负责在运行时切换主题的类中,我简单地调用了如下方法。

MdHeader.headerImageIsDark(true);