【问题标题】:BoxConstraints forces an infinite height[error]BoxConstraints 强制无限高[错误]
【发布时间】:2022-01-04 17:41:10
【问题描述】:

这是我的代码.. 我收到错误消息说“BoxConstraints 强制无限高”。然后是一个描述,我是新来的,我没有办法解决这个问题。

  body:SingleChildScrollView(
    child: Stack(
      fit: StackFit.expand,
      children: [
        Image(
          color: Colors.black12,
          colorBlendMode: BlendMode.darken,
          image: AssetImage('images/Alone.jpg'),
          fit: BoxFit.cover,
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Show('Alone is Stronger', 'y'),
          ],
        )
      ],
    ),
  ),

这是我的 Show 小部件。

Widget Show(String x,String y){
return
    Container(
    margin: EdgeInsets.fromLTRB(10, 5, 10, 2),
    width: 400,
    decoration: BoxDecoration(
      color: Colors.white10,
      borderRadius: BorderRadius.circular(10),
    ),
    padding: EdgeInsets.all(10),
    child: Text(
      '$x',
          style: TextStyle(
        fontSize: 20,
         fontWeight: FontWeight.bold,
      color: Colors.yellowAccent,
           ),
        ),
     ); 
  }

很多问题已经在这里,但我没有找到问题所在。 提前致谢

【问题讨论】:

  • 您是否要在正文上添加背景图片?
  • @yeasinSheikh,是的,我希望将背景图像固定在正文上,并且文本部分可滚动。

标签: flutter dart widget


【解决方案1】:

从您的代码-sn-p 中,我可以理解您正试图拥有body 的背景。

SingleChildScrollView 提供无限高度,而Stack 是其子级并使用父级大小,它将获得无限高并通过错误。

在您的情况下,小部件结构将是

  body: Stack(
        fit: StackFit.expand,
        children: [
          Image(
            color: Colors.black12,
            colorBlendMode: BlendMode.darken,
            image: AssetImage('images/Alone.jpg'),
            fit: BoxFit.cover,
          ),
          SingleChildScrollView(
              child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Show('Alone is Stronger', 'y'),
            ],
          ))
        ],
      ),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-15
    • 2020-12-22
    • 2023-04-09
    • 2020-04-27
    • 2019-11-04
    • 2019-12-02
    • 2019-02-25
    • 2020-09-21
    相关资源
    最近更新 更多