如何解决卡片小部件中的“ RenderBox未布局:”

时间:2018-08-12 13:52:23

标签: flutter flutter-layout

我有一张有三个容器的卡。前两个包含文本,最后一个包含文本旁边的TextFormField。所以我要排成一排,将两者保持在一起。唯一的事情是,当我添加TextFormField小部件时,它没有出现,并且控制台抛出的错误显示了

══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY 
╞═════════════════════════════════════════════════════════
I/flutter (14101): The following assertion was thrown during 
performLayout():
I/flutter (14101): BoxConstraints forces an infinite width.
I/flutter (14101): These invalid constraints were provided to 
RenderRepaintBoundary's layout() function by the
I/flutter (14101): following function, which probably computed the invalid 
constraints in question:
I/flutter (14101):   _RenderDecoration._layout.layoutLineBox 
(package:flutter/src/material/input_decorator.dart:750:11)
I/flutter (14101): The offending constraints were:
I/flutter (14101):   BoxConstraints(w=Infinity, 0.0<=h<=100.0)



I/flutter (14101): Another exception was thrown: RenderBox was not laid out: 
RenderPadding#150b0 relayoutBoundary=up3 NEEDS-PAINT
I/flutter (14101): Another exception was thrown: 
'package:flutter/src/rendering/shifted_box.dart': Failed assertion: line 310 
pos 12: 'child.hasSize': is not true.
I/flutter (14101): Another exception was thrown: RenderBox was not laid out: 
RenderPhysicalShape#1d998 relayoutBoundary=up4

代码类似于

import 'package:flutter/material.dart';

class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Container(
    child: Center(
  child: Card(
    elevation: 2.0,
    child: Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Container(
          height: 100.0,
          color: Colors.purple,
        ),
        Container(
          height: 200.0,
          color: Colors.pink,
          child: Column(
            children: <Widget>[
              new RichText(
                textAlign: TextAlign.center,
                text: TextSpan(
                  text: 'Some Text',
                  style: TextStyle(
                      color: Colors.grey[800],
                      fontWeight: FontWeight.bold,
                      fontSize: 17.0),
                ),
              ),
              new RichText(
                softWrap: true,
                textAlign: TextAlign.center,
                text: TextSpan(
                  text:
                      'Some other text',
                  style: TextStyle(
                      color: Colors.black54,
                      fontWeight: FontWeight.normal,
                      fontSize: 17.0),
                  ),
                    ),
                  Row(
                children: <Widget>[
                  Text('adfa'),
                  Text('data'),
                  Form(
                    child: TextFormField(),
                  ),
                ],
              )
            ],
          ),
        ),
      ],
    ),
  ),
));
}
 }

5 个答案:

答案 0 :(得分:22)

花费大量时间后,我找到了以下解决方案:

请在shrinkWrap: true内添加ListView.builder()

它帮助了我。

答案 1 :(得分:18)

错误原因:

Row沿水平方向扩展,TextField也沿水平方向扩展,因此我们需要限制TextField的宽度,您可以尝试以下任一种方法。

  1. 使用Expanded

     Row(
      children: <Widget>[
        Expanded(child: TextField()),
      ],
    )
    
  2. 使用Flexible

    Row(
      children: <Widget>[
        Flexible(child: TextField()),
      ],
    )
    
  3. 将其包装在ContainerSizedBox中并提供width

    Row(
      children: <Widget>[
        SizedBox(width: 100, child: TextField()),
      ],
    )       
    

答案 2 :(得分:7)

TextFormField引起了问题。它需要限制宽度。 例如。将其包装到扩展的窗口小部件或具有宽度的容器中。

答案 3 :(得分:2)

我在German Saprykin帖子的增强中回复,下面的错误我也得到了相同的提示

  

════════(2)渲染库捕获异常   ══════════════════════════   InputDecorator,   通常由TextField创建,不能具有无界   宽度。当父窗口小部件不提供有限时会发生这种情况   宽度约束。例如,如果InputDecorator包含在   行,然后必须限制其宽度。扩展小部件或   SizedBox可用于约束InputDecorator的宽度或   包含它的TextField。   'package:flutter / src / material / input_decorator.dart':失败的断言:   910行pos 7:'layoutConstraints.maxWidth      

════════(3)渲染库捕获到异常   ══════════════════════════   RenderBox不是   布置:_RenderDecoration#b1ce0 relayoutBoundary = up26 NEEDS-PAINT   需求组成位更新   'package:flutter / src / rendering / box.dart':断言失败:第1681行   pos 12:“ hasSize”用户创建的导致错误的小部件的祖先   原为:TextField   文件:/// C:/CommBack/My_Workplace/Flutter/wiremusic/wiremusic_dev/lib/wire/widgets/searchfield.dart:15:14   ══════════════════════════════════════════════════ ════════

这是因为在下面的较早的源代码行中。

return Container(
  color: Colors.yellow,
  constraints: BoxConstraints(minWidth: 230.0, minHeight: 25.0),
  child: TextField(),
);

因为TextField明确要求祖先hasSize,并且在明确向Container提到宽度之后,错误消失了,就像 Thanos

return Container(
  color: Colors.yellow,
  width: 230,
  constraints: BoxConstraints(minWidth: 230.0, minHeight: 25.0),
  child: TextField(),
);

希望这会对某人有所帮助。

答案 4 :(得分:1)

如果您使用 ListView.builder 并收到此错误,则这就是解决方案

在builder中使用==shrinkWrap: true,

ListView.builder(
   itemCount: historyList.length,
   shrinkWrap: true, ///////////////////////Use This Line 
   itemBuilder: (BuildContext context, int index) {
        return historyListItem(historyList[index]['text'], 60, () {}, false, size);
    },
 )