简体   繁体   中英

How do I convert a listView to a GridView flutter?

I have a scrollable list view in a horizontal format that I want to covert into a grid view with the same elements.

Container(
  height: MediaQuery.of(context).size.height / 4,
  child: ListView.builder(
    scrollDirection: Axis.horizontal,
    itemCount: homeList.length,
    itemBuilder: (ctx, i) {
      return GestureDetector(
        onTap: () {
          if (i == 0) {            
            _interstitialAd.show();
          } else if (i == 1) {

          } else if (i == 2) {
            sendInvite();
          }
        },
      );
    },
  ),
)

This is what the list view looks like: 在此处输入图片说明

And this is what I want it to look like: 在此处输入图片说明

Flutter has the equivalent to ListView.builder for GridView . You only need to tell it the number of columns/rows(depending in the orientation) you want.

GridView.builder(
          itemCount: homeList.length,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
          itemBuilder: (BuildContext context, int index) {
             return GestureDetector(
                 onTap: () {
                  if (i == 0) {
                       _interstitialAd.show();
                  } else if (i == 1) {

                  } else if (i == 2) {
                      sendInvite();
                  });
          },
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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