【问题标题】:A RenderFlex overflowed by x pixels on the right右侧溢出 x 个像素的 RenderFlex
【发布时间】:2021-08-13 17:28:22
【问题描述】:

我试图用以下代码显示项目列表,我试图用Expanded 包装这个Column,但得到了同样的错误。

class QuizDoneByStudent extends StatelessWidget {
  const QuizDoneByStudent({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: kDarkGrey2,
      body: SafeArea(
        child: Container(
          child: Column(
            children: [
              Stack(
                clipBehavior: Clip.none,
                children: [
                  Container(
                    height: size.height * .2,
                    decoration: BoxDecoration(
                        image: DecorationImage(
                            image:
                                NetworkImage("https://imgur.com/Mr6Rozm.png"))),
                  ),
                  Positioned(
                    bottom: -20,
                    left: 20,
                    child: CircleAvatar(
                      radius: 35,
                      backgroundColor: kLightGreyColor,
                      backgroundImage:
                          NetworkImage("https://i.imgur.com/MGcuXst.png"),
                    ),
                  ),
                  Positioned(
                    bottom: -33,
                    left: 100,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          "DonaldTrump",
                          style: TextStyle(
                              color: Colors.white,
                              fontWeight: FontWeight.bold,
                              fontSize: 20),
                        ),
                        SizedBox(
                          height: 10,
                        ),
                        Text(
                          "@fintory",
                          style: TextStyle(
                            color: Colors.white,
                          ),
                        )
                      ],
                    ),
                  )
                ],
              ),
              SizedBox(
                height: 50,
              ),
              PostQuizStatListView()
            ],
          ),
        ),
      ),
    );
  }
}

class PostQuizStatListView extends StatelessWidget {
  const PostQuizStatListView({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: ListView.separated(
          shrinkWrap: true,
          itemBuilder: (BuildContext context, int index) {
            return PostQuizStat();
          },
          separatorBuilder: (BuildContext context, int index) => SizedBox(
                height: 10,
              ),
          itemCount: 5),
    );
  }
}

class PostQuizStat extends StatefulWidget {
  const PostQuizStat({
    Key? key,
  }) : super(key: key);

  @override
  _PostQuizStatState createState() => _PostQuizStatState();
}

class _PostQuizStatState extends State<PostQuizStat> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(10),
      decoration: BoxDecoration(
          border: Border.all(color: kLightGreyColor),
          borderRadius: BorderRadius.circular(15)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          CachedNetworkImage(
            placeholder: (context, url) =>
                Image.asset("assets/images/post_placeholder.png"),
            imageUrl: "https://imgur.com/uHZ3Mim.png",
          ),
          SizedBox(
            width: 10,
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                "The Future of sdsfd sd ffsdf sdfsdf d ",
                style:
                    TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
              ),
              SizedBox(
                height: 5,
              ),
              Text(
                "5 - 8 - 2021     14:30",
                style: TextStyle(color: Colors.white),
              )
            ],
          ),
          SizedBox(
            width: 10,
          ),
          Text(
            "88%",
            style: TextStyle(
                color: Colors.white, fontWeight: FontWeight.bold, fontSize: 30),
          )
        ],
      ),
    );
  }
}

我遇到了这个溢出问题

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    ` 文本(

     "The Future of sdsfd sd ffsdf sdfsdf d ",
                style:
                    TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
              ),`
    

    正在溢出。没有足够的空间来显示整个文本。您可以将其设为多行或使用 Text 的 overflow 属性

    【讨论】:

      【解决方案2】:

      尝试在 Expanded 或 Flex Widget 中添加您的文本小部件,请参阅我的回答 here

      Expanded (
         child: Text (),
        ),
      

      【讨论】:

      • 现在我得到了不同的错误RenderFlex children have non-zero flex but incoming height constraints are unbounded.。截图在这里i.imgur.com/xfrRhP9.jpg
      • 尝试在列小部件中添加mainaxissize.min
      【解决方案3】:

      只需将列包围起来,如下所示

        Expanded(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                "The Future of sdsfd sd ffsdf sdfsdf d ",
                style:
                TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
              ),
              SizedBox(
                height: 5,
              ),
              Text(
                "5 - 8 - 2021     14:30",
                style: TextStyle(color: Colors.white),
              )
            ],
          ),
        ),
      

      【讨论】:

        【解决方案4】:

        用扩展的或灵活的方式包装这些文本小部件:

        
        Flexible(child: Text(
                        "The Future of sdsfd sd ffsdf sdfsdf d ",
                overflow: TextOverflow.fade,
                style:
                      TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
                      ),
        )
        

        和同一列中的其他长的。 你也可以用flexible包裹它,并使用overflow:Textoverflow.elipsis

        【讨论】:

        • 现在我得到了不同的错误RenderFlex children have non-zero flex but incoming height constraints are unbounded.。截图i.imgur.com/xfrRhP9.jpg
        • 改用灵活。
        • 和列 mainaxissize.min
        • 谢谢,@Huthaifa。但仍然无法正常工作,我现在正在使用Flexible 和`mainAxisSize: MainAxisSize.min`。 imgur.com/4RclOIZ
        猜你喜欢
        • 2021-09-07
        • 1970-01-01
        • 1970-01-01
        • 2021-09-27
        • 2019-06-08
        • 1970-01-01
        • 1970-01-01
        • 2021-10-06
        • 2019-03-20
        相关资源
        最近更新 更多