带有CrossAxisAlignment.stretch的Flutter Row引发错误BoxConstraints强制无限高度

时间:2019-07-24 13:27:19

标签: flutter dart alignment

不确定为什么会引发错误BoxConstraints forces an infinite height

 return Scaffold(
  body: Column(
    children: <Widget>[
      Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
           Text('OK'),
           Text('Then')
       ],
      )
    ],
  ),
);

没有CrossAxisAlignment.stretch没问题,但是我需要该行中的元素从屏幕顶部一直延伸到底部。

当尝试在单个文本小部件或包含文本小部件的列周围使用Expanded时,出现相同的错误。

2 个答案:

答案 0 :(得分:1)

这不会引发错误

return Scaffold(
  body: Column(
    children: <Widget>[
      Expanded(
          child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[Text('OK'), Text('Then')],
      ))
    ],
  ),
);

UPD: 如果您想拉长Text-我想,您不需要Column

return Scaffold(
  body: Row(
    mainAxisAlignment: MainAxisAlignment.center,
    crossAxisAlignment: CrossAxisAlignment.stretch,
    children: <Widget>[
      Container(
        alignment: Alignment.center,
        child: Text('OK'),
      ),
      Container(
        alignment: Alignment.center,
        child: Text('Then'),
      ),
    ],
  ),
);

答案 1 :(得分:0)

将CrossAxisAlignment.stretch移动到该列。

Column(
    mainAxisAlignment: MainAxisAlignment.center,
    crossAxisAlignment: CrossAxisAlignment.stretch,
    children: <Widget>[
      Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[Text('OK'), Text('Then')],
      )
    ],
  ),

让元素自上而下地重新组织一切

Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
      Expanded(
          child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            color: Colors.amber,
            child: Align(child: Text('OK')),
          ),
          Container(
              color: Colors.amberAccent, 
              child: Align(child: Text('Then')))
        ],
      ))
    ],
  ),

刚看到Andrey的回答,就意识到您甚至不需要CrossAxisAlignment.stretch

  Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
      Expanded(
          child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            height: double.infinity,
            color: Colors.amber,
            child: Align(child: Text('OK')),
          ),
          Container(
              height: double.infinity,
              color: Colors.amberAccent,
              child: Align(child: Text('Then')))
        ],
      ))
    ],
  ),