flutter flutter Listview -> Container \'width\' 不起作用?
\n/示例/\n\xe3\x85\xa1\xe3\x85\xa1\xe3\x85\xa1\xe3\x85\xa1\xe3\x85\xa1\xe3\x85\xa1\xe3\x85\xa1\xe3\ x85\xa1\xe3\x85\xa1\xe3\x85\xa1\xe3\x85\xa1\xe3\x85\xa1
\nscaffold \nbody : ListView\nchildren : [\n Container (\n width: 100, // Not Working \n height: 100, // Ok\n color: Colors.red.\n ),\n]\nRun Code Online (Sandbox Code Playgroud)\n!!! 但 !!!
\nscaffold \nbody : ListView\nchildren : [\n Stack(\n children : [\n Container (\n width: 100, // OK\n height: 100, // Ok\n color: Colors.red.\n ),\n ]\nRun Code Online (Sandbox Code Playgroud)\n为什么 ??
\n\nRav*_*til 13
尝试下面的代码,将容器包裹在里面UnconstrainedBox()
ListView(
shrinkWrap: true,
children: [
UnconstrainedBox(
child: Container(
width: 100, // OK
height: 100, // OK
color: Colors.red,
),
)
],
),
Run Code Online (Sandbox Code Playgroud)
下面的代码也可以工作。我们还可以使用对齐小部件。
ListView(
shrinkWrap: true,
children: [
Align(
alignment: Alignment.topLeft,
child: Container(
width: 100,
height: 100,
color: Colors.red,
),
)
],
)
Run Code Online (Sandbox Code Playgroud)
还要检查约束在颤振中的工作原理。https://docs.flutter.dev/development/ui/layout/constraints
当您使用stack小部件时,这意味着您是overlapping您的父小部件,因此在您的情况下,由于您的父小部件,您无法获得适当的宽度,但当您使用堆栈时,Container由于它是重叠的,因此您可以获得足够的空间。
| 归档时间: |
|
| 查看次数: |
2368 次 |
| 最近记录: |