【问题标题】:Flutter: The following assertion was thrown during performResize(): Vertical viewport was given unbounded heightFlutter:在 performResize() 期间抛出了以下断言:垂直视口被赋予了无限的高度
【发布时间】:2021-07-16 08:28:32
【问题描述】:

编码和学习 Flutter 的超级新手。

尝试在 Flutter 中为将从云端调用数据的应用的新闻源发布模板。

飞镖代码中没有标记错误,但在运行时返回“垂直视口的高度没有限制。”。代码如下。Only appbar appears. body always blank

class Post extends StatelessWidget {
  final Post post;
  Post({newsfeed.post});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Card(
          elevation: 8.0,
          shadowColor: Colors.grey,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
          child: ListView(
            padding: const EdgeInsets.all(10.0),
            children: <Widget>[
              Row(
                children: [
                  Container(
                    child: ListTile(
                      leading: CircleAvatar(
                        radius: 20.0,
                        backgroundImage: NetworkImage(post.userimage),
                      ),
                      title: Text(
                        post.username,
                      ),
                      subtitle: Text("Post Date"),
                      trailing:
                          Icon(Icons.more_vert, color: Colors.purple[600]),
                    ),
                  ),
                ],
              ),
              SizedBox(height: 10.0),
              Row(children: [
                Container(
                  width: 250.0,
                  height: 200.0,
                  decoration: BoxDecoration(
                    image: DecorationImage(
                        fit: BoxFit.cover, image: NetworkImage(post.postimage)),
                    borderRadius: BorderRadius.all(Radius.circular(10.0)),
                  ),
                ),
                SizedBox(height: 10.0),
                Row(
                  children: [
                    Icon(Icons.textcomment, color: Colors.purple[600]),
                    Text(post.intcomments.toString()),
                    SizedBox(width: 20),
                    Icon(Icons.favorite, color: Colors.purple[600]),
                    SizedBox(width: 20),
                    Text(post.intlikes.toString()),
                  ],
                ),
                SizedBox(height: 10.0),
              ]),
            ],
          ),
        ),
      ],
    );
  }
}

【问题讨论】:

    标签: flutter flutter-layout flutter-web


    【解决方案1】:

    通过将宽度设置为ListTile 并将ListView 替换为Card 上的列将解决此问题。在网络上使用网络图像时,尝试使用specific render option,例如flutter run -d c --web-renderer html

    检查这个解决方案[更新]

    import 'package:flutter/material.dart';
    
    class Post extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return LayoutBuilder(
          builder: (context, constraints) => SingleChildScrollView(
            child: Column(
              children: [
                columnItem(constraints),
                columnItem(constraints),
                columnItem(constraints),
                columnItem(constraints),
              ],
            ),
          ),
        );
      }
    
      Card columnItem(BoxConstraints constraints) {
        return Card(
          elevation: 8.0,
          shadowColor: Colors.grey,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(
              20.0,
            ),
          ),
          child: Column(
            children: <Widget>[
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                    //removing extra padding
                    width: constraints.maxWidth - 30,
                    child: ListTile(
                      leading: CircleAvatar(
                        ///for netWork
                        radius: 20,
                        backgroundImage: NetworkImage(
                          "https://www.rd.com/wp-content/uploads/2017/09/01-shutterstock_476340928-Irina-Bg-1024x683.jpg",
                        ),
                      ),
                      title: Text(
                        "Name here ",
                      ),
                      subtitle: Text("Post Date"),
                      trailing: Icon(Icons.more_vert, color: Colors.purple[600]),
                    ),
                  ),
                ],
              ),
              SizedBox(height: 10.0),
              Row(
                children: [
                  Container(
                    width: 250.0,
                    height: 200.0,
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        fit: BoxFit.cover,
                        image: AssetImage("assets/image.jpg"),
                      ),
                      borderRadius: BorderRadius.all(Radius.circular(10.0)),
                    ),
                  ),
                  SizedBox(height: 10.0),
                  Row(
                    children: [
                      Icon(Icons.ac_unit, color: Colors.purple[600]),
                      Text("heiya "),
                      SizedBox(width: 20),
                      Icon(Icons.favorite, color: Colors.purple[600]),
                      SizedBox(width: 20),
                      Text("hi asddddddddddda"),
                    ],
                  ),
                  SizedBox(height: 10.0),
                ],
              ),
            ],
          ),
        );
      }
    }
    
    
    

    【讨论】:

    • 你好@Yeasin,感谢您的快速帮助!真的很感激。能够使一些内容出现。不过还是有一些小故障,CircleAvatar NetworkImage 没有出现,但帖子的图像出现了。并且列表不会滚动(云中有很多示例 json 帖子,但只出现了 2 个)。希望你能回答我的问题。非常感谢:)
    • 你好@Yeasin,我用更新后的代码,整个body变成空白,只返回“else if,return”error。我对Sliver List不熟悉,可以实现newsfeed吗用 SingleChildScrollView 小部件代替?非常感谢... T_T
    • 嗨@Yeasin,更新的代码已经工作了。非常感谢^_^
    猜你喜欢
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    • 2019-09-08
    • 2019-01-15
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多