首页 > 解决方案 > 如何使容器高度取决于文本颤动的长度

问题描述

我想知道,如何让容器高度取决于文本的长度并将其覆盖在容器中而不是在图 2 中如果我将文本设置得太长,文本会出现在容器上方,那么如何修复它

b-akused02 的消息:它很短,但仍然在高度:100,

关于 b-akused01 的消息:容器已过期

b-akused03 的消息:它很短,仍然在高度:100,

Widget _buildCommentItem({@required CommentDetail commentDetail}) {
    return Container(
      height: 100,
      child: Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 20.0,
            ),
            Expanded(
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 20),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Expanded(
                      flex: 1,
                      child: Container(
                        height: 45, //height, //155,
                        width: 45, //width, //155,
                        decoration: BoxDecoration(
                          color: const Color(0xff7c94b6),
                          image: DecorationImage(
                            image: NetworkImage(
                                commentDetail.otherUserProfileImageUrl),
                            fit: BoxFit.cover,
                          ),
                          borderRadius: BorderRadius.circular(12),
                        ),
                      ),
                    ),
                    Expanded(
                      flex: 8,
                      child: Padding(
                        padding: EdgeInsets.symmetric(horizontal: 10),
                        child: Wrap(
                          // crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            FittedBox(
                              fit: BoxFit.cover,
                              child: Text(
                                commentDetail.otherUsername,
                                style: TextStyle(
                                    color: Colors.black,
                                    fontWeight: FontWeight.bold),
                              ),
                            ),
                            Text(
                              commentDetail.comment,
                              style: TextStyle(
                                color: Colors.black,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            SizedBox(
              height: 20,
            )
          ],
        ),
      ),
    );
  }

这是 Container 高度 100,文本覆盖在 100

这是 Container hight 100 并且文本比 100 长

标签: flutterflutter-layout

解决方案


将给定的容器包装在 Column 中,并从内部删除 height 参数。


推荐阅读