简体   繁体   English

如何在 Flutter / GetX 中动态设置 BoxDecoration 主题

[英]How to dynamically theme BoxDecoration in Flutter / GetX

I am using GetX with Flutter and dynamically change Application theme via standard ThemeData.我将 GetX 与 Flutter 一起使用,并通过标准 ThemeData 动态更改应用程序主题。 All is working great for properties within ThemeData.一切都适用于 ThemeData 中的属性。

However, I just implemented Drawer widget and wanted to also theme the 'decoration' property within DrawerHeader (of type BoxDecoration).但是,我刚刚实现了 Drawer 小部件,并且还想在 DrawerHeader(BoxDecoration 类型)中设置“装饰”属性的主题。 I wanted to theme the color within BoxDecoration depending on selected theme but it only seems to accept constant.我想根据所选主题为 BoxDecoration 中的颜色设置主题,但它似乎只接受常量。 I have not find any way to dynamically change it based on the theme.我还没有找到任何方法来根据主题动态更改它。

return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: const <Widget>[
          DrawerHeader(
            decoration:
                BoxDecoration(color: Get.isDarkMode ? Colors.red : Colors.blue),

I get the following error:我收到以下错误:

The values in a const list literal must be constants. const 列表文字中的值必须是常量。 Try removing the keyword 'const' from the list literal.尝试从列表文字中删除关键字“const”。

It relates to the following part: color: Get.isDarkMode? Colors.red: Colors.blue它涉及以下部分: color: Get.isDarkMode? Colors.red: Colors.blue color: Get.isDarkMode? Colors.red: Colors.blue

Any recommendation how to do that?任何建议如何做到这一点?

Thank you.谢谢你。

Felipe - you are right.费利佩 - 你是对的。 I spent hours of reading and testing but for some reason I completely missed the obvious thing - was always looking only at Decoration / BoxDecoration itself and not up the tree.我花了几个小时阅读和测试,但由于某种原因,我完全错过了显而易见的事情——总是只看装饰/盒子装饰本身,而不是树上。 Thank you so much for your fast answer.非常感谢您的快速答复。 It works now.现在可以了。

Issue was with the following line: children: const <Widget>[问题出在以下行: children: const <Widget>[

after removing the const it works - solution:删除const后它可以工作 - 解决方案:

children: <Widget>[

Render two different drawer headers based on Get.color, instead of rendering one drawer head with variable colors.基于 Get.color 渲染两个不同的抽屉头,而不是使用变量 colors 渲染一个抽屉头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM