Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expanded widgets must be placed inside Flex widgets

Tags:

flutter

dart

New to flutter, can some one tells me whats wrong with below codeenter image description here

  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.

like image 899
pallav bohara Avatar asked May 09 '18 10:05

pallav bohara


People also ask

What is the difference between expanded and flexible widgets?

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.

Where is expanded widget in flutter?

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.

How do you use Flex widget in flutter?

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.


2 Answers

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.

like image 112
Shady Aziza Avatar answered Nov 10 '22 15:11

Shady Aziza


Crash

This crashes because Expanded is not a descendant of a Flex widget:

Container(
  child: Expanded(
    child: MyWidget(),
  ),
)

Acceptable

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(),
)
like image 45
Suragch Avatar answered Nov 10 '22 13:11

Suragch