https://api.flutter.dev/flutter/widgets/Padding-class.html
Padding
可以给其子节点添加填充(留白),和内边距效果类似。
定义:
Padding({
...
EdgeInsetsGeometry padding,
Widget child,
})
EdgeInsetsGeometry
是一个抽象类,开发中,我们一般都使用EdgeInsets
类,它是EdgeInsetsGeometry
的一个子类,定义了一些设置填充的便捷方法。
EdgeInsets
我们看看EdgeInsets
提供的便捷方法:
fromLTRB(double left, double top, double right, double bottom)
:分别指定四个方向的填充。all(double value)
: 所有方向均使用相同数值的填充。only({left, top, right ,bottom })
:可以设置具体某个方向的填充(可以同时指定多个方向)。symmetric({ vertical, horizontal })
:用于设置对称方向的填充,vertical
指top
和bottom
,horizontal
指left
和right
。
示例
下面的示例主要展示了EdgeInsets
的不同用法,比较简单,源码如下:
Padding(
padding: EdgeInsets.all(20.0), //全边距 20.0
// padding: EdgeInsets.fromLTRB(5.0, 10.0, 15.0, 20.0), //左、上、右、下 边距
// padding: EdgeInsets.only(left: 5.0, top: 10.0), //左边距 5.0,上边距10.0
// padding: EdgeInsets.symmetric(vertical: 5.0, horizontal: 20.0), //上下边距5.0,左右边距20.0
child: Text('hello padding'),
);
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 10, 0),
child: GridView.count(
crossAxisCount: 2,
// crossAxisSpacing: 10,
// mainAxisSpacing: 20,
childAspectRatio: 1.7,
children: [
for (var i = 0; i < 10; i++)
Padding(
padding: EdgeInsets.fromLTRB(10, 10, 0, 0),
child: Image.network(
'https://img.pic88.com/preview/2020/08/05/15966342611450126.jpg',
fit: BoxFit.cover,
),
),
],
),
);