Search code examples
fluttergridviewwidgetwidth

show grid item as seventh grid with full width after every shown six grid items


I'm fetching data from an api. I'm listing this data in a gridview. After every (index % 6 == 0) I want to show the seventh_item with crossAxisCount: 1 in the grid view. If (index % 6 == 0) is not true, I want to show any_item with crossAxisCount: 2 in the grid view.

seventh_item = adbanner grid item;

while__

any_item = any grid item with data from db.

 Expanded(
          child: FutureBuilder<List<Item>>(
            future: xtraDataAvailable == false
                ? fetchItems(0, 10)
                : fetchItems(skip, limit),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if (snapshot.data != null) {
                return NotificationListener(
                  child: GridView.builder(
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 2,
                    ),
                    itemCount:
                        _search.isEmpty ? itemsgrid.length : _search.length,
                    itemBuilder: (context, index) {
                      if (index != 0 && index % 6 == 0) {
                        return Container(
                          margin: EdgeInsets.only(bottom: 20.0),
                          // sevent_item, the banner has crossAxisCount: 1
                          child: Text('seventh_item'),
                          ),
                        );
                      } else {
                         // any_item, any item from db and has crossAxisCount: 2
                         return Container(child: Text('any_item')),
                    }
                  )
                )

Note There can be a better way like updating the crossAxisCount in the gridview. I have tried this with setstate but it is not working as expected. Just want to make this work. Thank you.


Solution

  • You can use the StaggeredGridView to achieve this from https://pub.dev/packages/flutter_staggered_grid_view#-readme-tab-

    Example

    class Staggered extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: SafeArea(
            child: StaggeredGridView.count(
              crossAxisCount: 2,
              mainAxisSpacing: 5,
              crossAxisSpacing: 5,
              children: List.generate(14, (index){
                return Container(
                  child: Center(
                    child: Text("${index+1}"),
                  ),
                  color: Colors.blue,
                );
              }),
              staggeredTiles: buildTiles(),
            ),
          ),
        );
      }
    
      List<StaggeredTile> buildTiles(){
        return List.generate(14, (index){
          if((index+1)%7 == 0){
            return StaggeredTile.count(2, 1);
          }else{
            return StaggeredTile.count(1, 1);
          }
        });
      }
    }
    

    The Output:

    enter image description here