child:Column(
children: <Widget>[
Container(
height: double.infinity,
width: 100.0,
color: Colors.red,
child: Text('hello'),
),)
in this,when i make height: double.infinity,it gives error in run saying BoxConstraints forces an infinite height. but when i give height manually it work fine.
can anyone explain me why this happening.
BoxConstraints forces an infinite height
You're asking to render an infinite height object without a height constraint... Flutter can't do that.
Column lays out children in two phases:
non-Flex items (anything not Expanded, Flexible or Spacer)
Flex items (Expanded,Flexible, Spacer only)
Column's phase 1 vertical layout is done in unbounded space. That means:
Flex items share the remaining/leftover space
double.infinity height will expand to use up the remaining spaceHere's an example of using infinite height on a Container inside a Column, which is fine:
class ColumnInfiniteChildPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
Flexible(
child: Container(
height: double.infinity, // ← perfectly fine
child: Text('Column > Container > Text')),
),
Text('Column > Text')
],
),
),
);
}
}
Remove the Flexible and the error will be thrown.
How about this one.
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
children: <Widget>[
Expanded(
child: Container(
// height: double.infinity,
width: 100.0,
color: Colors.red,
child: Text('hello'),
),
),
],
),
),
);
}
}

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With