1

at the moment I am using GridView.builder to create a layout of venues which looks like this: standard gridview layout.

The only problem is, I want the layout to look like this instead (i.e. after every 4 cards arranged in a grid like normal there is to be one full width card. Then the pattern continues as normal). Each card will display the name of a different venue in the database desired grid layout.

Can anyone help me to build this from scratch? I have previously used a library called flutter_staggered_grid_view but the issue I had was that every card had the same name. As such I'd like to build my own (also will come in handy in the future!)

This is the code for using the standard GridView.builder:

if (snapshot.hasData) {
     List<Venue?> venuesData = snapshot.data!; // data coming from db
       return Padding(
          padding: EdgeInsets.only(bottom: 2.h),
            child: GridView.builder(
              gridDelegate:
                const SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                ),
                  itemCount: venuesData.length,
                  itemBuilder: (BuildContext context, int index) {
                    return Card(
                      color: Colors.amber,
                      child: Center(
                        child: Text(venuesData[index]!.name)),
                      );
                    }),
4
  • Why not split the List data into [a,b,c,d][e][f,g,h,i][j] when drawing? you can control any effect you want Commented Jan 10, 2023 at 2:30
  • Let's say I follow this approach, then how do I put all these different arrays together without using Gridview.builder ? Commented Jan 10, 2023 at 13:34
  • Do you need pagination? Commented Jan 10, 2023 at 13:40
  • It's likely to end up being a list of a few hundred entries (max) so loading them as the user scrolls is desirable, but the whole list will be on one page/ screen Commented Jan 10, 2023 at 15:35

2 Answers 2

1

From your description, you want to build (from scratch) a custom scrolling layout that displays 4 smaller tiles 2x2, then 1 big tile, then repeat.

A relatively easy approach is to use a ListView (instead of trying to use a GridView like you were doing) to display the items. You can treat a "2x2 grid" as a single item in the list. First, you need to calculate how many rows (itemCount) the ListView will have. For example, if you have 5 items, the ListView will have 2 rows: 1 row with 4 smaller tiles, and 1 row with 1 big tile. If you have 10 items, you will have 4 rows.

Next, when creating the ListView, in the itemBuilder callback, you can do your own calculation and decide if you want to return a "2x2 item" or a single "large item".

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your response! The itemCount will constantly be increasing/ changing as I add new venues to the db. If I am to treat the 2x2 grid as a single item, how will that work?
@DiscoQueenXOXO You can use math to figure it out. For example, when the database shows you currently have 5 items, then that'll be 2 rows - one row for the first 4, another row for the last 1. Later when the db shows you have 10 items, that will be calculated into having 4 rows.
I'm currently playing with a solution whereby if the index % 5 == 0 I want to return a wide card, otherwise return a GridView.builder. Though I don't really want to return a gridview inside a listview, my searches of how to implement 2 x 2 columns inside a listview keep leading me to just use a gridview as that's what it's for. Do you have any further suggestions?
Update the maths logic would be: (index % 5 == 4) but that's besides the point haha
It seems it cannot be done just using vanilla flutter :( - others have tried, dang! stackoverflow.com/questions/61003079/…
|
1

For any struggling like I was, it seems there is no way to make your own custom layout like I wanted without using a package (which seems really dumb to me). HOWEVER, there is a package that does exactly what I needed, so massive kudos to flutter_staggered_grid_view as it has saved me from banging my head against the wall! Here is the final layout and the bottom of that layout and my code in case it helps anyone else:


        GridView.custom(
          shrinkWrap: true,
          physics: const ScrollPhysics(),
          padding: const EdgeInsets.all(4),
          gridDelegate: SliverQuiltedGridDelegate(
            crossAxisCount: 2,
            mainAxisSpacing: 4,
            crossAxisSpacing: 4,
            repeatPattern: QuiltedGridRepeatPattern.same,
              pattern: [
                const QuiltedGridTile(1, 1),
                const QuiltedGridTile(1, 1),
                const QuiltedGridTile(1, 1),
                const QuiltedGridTile(1, 1),
                const QuiltedGridTile(1, 2),
              ],
            ),
            childrenDelegate: SliverChildBuilderDelegate(
              (context, index) {
                return Container(
                  color: Colors.amber,
                    child: Text(venuesData[index]!.name));
               },
            childCount: venuesData.length,
           ),
          ),

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.