Home > Enterprise >  Why Can't I Define the Height of an Inner Container?
Why Can't I Define the Height of an Inner Container?

Time:10-09

No matter what I do I can't seem to adjust the height of a nested Container in my flutter app. It always seems to inherit the height of the parent Container. Everything I read online said to wrap the inner container with a Center(). I tried this but still ended up with the same issue.


    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Container(
          padding: EdgeInsets.symmetric(horizontal: 5.0, vertical: 16.0),
          height: 200,
        child: ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: _recommendations.length,
          itemBuilder: (context, index) {
            return Container(
              width: 150,
              height: 100,
              child: Card(
                color: Colors.blue,
                child: Center(
                  child: Text(_recommendations[index])
                )
              )
            );
          }
        )

      )
      );

CodePudding user response:

Usually, a ListView widget tries to fill all the available space given by the parent element, even when the list items would require less space. You can disable this feature using shrinkWrap parameter of ListView as follow:\

ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: _recommendations.length,
          shrinkWrap:true,
          itemBuilder: (context, index) {
            return Container(
              width: 150,
              height: 100,
              child: Card(
                color: Colors.blue,
                child: Center(
                  child: Text(_recommendations[index])
                )
              )
            );
          }
        )

CodePudding user response:

I tried to reproduce the code, and it was needed _recommendations defined.

  • Related