根据您的问题,这是这里出了什么问题的概念。
Column 的第一个孩子是 Container with shadow。 Shadow 呈现超出定义的大小。如果您在此Container 之后不提供任何空格,我们将无法看到阴影。这个空间可以通过Container margin、SizedBox或wrapping our list with Padding. But now our main question is how we get shadow while index=0. I believe it is coming from ListChildren`来完成。它们包含上部空格,这就是为什么我们只能看到第一次。
在 Ui 上渲染优先级从底部到顶部。
如何解决这个问题:
我们可以在容器底部提供空间或分配margin(不是填充),或者在container 之后使用SizedBox,提供与shadow 相同的高度。
- 在容器上提供底部
margin。
- 添加具有阴影高度的 SizedBox。
- 用
Padding 包装我们的列表并提供top:。
在这张图片中,
我们的shadow: white、background:amber。
演示代码:
import 'package:flutter/material.dart';
class ColumnWithContainerShadow extends StatelessWidget {
const ColumnWithContainerShadow({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.amber,
body: Column(
children: [
Container(
height: 50,
width: double.infinity,
////* we dont need margin if we have padding on ListView
// margin: EdgeInsets.only(bottom: 12),
decoration: BoxDecoration(
color: Colors.green,
boxShadow: [
BoxShadow(
offset: Offset(0, 12),
color: Colors.white,
)
],
),
child: Center(child: Text("Container A")),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(top: 12),
child: ListView(
children: [
...List.generate(
333,
(index) => Container(
/// enable this margine and remove other spaces to see 1st child shadow.(shadow depend on children position)
// margin: EdgeInsets.only(top: 12),
height: 60,
color: Colors.deepPurple,
child: Text("$index"),
),
)
],
),
),
),
Container(
height: 50,
width: double.infinity,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.green,
boxShadow: [
BoxShadow(
offset: Offset(0, 12),
color: Colors.white,
)
],
),
child: Text("Bottom Container"),
),
// Comment to close shadow
SizedBox(
height: 20,
)
],
),
);
}
}