Home > Software design >  Flutter FittedBox site ratio
Flutter FittedBox site ratio

Time:09-04

I have many FittedBoxes in a Gridview. The fitted box automatically has a width:100%. I want to set a site ratio for the FittedBox. Is there a way to set it or get the width to set the height?

Container(
  height:?,
  child: FittedBox(
    fit: BoxFit.fill,
    child: Image.network(''),
  )
);

EDIT: The ratio is 1/1. Why is that?

AspectRatio(
  aspectRatio: 16/9,
  child: FittedBox(
    fit: BoxFit.fill,
    child: Image.network('')),
);

CodePudding user response:

The aspect ratio is defined in the gridDelegate that is setup in the GridView.builder. It overwrites the dimensions defined in the child widget. If e.g. SliverGridDelegateWithFixedCrossAxisCount is used as gridDelegate the aspect ratio for the children is set with parameter childAspectRatio.

gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 4, childAspectRatio: 2),

CodePudding user response:

yes, you can use the AspectRatio widget

AspectRatio(
 aspectRatio: 1 / 1
 Child: YourWidget(),
 )

this will force the child widget to be a square by the aspect ratio of 1, for better getting of information, try on your custom widget any divisions like 16/9,, 9/16, 4/3...

Hope this will help

  • Related