Flutter GridView footer (for indicating load with infinite scrolling)
Asked Answered
C

3

20

I have a GridView which works pretty smooth. I use the grid in the context of infinite scrolling where more items are loaded via a REST API whenever scrolling is close to the bottom. No problems here.

However I want to be able to display a loading indicator at the bottom of the grid. This Widget should span all columns in the grid, but should still be a part of the scrollable content.

I am new to Flutter and lost as how to achieve this.

My (working) GridView is instantiated like so:

return new GridView.builder(
  gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: _COLUMN_COUNT),
  controller: widget.scrollController,
  itemCount: widget.items.length,
  itemBuilder: (BuildContext context, int index) {
    return new _ItemView(item: widget.items[index]);
  },
);
Coquette answered 29/10, 2017 at 14:31 Comment(0)
B
25

Wrap SliverGrid by CustomScrollView:

return new CustomScrollView(slivers: <Widget>[
  new SliverGrid(
    gridDelegate: yourGridDelegate,
    delegate: yourBuilderDelegate,
  ),
  new SliverToBoxAdapter(
    child: yourFooter,
  ),
]);
Buskirk answered 31/10, 2017 at 1:37 Comment(0)
C
0

What @najeira mentions is correct and if you still want to keep the "controller: widget.scrollController" property, the CustomScrollView widget supports it:

CustomScrollView(
  controller: widget.scrollController,
  slivers: <Widget>[
      SliverGrid(
        delegate: SliverChildBuilderDelegate( (c,index){return Widget();},
                                              childCount: widget.list.length),
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
      )...
Clonus answered 19/1, 2022 at 16:38 Comment(0)
R
-1

I am not sure in which manner you are building your layout, regardless, I know it is a little different than what you were thinking of, but this sounds to me like a good use case for CircularProgressIndicator.

Note in my case it keeps loading because my snapshot.hasData is always false. Whenever your data is loaded it will replace the ProgressIndicator with whatever data you originally intend to inject inside your widgets.

enter image description here

@override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(title: new Text("GridView Test"),
        ),
        body: new FutureBuilder(future: _getAsyncStuff(),
            builder: (BuildContext context,
                AsyncSnapshot<DataSnapshot> snapshot) {
              return new GridView.builder(
                  gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 3),
                  itemBuilder: (BuildContext context, int index) {
                    return new Card(child:
                    !snapshot.hasData ? new CircularProgressIndicator(
                      strokeWidth: 0.8,) : new Text("Snapshot data#$index")
                    );
                  });
            })

    );
  }
}
Revanche answered 29/10, 2017 at 20:47 Comment(1)
This is not what he was asking for. He was referring to the loading indicator for infinite scrolling in the footer.Jolyn

© 2022 - 2024 — McMap. All rights reserved.