https://api.flutter.dev/flutter/widgets/Padding-class.html

Padding可以给其子节点添加填充(留白),和内边距效果类似。

定义:

  1. Padding({
  2. ...
  3. EdgeInsetsGeometry padding,
  4. Widget child,
  5. })

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 }):用于设置对称方向的填充,verticaltopbottomhorizontalleftright

示例

下面的示例主要展示了EdgeInsets的不同用法,比较简单,源码如下:

  1. Padding(
  2. padding: EdgeInsets.all(20.0), //全边距 20.0
  3. // padding: EdgeInsets.fromLTRB(5.0, 10.0, 15.0, 20.0), //左、上、右、下 边距
  4. // padding: EdgeInsets.only(left: 5.0, top: 10.0), //左边距 5.0,上边距10.0
  5. // padding: EdgeInsets.symmetric(vertical: 5.0, horizontal: 20.0), //上下边距5.0,左右边距20.0
  6. child: Text('hello padding'),
  7. );
  1. Padding(
  2. padding: EdgeInsets.fromLTRB(0, 0, 10, 0),
  3. child: GridView.count(
  4. crossAxisCount: 2,
  5. // crossAxisSpacing: 10,
  6. // mainAxisSpacing: 20,
  7. childAspectRatio: 1.7,
  8. children: [
  9. for (var i = 0; i < 10; i++)
  10. Padding(
  11. padding: EdgeInsets.fromLTRB(10, 10, 0, 0),
  12. child: Image.network(
  13. 'https://img.pic88.com/preview/2020/08/05/15966342611450126.jpg',
  14. fit: BoxFit.cover,
  15. ),
  16. ),
  17. ],
  18. ),
  19. );

image.png