Constraints.hasBoundedWidth':不为真 [英] 'constraints.hasBoundedWidth': is not true

查看:16
本文介绍了Constraints.hasBoundedWidth':不为真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码

main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Student Info",
      debugShowCheckedModeBanner: false,
      home: SafeArea(
        child: HomePage(),
      ),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: ListView(
        children: <Widget>[StudentScreenAppBar(), StudentGraduateList()],
      ),
    );
  }
}

class StudentScreenAppBar extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        AppBar(
          leading: IconButton(
              icon: Icon(
                Icons.arrow_back_ios,
                color: Colors.black,
                size: 20.0,
              ),
              onPressed: () {}),
          centerTitle: true,
          title: Text(
            "Student Info",
            style: TextStyle(color: Colors.black),
          ),
          actions: <Widget>[
            IconButton(
                icon: Icon(
                  Icons.settings,
                  size: 20.0,
                  color: Colors.black,
                ),
                onPressed: () {}),
//        SizedBox(width: 10.0,),
            IconButton(
                icon: Icon(
                  Icons.settings_ethernet,
                  color: Colors.black,
                  size: 20.0,
                ),
                onPressed: () {})
          ],
          elevation: 0.0,
          backgroundColor: Colors.white,
        ),
        Container(
            decoration: BoxDecoration(border: Border.all(color: Colors.black)),
            padding: const EdgeInsets.fromLTRB(8.0, 2.0, 8.0, 2.0),
            child: Image.asset(
              "images/isdi_school.png",
              width: 40.0,
              height: 15.0,
              fit: BoxFit.contain,
            ))
      ],
    );
  }
}

class StudentGraduateList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.fromLTRB(0.0, 20.0, 0.0, 0.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          yearListForUgPg("Under Graduate"),
          yearListForUgPg("Post Graduate")
        ],
      ),
    );
  }

  Widget yearListForUgPg(String graduationName) {
    return Column(
      children: <Widget>[
        Container(
          padding: const EdgeInsets.fromLTRB(25.0, 4.0, 25.0, 4.0),
          child: Text(
            graduationName,
            style:
                TextStyle(color: Colors.white, fontFamily: "suisseintlMedium"),
          ),
          decoration: BoxDecoration(color: Colors.black),
        ),
        ListView.builder(
          shrinkWrap: true,
          itemBuilder: (context, index) {
            return Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  "2018",
                  style: TextStyle(
                      fontFamily: "okomitoBold", color: Colors.black),
                ),
                Icon(Icons.arrow_forward)
              ],
            );
          },
          itemCount: 10,
        )
      ],
    );
  }
}
ListView小部件不会显示。如果我注释ListView,则代码运行良好,并且我能够看到UI。我尝试在扩展和灵活的Widget中包装ListView,但不起作用。

如果我取消对ListView的注释,则会收到错误,说'constraints.hasBoundedWidth':不是真的。我正试图在我的用户界面中实现这样的功能:

![图像][1]

推荐答案

只需将-Column包装在Expanded小部件中:这样您Column将尝试占用父级Row中的可用空间。

更新代码:

class StudentGraduateList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.fromLTRB(0.0, 20.0, 0.0, 0.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          Expanded(child: yearListForUgPg("Under Graduate")),
          Expanded(child: yearListForUgPg("Post Graduate"))
        ],
      ),
    );
  }

这篇关于Constraints.hasBoundedWidth&#39;:不为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆