颤振:尽管已用COLUMN包装

时间:2020-08-21 21:30:01

标签: flutter dart widget

我只是想基于列表创建一些文本标签,并将它们输出到可扩展列的单元格中。好的,今天我了解到我需要把EXPANDED放在前面,另外,EXPANDED还需要用FLEX,ROW或COLUMN包装。

但是我不断收到错误消息

 The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a
RenderObject, which has been set up to accept ParentData of incompatible type BoxParentData.
Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically,
Expanded widgets are placed directly inside Flex widgets.
The offending Expanded is currently placed inside an Align widget.
The ownership chain for the RenderObject that received the incompatible parent data was:
  _ScrollSemantics-[GlobalKey#05460] ← Scrollable ← PrimaryScrollController ← SingleChildScrollView
← Expanded ← Align ← Padding ← DecoratedBox ← Padding ← Container ← ⋯
child: Column(
  children: <Widget>[
    CupertinoButton(
      child: Text('expand', style: TextStyle(color: CupertinoColors.activeBlue)),
      onPressed: () {
        setState(() {
          step1Expanded = !step1Expanded;
        );
      },
    ), //Button
    Container(
      child: Column(
        children: <Widget>[
          Expanded(
            flex: 1,
            child: (step1Expanded)? step1RowWidget(taskPrmtrs) : Container()
          ),
        ] //Widget
      ),
    ), //Container
  ],
),

Widget step1RowWidget(List<Parameter> parameters)  {
  TextEditingController name;
   Row(
      children: <Widget>[
        Text("Hallo"),
        Column(
          children: <Widget>[
              for (var parameter in parameters) Text(parameter.descr)
            ],
          ),
        Column(
          children: <Widget>[
            for (var parameter in parameters) Text(parameter.descr)
//            for (var parameter in parameters) _createTextField()
          ],
        ),
  ],
  );
}

Widget _createTextField() {
  TextEditingController name;
  CupertinoTextField(
    controller: name,
    decoration: BoxDecoration(
      ),
    placeholder: "Wert",
    );
}

错误发生在扩展行中代码段中间的上方。还有什么问题?

更新: 通过添加2条EXPANDED行,我解决了该问题(基于以下答案)。我还需要上面的EXPANDED(CONTAINER(来修复它。但是,当屏幕打开时,最外面的COLUMN的第二个外部CONTAINER现在将屏幕填充到底部(一个包裹按钮的列和内部的动态列) 。 预期的行为:CUPERTINOBUTTON的父容器应紧紧包裹在按钮上(基于边距/填充),并且不应在屏幕底部产生空白。我想这似乎是由EXPANDED引起的

  navigationBar: CupertinoNavigationBar(
      middle: Text('Header'),
    ),
    child: SafeArea(
      child: Column(
        children: <Widget>[
          Container(
            alignment: Alignment.center,
            margin: new EdgeInsets.all(10.0),
            padding: new EdgeInsets.all(10.0),
            child: new Expanded(
              flex: 1,
              child: new SingleChildScrollView(
                scrollDirection: Axis.vertical,//.horizontal
                child: Text(fillParamGeneric(taskText, taskPrmtrs)),
              ),
            ),
          ),
          Expanded ( // ADDED according to answers here
            child: Container( // ADDED "child:"
              alignment: Alignment.center,
              margin: new EdgeInsets.all(10.0),
              padding: new EdgeInsets.all(10.0),
              child: Column(
                children: <Widget>[
                  CupertinoButton(
                    child: Text('expand', style: TextStyle(color: CupertinoColors.activeBlue)),
                    onPressed: () {
                      setState(() {
                        step1Expanded = !step1Expanded;
                      });
                    },
                  ),
                  Expanded( // ADDED according to answers here
                    child: Column(
                        children: <Widget>[
                          (step1Expanded)? step1RowWidget(taskPrmtrs) : Container(width: 0.0, height: 0.0)
                        ]
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    ),
  );
}

2 个答案:

答案 0 :(得分:1)

您所要做的只是关闭Container小部件,而使用Expanded小部件。这样,它将占用外部Column的所有剩余空间。 Column小部件未对其子级施加任何约束,因此,由于您没有给Container提供任何大小或约束,因此默认情况下它将尝试尽可能大。

答案 1 :(得分:1)

问题是您的column中有一个column

并且列的高度是无限的。它将引发溢出错误。

您可以做的是将column内的container包裹在Expanded中。