dart - Flutter 在 ListView 或 PageView 中跳过 child

标签 dart flutter

假设我的 PageView 中有 4 个 child

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

我想在 bool 值为 true 时显示 Page3,我该怎么做,我想放一个 null 但我不允许这样做。

最佳答案

一种方法是使用 - 使用 sync* 函数。

如果 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
          }()),
        ),

关于dart - Flutter 在 ListView 或 PageView 中跳过 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54842549/

相关文章:

android - 调用函数并将参数从 Kotlin 传递给 Flutter

flutter - 我该如何解决这个问题(操作系统错误: A required privilege is not held by the client. errno = 1314)

dart - 如何以编程方式更改其子项中 main.dart AppBar 的标题?

dart - Flutter Firestore嵌套查询

dart - Flutter:共享偏好

flutter - 如何在flutter中的showModalBottomSheet中从CupertinoDatePicker返回数据?

dart - 如何确定 Flutter 中图像的宽度和高度?

Flutter:setState() 不会触发构建

flutter - sleep (持续时间)与 Future.delayed(持续时间)

flutter - 压缩视频文件