【问题标题】:Flutter change drawer header image at runtimeFlutter 在运行时更改抽屉标题图像
【发布时间】:2020-03-27 09:17:28
【问题描述】:

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

我的项目中有这段代码可以将图像加载到我的抽屉标题中。 但是必须要更改图像,就像可以使用 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,
      ),
    ),
  ),
),

【问题讨论】:

    标签: android iphone flutter dart


    【解决方案1】:

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

    【讨论】:

    • 我正在使用Provider 来更改我的主题。你有我如何改变图像的例子吗?
    【解决方案2】:

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

    我就是这样做的。

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 2023-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多