简体   繁体   English

颤抖跳过ListView或PageView中的子级

[英]Flutter skip children in ListView or PageView

Let's say I have 4 children in my PageView as 假设我的PageView有4个孩子,

PageView(
  children: <Widget>[
    Page1(),
    Page2(),
    Page3(), // how to skip this when a condition is false
    Page4(),
  ],
)

I want to show Page3 when a bool value is true , how do I do that, I thought of putting a null but I am not allowed to do that. bool值为true ,我想显示Page3 ,我该怎么做,我想放置一个null但我不允许这样做。

One way to do is with - With sync* functions. 一种方法是使用-具有sync*功能。

Page2 will be displayed if the condition is true. 如果condition为真,将显示第Page2

bool condition = true;

PageView(
          children: List.unmodifiable(() sync* {
            yield Center(child: Text('Page1'));   //Page 1
            if (condition) {
              yield Center(child: Text('Page2'));  // Page2(conditional)
            }
            // yield* children;
            yield Center(child: Text('Page3'));  //Page3
          }()),
        ),

Seems like this is a better way to make the things happen. 似乎这是使事情发生的更好方法。

List<Widget> list = [];
if (condition) {
  list = [
    Page1(),
    Page2(),
    Page3(), 
    Page4(),
  ];
} else {
  list = [
    Page1(),
    Page2(),
    Page4(),
  ];
}

return PageView(children: list);

Beginning with Dart 2.2.2, you can use normal if condition. 从Dart 2.2.2开始,您可以使用normal if条件。

PageView(
  children: <Widget>[
    Page1(),
    Page2(),
    if (_condition) Page3(), // visible only when condition is true
    Page4(),
  ],
)

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

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