gpt4 book ai didi

flutter - Flutter:容器+ ListView可滚动

转载 作者:行者123 更新时间:2023-12-03 04:08:48 25 4
gpt4 key购买 nike

我有一个带有容器+ listView的页面。现在listView是可滚动的,但是我希望整个页面都是可滚动的。 (因此,如果用户滚动,则容器会“消失”)

Column(
children: <Widget>[
Container(
color: Colors.white.withOpacity(0.95),
width: 400,
height: 400,
child: Text("")),
ListView(
children: posts,
),
],
),

这可能吗,如何实现?

谢谢!

最佳答案

  • 如果您不使用Column并使用ListView.builder
    根据索引显示小部件。
  • 例如,在0索引上显示Container
  • 这样,您无需使用任何其他小部件即可滚动
    并根据您的要求,容器将与列表一起滚动。
  • 这样就不会有任何溢出错误
    就像您在使用Column时得到的一样。
  • ListView.builder的
  • Plus要点是,您的帖子将被延迟创建,这是在Flutter中创建ListView的有效方法。无论您的用户界面有多少帖子都不会落后。
  • 乍一看,这看起来像是骇客,但是根据您的要求,容器也必须能够滚动,这种解决方案对我来说很有意义,因为容器可以被视为列表的第一个元素。

  • 让我知道它是否对您有用。

    以下是工作代码供您引用:
    @override
    Widget build(BuildContext context) {
    return ListView.builder(
    itemCount: posts.length + 1, // +1 because you are showing one extra widget.
    itemBuilder: (BuildContext context, int index) {
    if (index == 0) {
    return Container(
    color: Colors.white.withOpacity(0.95),
    width: 400,
    height: 400,
    child: Text(""));
    }
    int numberOfExtraWidget = 1; // here we have 1 ExtraWidget i.e Container.
    index = index - numberOfExtraWidget; // index of actual post.
    return posts[index];
    },
    );
    }

    希望这对您有用,如有任何疑问,请发表评论。

    关于flutter - Flutter:容器+ ListView可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59499302/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com