【问题标题】:Insert Custom Page in PageView.builder in Flutter在 Flutter 的 PageView.builder 中插入自定义页面
【发布时间】:2021-05-29 04:55:55
【问题描述】:

我正在使用 PageView.builder 小部件,我想在每 5 次网页浏览后插入一个自定义页面。

return PageView.builder(
  itemCount: 50,
  itemBuilder: (context, index) {
    if (index % 5 == 0 && index != 0)
      return Column(
        children: [
          customImage(),   // this will be my custom pageview
          Image.network(_newList[index].image),
        ],
      );
    else
      return Image.network(_newList[index].image);
  },
);

上面的代码工作正常,但它正在替换索引 5 的图像并显示我的自定义页面。

所以请帮助我如何在不丢失任何索引数据的情况下显示 customImage。

【问题讨论】:

  • Image.network(_newList[index - index ~/ 5].image) 或类似的东西
  • 不工作,您能提供任何其他解决方案
  • 我丢失了 index = 5 个数据
  • 不,你没有输:child: PageView.builder( itemCount: 50, itemBuilder: (ctx, i) { return FittedBox( child: i % 5 == 0 && i != 0 ? FlutterLogo() : Text('${i - i ~/ 5}') ); } ), - 基本上你必须跳过i ~/ 5元素,它是用i - i ~/ 5完成的
  • 我正在丢失数据,itemCount 是 50,这意味着我必须获得索引 0 到 49,但我却达到了 40,为什么?

标签: android flutter dart flutter-dependencies flutter-animation


【解决方案1】:

你需要增加你的itemcount

int modular = 5;
int count = 50;
int i = 0;
int itemCount = count + modular;

return PageView.builder(
  itemCount: itemCount,
  itemBuilder: (context, index) {
    if (index % modular == 0 && index != 0)
      return Column(
        children: [
          customImage(),
          Image.network(_newList[index].image),
        ],
      );
    else {
     Widget yourImage = Image.network(_newList[i].image);
     i = index + 1;
     return yourImage;
    }
  },
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多