Warm tip: This article is reproduced from serverfault.com, please click

Incorrect use of ParentDataWidget

发布于 2020-12-05 17:52:30

I am trying to build a container with a row. In this row, I would like to have 4 icons.

If I have done it, my problem is that I am getting an error "Incorrect use of ParentDataWidget".

I do not find where the problem is coming from. If you could help me to solve this it would be appreciated. Many thanks.

 Card(
                        child:
                        Container(
                          // color: Colors.red,
                          alignment: Alignment.center,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children:[
                              //Time
                              FlatButton(
                                child:
                                InkWell(
                                  child: Container(
                                    //  color: Colors.white,
                                      child: Column(
                                        mainAxisAlignment: MainAxisAlignment.center,
                                        children: [
                                          Icon(Icons.timer),
                                          Text('Time'),
                                        ],
                                      )
                                  ),
                                  onTap: () {
                                    print("Tapped on Time");
                                  },
                                ),
                              ),

                              //Energy
                              FlatButton(
                                child:
                                InkWell(
                                  child: Expanded(
                                    child: Container(
                                      //   color: Colors.white,
                                        child: Column(
                                          mainAxisAlignment: MainAxisAlignment.center,
                                          children: [
                                            Icon(Icons.battery_charging_full),
                                            Text('Energy'),
                                          ],
                                        )
                                    ),
                                  ),
                                  onTap: () { },
                                ),
                              ),

                              //Important
                              FlatButton(
                                child:
                                InkWell(
                                  child: Expanded(
                                    child: Container(
                                      //   color: Colors.white,
                                        child: Column(
                                          mainAxisAlignment: MainAxisAlignment.center,
                                          children: [
                                            isImportant =="true" ? Icon(Icons.star,color: Colors.orange,) :
                                            Icon(Icons.star_border, color: Colors.grey,),
                                            // Icon(Icons.battery_charging_full),
                                            Text('Important'),
                                          ],
                                        )
                                    ),
                                  ),
                                  onTap: () {
                                    setState(() {
                                      if (isImportant=='true'){
                                        isImportant = 'false';}
                                      else
                                      {isImportant= 'true';
                                      }
                                    });
                                  },
                                ),
                              ),

                              //Urgent
                              FlatButton(
                                child:
                                InkWell(
                                  child: Expanded(
                                    child: Container(
                                      //   color: Colors.white,
                                        child: Column(
                                          mainAxisAlignment: MainAxisAlignment.center,
                                          children: [
                                            isUrgent =="true" ? Icon(Icons.flag,color: Colors.red,) :
                                            Icon(Icons.outlined_flag, color: Colors.grey,),
                                            // Icon(Icons.battery_charging_full),
                                            Text('Urgent'),
                                          ],
                                        )
                                    ),
                                  ),
                                  onTap: () {
                                    setState(() {
                                      if (isUrgent=='true'){
                                        isUrgent = 'false';}
                                      else
                                      {isUrgent= 'true';
                                      }
                                    });
                                  },
                                ),
                              ),
                            ],
                          ),

                        )),

Questioner
Laurent Thomas
Viewed
0
bluenile 2020-12-06 02:14:50

You are using Expanded widget where it cannot be used. The parent of an Expanded widget can only be a Flex widget. Expanded widget has to be a child of Row, Column or Flex widget.

Therefore you will have to remove Expanded widget from all places where the parent of Expanded Widget is not either a Row, Column or Flex. For example in the code below, Expanded widget is a child of FlatButton so you will have to remove the Expanded from here.

                          FlatButton(
                            child:
                            InkWell(
                >>>>>>>>>     child: Expanded(     <<<<< REMOVE FROM HERE 
                                child: Container(
                                  //   color: Colors.white,
                                    child: Column(
                                      mainAxisAlignment: MainAxisAlignment.center,
                                      children: [
                                        Icon(Icons.battery_charging_full),
                                        Text('Energy'),
                                      ],
                                    )
                                ),
                              ),
                              onTap: () { },
                            ),
                          ),

Please refer to Flutter docs : Expanded class

Expanded

A widget that expands a child of a Row, Column, or Flex so that the child fills the available space.

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.

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).