简体   繁体   English

Flutter ListView.builder创建一个无限滚动。 如何将其设置为在内容结束时停止?

[英]Flutter ListView.builder creates an infinite scroll. How can I set it to stop at the end of content?

I am using ListView.buildler to layer Widget, into a vertical scroll. 我正在使用ListView.buildler将Widget分层为垂直滚动。 My only issue with this approach is that when the scroll reaches the end of my content. 这种方法的唯一问题是当滚动到达我的内容的末尾时。 It wraps back to the top automatically. 它会自动回到顶部。 Currently I have this: 目前我有这个:

    new ListView.builder(
    scrollDirection: Axis.vertical,
    key: new Key(randomString(20)),
    reverse: false,
    primary: true,
    itemBuilder: (BuildContext context, int index) {
        return new Column(
        children: <Widget>[
            new Padding(
            padding: _bookPadding,
            child: new Container(
                width: 106.0,
                height: 162.0,
                child: new FadeInImage(
                    placeholder: new AssetImage('assets/loading.gif'),
                    image: this.book.cover)
            ),
            ),
            const Divider(),
            new Padding(
                padding: _bookPadding,
                child: new Text(
                    this.book.bookName,
                    style: getTextStyle())),
            const Divider(),
            new Padding(
            padding: _bookPadding,
            child: new Text(
                'By ' + this.book.author,
                style: getTextStyle(), ),
            ),
           ....

Is it possible to set ListView.builder to no-wrap when it reaches the end? 是否有可能在ListView.builder到达结束时将其设置为无包装?

This ended up being pretty easy. 这最终很容易。 Not sure how I missed it from the docs, all I needed to add was an Item count = non-null. 不确定我是如何从文档中错过它的,我需要添加的是Item count = non-null。 In my case, since I am building all of my content in a single Column widget, inside the list-view builder, this is what my builder looks like: 就我而言,由于我在单个Column小部件中构建我的所有内容,在list-view构建器中,这就是我的构建器的样子:

new ListView.builder(
    scrollDirection: Axis.vertical,
    key: new Key(randomString(20)),
    primary: true,
    itemCount: 1,
    itemBuilder: (BuildContext context, int index) {
        return new Column(
        children: <Widget>[ .....

I do wonder about this implementation if its a bad pattern. 如果这是一个糟糕的模式,我确实想知道这个实现。 Will ListView.Builder still render each nested children of Component Column, as each children of Column becomes visible? ListView.Builder是否仍会渲染Component Column的每个嵌套子元素,因为Column的每个子元素都可见?

From the docs: "Creates a scrollable, linear array of widgets that are created on demand. This constructor is appropriate for list views with a large (or infinite) number of children because the builder is called only for those children that are actually visible." 从文档: “创建按需创建的可滚动,线性的小部件数组。此构造函数适用于具有大量(或无限)子级数的列表视图,因为仅为那些实际可见的子级调用构建器。 “

You could skip lots of your code. 你可以跳过很多代码。 Sometimes the itemBuilder is very nice, especially when you have lots of widgets and can easily identify your item by index, but you can also use the children directly. 有时itemBuilder非常好,特别是当你有很多小部件并且可以通过索引轻松识别你的项目时,你也可以直接使用这些孩子。 An example from the ListView documentation : ListView文档中的一个示例:

new ListView(
  shrinkWrap: true,
  padding: const EdgeInsets.all(20.0),
  children: <Widget>[
    const Text('I\'m dedicating every day to you'),
    const Text('Domestic life was never quite my style'),
    const Text('When you smile, you knock me out, I fall apart'),
    const Text('And I thought I was so smart'),
  ],
)

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

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