简体   繁体   English

Flutter - 带 Appbar 和列表视图的脚手架 - 与 Expanded 决裂

[英]Flutter - Scaffold with Appbar and listview - breaking with Expanded

I have an issue with Scaffold.我对脚手架有疑问。 I have an AppBar with text and a button.我有一个带有文本和按钮的 AppBar。 The body hosts a ListView.builder.主体托管一个 ListView.builder。 Inside the ListView is a container and a number of TextButton Widgets. ListView 内部是一个容器和一些 TextButton Widget。

The Text part of the TextButton can be wide, and causes the warning bar to appear. TextButton 的 Text 部分可以很宽,并导致出现警告栏。 So, I wrapped in an Expanded widget to wrap it.所以,我用一个 Expanded 小部件来包装它。

This results in an error...这会导致错误...

The following assertion was thrown during performLayout(): RenderFlex children have non-zero flex but incoming width constraints are unbounded.在 performLayout() 期间抛出以下断言:RenderFlex 子级具有非零弹性但传入宽度约束是无界的。

When a row is in a parent that does not provide a finite width constraint, for example if it is in a horizontal scrollable, it will try to shrink-wrap its children along the horizontal axis.当一行位于不提供有限宽度约束的父项中时,例如,如果它位于水平可滚动项中,它将尝试沿水平轴收缩包装其子项。 Setting a flex on a child (eg using Expanded) indicates that the child is to expand to fill the remaining space in the horizontal direction.在子项上设置 flex(例如使用 Expanded)表示子项将展开以填充水平方向的剩余空间。 These two directives are mutually exclusive.这两个指令是互斥的。 If a parent is to shrink-wrap its child, the child cannot simultaneously expand to fit its parent.如果父母要收缩其孩子,则孩子不能同时展开以适合其父母。

Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible children (using Flexible rather than Expanded).考虑将 mainAxisSize 设置为 MainAxisSize.min 并使用 FlexFit.loose 适合灵活的孩子(使用 Flexible 而不是 Expanded)。 This will allow the flexible children to size themselves to less than the infinite remaining space they would otherwise be forced to take, and then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum constraints provided by the parent.这将允许灵活的子级将自身调整到小于他们将被迫占据的无限剩余空间,然后将导致 RenderFlex 收缩包裹子级而不是扩展以适应父级提供的最大约束。

and also the contents of my AppBar disappear, and I can no longer click the back arrow.而且我的 AppBar 的内容也消失了,我不能再单击后退箭头。

Here is the scaffold to build the page...这是构建页面的脚手架...

late StateSetter _setState;

@override
Widget build(BuildContext context) {

  return buildSubFormPage();

}


Widget buildSubPage() {

  return StatefulBuilder(
      builder: (bContext, StateSetter setBState) {
        _setState = setBState;
        return Scaffold(
            appBar: new AppBar(
              title: new Text("My list of items"),
              actions: [
                Padding(
                  padding: EdgeInsets.only(
                      right: 20.0),
                  child: openSubFormItem(
                      widget.field, 0,
                      "+ Add new", null),
                )
              ],
            ),
            body: ListView.builder(
              padding: EdgeInsets.all(8.0),
              //shrinkWrap: true,
              itemCount: subFormItems.length,
              itemBuilder: (context, index) {
                return Container(
                  //width: screenWidth,
                  child: openSubFormItem(
                      widget.field, index, "Press Here",
                      subFormItems[index]),

                );
              },

            )

        );
      }
  );
}

Here is mostly the content of my openSubFormItem widget.这里主要是我的 openSubFormItem 小部件的内容。

The building of the button text has been removed, but you can get the idea...按钮文本的构建已被删除,但您可以理解...

Widget openSubFormItem(PendigoFields field, int index, String buttonText, dynamic subFormItem) {

String outputText = "A long string of text, that will surely cause the warning bars if I can't make it wrap properly";

if (buttonText == "+ Add new") {
  outputText = buttonText;
}

return Container(
    //padding: EdgeInsets.all(4),
  //width: screenWidth - 30,
    child:
    TextButton(
      child: Row(
        mainAxisSize: MainAxisSize.max,
        //mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          // Flexible(
          //   fit : FlexFit.loose,
          //   child: Text(outputText, style: TextStyle(color: Colors.black,
          //     fontSize: 15.0,
          //     fontWeight: FontWeight.bold,),),
          // ),

          Expanded(child: Text(outputText, style: TextStyle(color: Colors.black,
            fontSize: 15.0,
            fontWeight: FontWeight.bold,),),
          ),

          // Text(outputText, style: TextStyle(color: Colors.black,
          //   fontSize: 15.0,
          //   fontWeight: FontWeight.bold,),),

          // I also have an IconButton here, removed for clarity

       ]
      ),
    )
    decoration: buttonText == "Press Here" ? BoxDecoration(
      
      border: Border(
          bottom: BorderSide(color: Colors.purple[400]!)),
    )
        : null
);

}

How do I get my AppBar to show the AppBar content (and button) and allow my ListView to be able to wrap the text if the text is too long?如果文本太长,如何让我的 AppBar 显示 AppBar 内容(和按钮)并允许我的 ListView 能够换行文本?

As the error says: unbounded width, this is because the Row parent which is the TextButton does not have a specified width, and when using Expanded, the Row or the Column parent of it must know its constrains.错误说:unbounded width,这是因为TextButton的Row父级没有指定宽度,使用Expanded时,它的Row或Column父级必须知道它的约束。

Give a width to the TextButton.给 TextButton 一个宽度。

Also a note, don't create your widgets using methods as this is not recommend from flutter team, use separated widgets instead.另外请注意,不要使用方法创建小部件,因为 flutter 团队不建议这样做,而是使用单独的小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM