How to take available space using container in Flutter

2.2k views Asked by At

I was trying to make an app with SingleChildScrollView and ListView using below code

SingleChildScrollView(
          child: Column(
            children: [
              maintile(),
              Container(
                padding: EdgeInsets.all(15.0),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                    topRight: Radius.circular(75.0),
                  ),
                ),
                child: ListView.builder(
                  shrinkWrap: true,
                  primary: false,
                  itemCount: items.length,
                  itemBuilder: (context, index) {
                    return listitem();
                  },
                ),
              ),
            ],
          ),
        

but the listview holding container does not take available space If I provide Expandable around it. is there any way to achieve Container take available space?

2

There are 2 answers

6
Maz341 On

Final Code:

final screenSize = MediaQuery.of(context).size;
return SafeArea(
  child: Scaffold(
      body: Container(
        height: screenSize.height,
        width: screenSize.width,
        child: Column(
          children: [
            maintile(),
            Expanded(
              child: Container(
                padding: EdgeInsets.all(15.0),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                    topRight: Radius.circular(75.0),
                  ),
                ),
                child: ListView.builder(
                  shrinkWrap: true,
                  scrollDirection: Axis.vertical,
                  itemCount: items.length,
                  itemBuilder: (context, index) {
                    return listitem();
                  },
                ),
              ),
            ),
          ],
        ),
      )
  ),
);
0
Milad jalali On

Use Expanded for Container parent like this :

Expanded(
      child: Container(
        child: Text("YOUR_WIDGET"),
      ),
    )