简体   繁体   English

Flutter 子小部件正在使用错误的 Theme.of(context).color 重建

[英]Flutter child widget is rebuilding with wrong Theme.of(context).color

After changing color and setting the state of the whole screen, everything rebuilds with the right theme except for the expansion tile (which has the same parent as the list view you see).更改颜色并设置整个屏幕的 state 后,除了扩展磁贴(与您看到的列表视图具有相同的父级)之外,所有内容都使用正确的主题重建。 The expansion tile is a my own widget that I created, where I copied the code of the original expansion tile and just changed the icon direction.扩展磁贴是我自己创建的小部件,我在其中复制了原始扩展磁贴的代码,只是更改了图标方向。 If I switch to a different tab and comeback it's updated and has the right theme.如果我切换到不同的选项卡并卷土重来,它会更新并具有正确的主题。 I am using CupertinoTheme.of(context).barBackgroundColor and this works for all other widgets just fine on updating.我正在使用 CupertinoTheme.of(context).barBackgroundColor ,这适用于所有其他小部件,在更新时效果很好。

enter image description here This picture shows how the expansion tile at the bottom still has the dark theme after rebuilding.在此处输入图片描述这张图片显示了底部的扩展图块在重建后仍然具有深色主题。

enter image description here This picture shows the TodaysWorkoutListVIewBuilder() which updates just fine and the TodaysCardioListViewBuilder(), which doesn't update the Theme correctly.在此处输入图像描述此图显示更新正常的 TodaysWorkoutListVIewBuilder() 和未正确更新主题的 TodaysCardioListViewBuilder()。

enter image description here Here you can see the usage of the ExpansionTile inside the TodaysCardioListViewBuilder() and when I print the color at the beginning of building this I get the right color(white), but the Expansion Tile collapsed background is still black.在此处输入图像描述在这里您可以看到在 TodaysCardioListViewBuilder() 中的 ExpansionTile 的用法,当我在构建开始时打印颜色时,我得到了正确的颜色(白色),但展开瓦片折叠的背景仍然是黑色的。

If you have any ideas where I went wrong please tell me.如果您对我哪里出错有任何想法,请告诉我。

I found the solution but will leave this on here in case somebody has the same issue.我找到了解决方案,但如果有人遇到同样的问题,我会把它留在这里。 The error was that MyCupertinoExpansionTile didn't have a key, so it wasn't rebuilding.错误是 MyCupertinoExpansionTile 没有密钥,所以它没有重建。 The solution was to just add the parameter key to the widget:解决方案是将参数键添加到小部件:

... MyCupertinoExpansionTile( key: UniqueKey(), ... ), ... MyCupertinoExpansionTile(键:UniqueKey(),...),

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

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