描述
在flutter界面布局中padding是我们经常要用到的,于是我特地封装了一个类用来处理这些padding,类似于bootstrap中的pl-1、pl-2什么什么的。
代码
import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; class ToolPadding { static EdgeInsets pr15 = EdgeInsets.only(right: 15.0.w); static EdgeInsets pall15 = EdgeInsets.all(15.0.w); static EdgeInsets pall25 = EdgeInsets.all(25.0.w); static EdgeInsets pall5 = EdgeInsets.all(5.0.w); static EdgeInsets pt15 = EdgeInsets.only(top: 15.0.h); static EdgeInsets pl15 = EdgeInsets.only(left: 15.0.w); static EdgeInsets ptb15 = EdgeInsets.only(top: 15.0.h, bottom: 15.0.h); static EdgeInsets ptw25 = EdgeInsets.only(top: 25.0.w); static EdgeInsets plr25 = EdgeInsets.only(left: 25.0.w, right: 25.0.w); static EdgeInsets ptlr25 = EdgeInsets.only(top: 25.0.h, left: 25.0.w, right: 25.0.w); static EdgeInsets pblr25 = EdgeInsets.only(bottom: 25.0.h, left: 25.0.w, right: 25.0.w); static EdgeInsets pPubla = EdgeInsets.only(left: 15.0.w, right: 7.5.w); static EdgeInsets pPublb = EdgeInsets.only(left: 7.5.w, right: 15.0.w); static EdgeInsets p0 = EdgeInsets.all(0.0); static EdgeInsets palltblist25 = EdgeInsets.only(left: 25.0.w, right: 25.0.w, top: 12.5.h, bottom: 12.5.h); }
注意:在该类中我使用了屏幕适配插件flutter_screenutil,如果你没有安装此插件,可以把.h.w这些单位去掉。
当然,这个类因为语义的原因应该是不大适合太多人使用的,这里只是提供一种思路,大家可以根据自己的代码习惯以及语义习惯来定义自己的padding类。
使用方式
在需要使用的地方引入:
import 'package:app包名/common/tool/padding.dart';
之后,比如右边内边距为15:
Container( adding: ToolPadding.pr15, ),