Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Container Layout behavior

when i read the Container layout behavior of Container document:

If the widget has no child, no height, no width, no constraints, and the parent provides unbounded constraints, then Container tries to size as small as possible.

so i write some code like below ,i think the second container should be as small as possible,but it fill the application’s content area (the entire screen) ,why?

class ContainerWithScaffold extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
  title: 'Container with scaffold',
  home: Scaffold(
    body: Container(
      color: Colors.blue,
      height: double.infinity,// provides unbounded height constraints for the child container
      width: double.infinity,// provides unbounded width constraints for the child container
      child: new Container(
        color: Colors.white,
      ),
    ),
  ),
);
}
}
like image 866
lgw150 Avatar asked Aug 14 '26 04:08

lgw150


1 Answers

You set a height and width, try using UnconstrainedBox

      Container(
                color: Colors.blue,
                height: double
                    .infinity, // provides unbounded height constraints for the child container
                width: double
                    .infinity, // provides unbounded width constraints for the child container
                child: UnconstrainedBox(
                  child: new Container(
                    color: Colors.red,
                  ),
                ),
              ),

More info : https://docs.flutter.io/flutter/widgets/UnconstrainedBox-class.html

like image 97
diegoveloper Avatar answered Aug 16 '26 17:08

diegoveloper