错误使用 ParentDataWidget - 行和容器

Incorrect use of ParentDataWidget - Row and container

我正在尝试构建一个带有一行的容器。在这一行,我想有 4 个图标。

如果我这样做了,我的问题是出现错误“ParentDataWidget 的使用不正确”。

我找不到问题出在哪里。如果你能帮我解决这个问题,我将不胜感激。非常感谢。

 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';
                                      }
                                    });
                                  },
                                ),
                              ),
                            ],
                          ),

                        )),

您正在使用无法使用的扩展小部件。 Expanded 小部件的父级只能是 Flex 小部件。展开的小部件必须是行、列或 Flex 小部件的子项。

因此,您必须从 Expanded Widget 的父级不是 Row、Column 或 Flex 的所有地方删除 Expanded widget。例如在下面的代码中,Expanded 小部件是 FlatButton 的子项,因此您必须从此处删除 Expanded。

                          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: () { },
                            ),
                          ),

请参考 Flutter 文档: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).