I want to squeeze a child so that it fit to the height of another child.
So I need to transform this:
to this:
My code is as follows:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(
home: HomePage(),
));
}
class HomePage extends StatelessWidget {
const HomePage({
super.key,
});
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Row(
children: [
Expanded(
child: Image.asset('assets/test.jpg'),
),
Container(
width: 200,
height: 150,
color: Colors.green,
),
],
)));
}
}
I know how to stretch the child to the row height. But I would like to squeeze child.
Edits: The green box has a dynamic height.
2
Answers
Since you didn’t specified if the second children (green box) will have a dynamic height, the easier way to achieve that is giving the row a fixed height
I would use a very handy package
boxy
for that as follows:Instead
Row
I’m using aBoxyRow
, which enables us to use theboxy
‘s goodies likeDominant
. Then, I wrap the widget that I want to dictate the cross-axis (inRow
the cross-axis is the vertical axis – height). I also made theImage
‘sfit
aBoxFit.contain
, so that it resizes its contents.As for the example, I wrapped the whole scaffold’s body in
Center
, so that the height constraints are loose.Scaffold
‘sbody
has tight constraints, but in real-world scenario I reckon you’d put this widget into aColumn
or some scrollable likeListView
, where the height constraints are also loose.Read more in the
boxy
‘s README.I recommend the
boxy
package greatly, as it has also a very powerfulCustomBoxy
, which is basically an abstraction over aRenderObject
layer with a much much higher Developer Experience.