简体   繁体   English

Flutter:动态按键隐藏/显示小部件

[英]Flutter: hide/show widget by key dynamically

How can we show/hide widget inside another widget in flutter?我们如何在 flutter 中的另一个小部件中显示/隐藏小部件?

I have list of question which are inside list view builder I want to hide questions on user answer selection.我有列表视图构建器中的问题列表,我想隐藏关于用户答案选择的问题。

if you have all equations in question[] and whether to show them in show[], you could place into listbuild如果你有所有有问题的方程 [] 以及是否在 show[] 中显示它们,你可以放入 listbuild

return (bool[index] ? Card(child:Text(question[index])) : Container())

This returns the card in bool is true and the empty (not displayed container) otherwise.这将返回 bool 中的卡片为真,否则为空(未显示的容器)。

(I think the other answer would do the trick too) (我认为另一个答案也可以解决问题)

Another option you can consider though is using the Visibility widget to wrap the "question" widget and toggle the visible property based on a bool.您可以考虑的另一个选项是使用 Visibility 小部件来包装“问题”小部件并基于 bool 切换可见属性。 To make this (or the above solution) work, you'd want to update the value of this bool using eg setState on the method called when,as you say, "user answer selection" happens.为了使这个(或上面的解决方案)工作,你想更新这个 bool 的值,例如在“用户答案选择”发生时调用的方法上使用例如 setState 。

Of course, if you have a more complicated application it might make more sense to use Provider for state management and wrap all of this in a Consumer.当然,如果您有一个更复杂的应用程序,使用 Provider 进行状态管理并将所有这些都包装在一个消费者中可能更有意义。

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

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