How to add Header to GridView in flutter?

Issue

Hi i want to create a GridView.builder with crossAxisCount: 2 and scrollDirection: Axis.horizontal and each set of GridView needed an header please check the image

Need to build something link this but

Solution

You can build using gridView.builder and Row widget in following way.

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Demo"),
      ),
      body: Container(
          child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text("Top"),
          Expanded(
            child: Row(
              children: [
                Expanded(
                  child: GridView.builder(
                    scrollDirection: Axis.horizontal,
                    itemCount: 10,
                    gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 2),
                    itemBuilder: (BuildContext context, int index) {
                      return Text(index.toString());
                    },
                  ),
                )
              ],
            ),
          ),
          Text("Trending"),
          Expanded(
            child: Row(
              children: [
                Expanded(
                  child: GridView.builder(
                    scrollDirection: Axis.horizontal,
                    itemCount: 10,
                    gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 2),
                    itemBuilder: (BuildContext context, int index) {
                      return Text(index.toString());
                    },
                  ),
                )
              ],
            ),
          ),
        ],
      )),
    );
  }

Answered By – Viren V Varasadiya

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.