CoderUni
CoderUni

Reputation: 6134

Can't use MediaQuery inside a ListView

I am trying to make my container take up 80% of the space available. It works when I replace ListView with a Column but it doesn't work when I use a ListView. Why does it behave like that?

Here is my code:

ListView(
 children: <Widget>[
  Container(
   width: MediaQuery.of(context).size.width * .8,
  )
 ]
)

Upvotes: 1

Views: 387

Answers (1)

CoderUni
CoderUni

Reputation: 6134

Big credit to pskink for his solution. He solved it by using a Row widget along with three Expanded widgets.Setting the Flex property of each Expanded widget allows me to make my container to take 80% of the width. Here is how I did it based on @pskink's Solution:

Row(
 children: <Widget>[
  Expanded(
    flex: 1,
    child: SizedBox(),
  ),
  Expanded(
    flex: 8,
    child: Container()
  ),
  Expanded(
    flex: 1,
    child: SizedBox(),
  ),
 ]
)

Upvotes: 1

Related Questions