1

Is it possible to layout in Flutter using grid system. Having something similar to Bootstrap grid system might be nice.

I'd like my rows to contain 4 columns with fixed spaces(gutters) between the columns. (Gutters should have same size, regardless of screen width).

And I'd like to have at least some way of saying that the widget should be N columns width, starting from i column.

So final api can be something like these:

GridRow(
  gutter: 16,
  columnsCount: 4,
  children: [
    GridColumn(child:SomeWidget, width:3, begin:2),
  ],
)

1 Answer 1

3

There is already a package that fulfills all your needs, it is the StaggeredGridView.

It is not exactly the syntax you described, but the functionality should be greater or the same. You can add the gutters by setting mainAxisSpacing and crossAxisSpacing, set the column (or row, depending on the orientation) count by using crossAxisCount.

To specify the space which is used by the child, you define a staggeredTiles list, in which each StaggeredTile represents the child of the same index.

It is not exactly the same as the Bootstrap grid, but it comes close enough.


I've put together a super slim example:

Demo

import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: StaggeredGridView.count(
          mainAxisSpacing: 8.0,
          crossAxisSpacing: 8.0, 
          crossAxisCount: 12,
          staggeredTiles: [
            StaggeredTile.count(6, 6),
            StaggeredTile.count(4, 1),
            StaggeredTile.count(2, 2),
            StaggeredTile.count(2, 2),
          ],
          children: <Widget>[
            Container(
                color: Colors.red,
                child: Text('6,6')
            ),
            Container(
                color: Colors.green,
                child: Text('4,1')
            ),
            Container(
                color: Colors.blue,
                child: Text('2,2')
            ),
            Container(
                color: Colors.yellow,
                child: Text('2,2')
            ),
          ],
        ),
      ),
    );
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

The only issues, is that items and positions are separated. If you need to add a new item, you need to insert in into both lists. But I can use it and create widget with better api.
Well you can simply create wrapper method like you said, hope this solves your question :)

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.