Container 封装

Flutter 开发中Container组件用到太多了,我也是结合自己的项目简单封装了一些够我使用的。不用再用InkWell等组件来添加事件方法了,目前这个只是非常简单的封装等后期我再升级一下。

大家先看看吧

代码如下:

/// 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: () {
          /// 点击事件处理
      },
      decoration: circleDecoration(20, 1, Colors.amberAccent),
      child: Row(
        children: [
          Text(_sourceTitle(context) ?? ""),
          Icon(Icons.keyboard_arrow_down, color: Colors.red),
          SizedBox(width: 5),
        ],
      ),
    );

也可以看我的其它文章也使用到了:传送门

你可能感兴趣的:(Container 封装)