Search code examples
flutterflutter-layout

Flutter, how to set max height to list view items


what I have tried is below.

         SizedBox(
           width: MediaQuery.of(context).size.width * 0.88,
           height: MediaQuery.of(context).size.height * 0.42,
             child: ListView.builder(
                   itemCount: 10,                          
                   itemBuilder: (BuildContext context, int index) {
                            return _buildListItem();
                         }),
                   )

and the list widget

 Widget _buildListItem() {
    return ConstrainedBox(
      constraints: BoxConstraints(
        minHeight: 20,
          maxHeight: 120),
       child:Container(child:Text("Looooongggg textttttttttt 
              the text size is alwayysss different "),
       ),}

I want to make the list view item size dynamically, but also the max height is fixed at 120. In other words, no matter how long the text is, the container also wants to be to not go up to 120 or more.

This code always shows me only max height containers. No matter how short the letters are,

how could I achieve that?


Solution

  • Try IntrinsicHeight widget

    Widget _buildListItem() {
        return ConstrainedBox(
          constraints: BoxConstraints(
            minHeight: 20,
              maxHeight: 120),
           child: IntrinsicHeight(child:Container(child:Text("Looooongggg textttttttttt 
                  the text size is alwayysss different "),
           )),}