New to flutter, can some one tells me whats wrong with below code
class GamePage extends StatelessWidget {
int _row;
int _column;
GamePage(this._row,this._column);
@override
Widget build(BuildContext context) {
return new Material(
color: Colors.deepPurpleAccent,
child:new Expanded(
child:new GridView.count(crossAxisCount: _column,children: new List.generate(_row*_column, (index) {
return new Center(
child: new CellWidget()
);
}),) )
);
}
}
Attaching error screenshot.
Flexible is a built-in widget in flutter which controls how a child of base flex widgets that are Row, Column, and Flex will fill the space available to it. The Expanded widget in flutter is shorthand of Flexible with the default fit of FlexFit.
Using an Expanded widget makes a child of a Row, Column, or Flex expand to fill the available space along the main axis (e.g., horizontally for a Row or vertically for a Column). If multiple children are expanded, the available space is divided among them according to the flex factor.
The Flex widget allows you to control the axis along which the children are placed (horizontal or vertical). This is referred to as the main axis. If you know the main axis in advance, then consider using a Row (if it's horizontal) or Column (if it's vertical) instead, because that will be less verbose.
You do not have a Flex
ancestor.
An Expanded widget must be a descendant of a Row, Column, or Flex, and the path from the Expanded widget to its enclosing Row, Column, or Flex must contain only StatelessWidgets or StatefulWidgets (not other kinds of widgets, like RenderObjectWidgets).
I am not sure about the need for Expanded
in your case. But removing it or wrapping it in a Column
should fix the problem.
This crashes because Expanded is not a descendant of a Flex widget:
Container(
child: Expanded(
child: MyWidget(),
),
)
Here Expanded is a descendant of Flex:
Flex(
direction: Axis.horizontal,
children: [
Expanded(
child: MyWidget(),
),
],
)
Row is also a Flex widget:
Row(
children: [
Expanded(
child: MyWidget(),
),
],
)
And so is Column:
Column(
children: [
Expanded(
child: MyWidget(),
),
],
)
Another option is to just get rid of the Expanded widget:
Container(
child: MyWidget(),
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With