gpt4 book ai didi

Flutter 容器布局行为

转载 作者:IT王子 更新时间:2023-10-29 06:40:30 24 4
gpt4 key购买 nike

当我阅读 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.

所以我写了一些像下面这样的代码,我认为第二个容器应该尽可能小,但它填满了应用程序的内容区域(整个屏幕),为什么?

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,
),
),
),
);
}
}

最佳答案

您设置了高度和宽度,尝试使用 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,
),
),
),

更多信息:https://docs.flutter.io/flutter/widgets/UnconstrainedBox-class.html

关于Flutter 容器布局行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53003020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com