将容器缩小 child 而不是扩展以填充 parent
Shrink container to smaller child rather than expanding to fill parent
我正在为段控件创建自定义小部件。
return Padding(
padding: const EdgeInsets.symmetric(vertical: 20.0),
child: Container(
decoration: BoxDecoration(border: Border.all(color: Colors.blue)),
child: Row(
mainAxisSize: MainAxisSize.min,
children: buttons,
),
),
);
我希望容器缩小到最小尺寸以容纳其 children。但是,当我将小部件包含在 parent 中时,它会扩展以填充 parent。这是可见的,因为装饰的边框比按钮大。
如何强制收缩容器?
为了让容器不占用整个父级,您需要告诉它应该放在父级中的什么位置——默认情况下,它不知道该放哪里,所以它会填充父级 =D。
最简单的方法是使用 Align
or a Center
小部件。在这种情况下,我相信你想把它放在你的 Container
周围,但我不是 100% 确定。
聚会有点晚了...我还是 flutter 的新手所以对我放轻松,但我认为使用 Align
或 Center
作为 Parent
然后为 Child
设置 Width
和 Height
值,那么父项应该缩小到子项。 =D
将容器包装在 Row() 小部件中会使容器缩小到此处提到的容器的子大小,,
https://github.com/flutter/flutter/issues/4949
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
child:button()
),
]
),
Container(
height: 16,
// width: 50,
padding: EdgeInsets.all(3),
decoration: BoxDecoration(
color: Color(0xffeeeeee),
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: []
),
)
)
只需将 Align
小部件添加到容器中,它就会修复子小部件
我正在为段控件创建自定义小部件。
return Padding(
padding: const EdgeInsets.symmetric(vertical: 20.0),
child: Container(
decoration: BoxDecoration(border: Border.all(color: Colors.blue)),
child: Row(
mainAxisSize: MainAxisSize.min,
children: buttons,
),
),
);
我希望容器缩小到最小尺寸以容纳其 children。但是,当我将小部件包含在 parent 中时,它会扩展以填充 parent。这是可见的,因为装饰的边框比按钮大。
如何强制收缩容器?
为了让容器不占用整个父级,您需要告诉它应该放在父级中的什么位置——默认情况下,它不知道该放哪里,所以它会填充父级 =D。
最简单的方法是使用 Align
or a Center
小部件。在这种情况下,我相信你想把它放在你的 Container
周围,但我不是 100% 确定。
聚会有点晚了...我还是 flutter 的新手所以对我放轻松,但我认为使用 Align
或 Center
作为 Parent
然后为 Child
设置 Width
和 Height
值,那么父项应该缩小到子项。 =D
将容器包装在 Row() 小部件中会使容器缩小到此处提到的容器的子大小,, https://github.com/flutter/flutter/issues/4949
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
child:button()
),
]
),
Container(
height: 16,
// width: 50,
padding: EdgeInsets.all(3),
decoration: BoxDecoration(
color: Color(0xffeeeeee),
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: []
),
)
)
只需将 Align
小部件添加到容器中,它就会修复子小部件