简体   繁体   中英

How to set dynamic height for Container Widget in flutter?

I have text widget inside of container and if text is more than 4 lines this is overflowing to bottom of container. How can I set dynamic height for this container that will depends of text lines and avoid bottom overflow ? At the moment container have fixed value but if I remove this value it will disappear.

在此处输入图像描述

home component 

Widget build(BuildContext context) {
 return Container (
   child:    SliverToBoxAdapter(
                        child: Container(
                          height: 180,
                          margin: EdgeInsets.only(top: 10),
                          child: CustomeInfoWidget(
                            infoData: _infoDataList,
                          ),
                        ),
                      ),
) 
}

CustomeInfoWidget

Widget build(BuildContext context) {
    final double _width = MediaQuery.of(context).size.width;

    return Container(
      margin: EdgeInsets.only(top: 15, left: 15, right: 15),
      child: Stack(
        children: [
          PageView.builder(
            itemCount: widget._infoDataList.length,
            controller: _pageController,
            
            itemBuilder: (BuildContext context, int index) {
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    width: _width - 10,
                    child: Padding(
                      padding: const EdgeInsets.only(bottom: 10),
                      child: Text(widget._infoDataList[index].title,
                          textAlign: TextAlign.left,
                         
                  ),
                  Container(
                    child: Text(
                      widget._infoDataList[index].content,
                    
                    ),
                  )
                ],
              );
            },
          ),
        
        ],
      ),
    );
  }

使用 Singlechildscrollview 包装容器或为 Text 小部件设置溢出Flutter - 在溢出时包装文本,例如插入省略号或淡入淡出

On your home component, you are fixing the height of CustomeInfoWidget . You can remove this.

 SliverToBoxAdapter(
   child: Container(
    //  height: 180,// remove this
      margin: EdgeInsets.only(top: 10),
          child: CustomeInfoWidget(

You can use SliverFillRemaining for PageView .

SliverFillRemaining(
  child:  PageView.builder(

Or you can wrap your text container with Singlechildscrollview for inner scroll.

You can also calculate the text height and provide it.

How can I get the size of the Text widget in Flutter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM