简体   繁体   中英

A non-scrollable grid in flutter

I looked at the GridView widget but its

a scrollable, 2D array of widgets

I want the grid to occupy the maximum area permitted by the parent both horizontally and vertically. Or if there is some widget that expands while allowing us to specify rows and column. There must be cause I can think of several use cases.

Anyway, this is what I have at the moment,

class NumberButtonsGrid extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(child: Row(children: <Widget>[
          Expanded(child: Text("1"), flex: 1),
          Expanded(child: Text("2"), flex: 1),
          Expanded(child: Text("3"), flex: 1),
        ],), flex: 1),
        Expanded(child: Row(children: <Widget>[
          Expanded(child: Text("4"), flex: 1),
          Expanded(child: Text("5"), flex: 1),
          Expanded(child: Text("6"), flex: 1),
        ],), flex: 1),
        Expanded(child: Row(children: <Widget>[
          Expanded(child: Text("7"), flex: 1),
          Expanded(child: Text("8"), flex: 1),
          Expanded(child: Text("9"), flex: 1),
        ],), flex: 1),
        Expanded(child: Row(children: <Widget>[
          Expanded(child: Text("."), flex: 1),
          Expanded(child: Text("0"), flex: 1),
          Expanded(child: Text("00"), flex: 1),
        ],), flex: 1),
      ],
    );
  }
}

Is there a better way to do this. It looks like a mess to me. What if I have to populate different widgets in individual cells? Thats going to be a disaster. This feels like making a table in HTML using <div> .

If you want to disable the scroll of the GridView/ListView, add:

physics: NeverScrollableScrollPhysics()

GridView/ListView has an auto-expanding behavior so you need to set the width of the parent widget if your widget is set to Axis.vertical and vise-versa.

And you can use MediaQuery.of(context).size.width * number to set the width of the GridView according to screen size.

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