在Flutter 開發(fā)中Container
組件用到太多了俊戳,我也是結(jié)合自己的項(xiàng)目簡單封裝了一些夠我使用的惨奕。不用再用InkWell
等組件來添加事件方法了雪位,目前這個(gè)只是非常簡單的封裝等后期我再升級一下。
大家先看看吧
代碼如下:
/// Container 封裝
class WLContainer extends StatelessWidget {
final Color color;
final Widget child;
final double width;
final double height;
final Function onClick;
final Clip clipBehavior;
final Decoration decoration;
final EdgeInsetsGeometry margin;
final EdgeInsetsGeometry padding;
final BoxConstraints constraints;
final AlignmentGeometry alignment;
final Decoration foregroundDecoration;
const WLContainer({
Key key,
this.child,
this.color,
this.width,
this.height,
this.margin,
this.onClick,
this.padding,
this.alignment,
this.decoration,
this.constraints,
this.foregroundDecoration,
this.clipBehavior = Clip.none,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return InkWell(
child: Container(
child: child,
width: width,
color: color,
height: height,
margin: margin,
padding: padding,
alignment: alignment,
decoration: decoration,
constraints: constraints,
clipBehavior: clipBehavior,
foregroundDecoration: foregroundDecoration,
),
onTap: onClick,
);
}
}
使用如下:
WLContainer(
height: 30,
width: 120,
padding: insetLR(20, 10),
alignment: Alignment.centerRight,
onClick: () {
/// 點(diǎn)擊事件處理
},
decoration: circleDecoration(20, 1, Colors.amberAccent),
child: Row(
children: <Widget>[
Text(_sourceTitle(context) ?? ""),
Icon(Icons.keyboard_arrow_down, color: Colors.red),
SizedBox(width: 5),
],
),
);
也可以看我的其它文章也使用到了:傳送門