Flutter 虚线组件的封装

虚线的组件

如果在css里面我们直接弄一个样式就可以了

但是在flutter里面这样做很明显是不行的

flutter它这个是不支持虚线的

所以我们要自己封装一个

虚线的组件核心就是SizedBox

SizedBox就可以 显示这样的

在这里插入图片描述

这个东西就像一个一个的div块但是它是用flex布局拼起来的

  • 注意使用这个东西的时候必须使用Container来包裹起来
  • 如果是垂直排布就要设置高度 如果是 水平排布就要设置宽度
  • 同时因该设置每个块的排布方式 Axis.vertical 时候垂直排布 默认的情况就是水平排布

属性

  • axis SizedBox是垂直排布还是水平排布
  • dashedWidth 每个块的宽度(水平排布时候的长度)
  • dashedHeigth(垂直排布时候的长度)
  • count:密度 个数
  • color: 虚线颜色
// 封装虚线组件 虚线这个东西需要一个Container
class HYDashedLine extends StatelessWidget {
//    Flex Column/Row方向 Axis
  final Axis axis;
  final double dashedWidth;
  final double dashedHeight;
  final int count;
  final Color color;

  HYDashedLine({
    this.axis = Axis.horizontal,  // 方向
    this.dashedHeight = 1,            // 虚线厚度
    this.dashedWidth = 1,             // 虚线长度
    this.count = 10,
    this.color = Colors.red
  });

  @override
  Widget build(BuildContext context) {
    return Flex(
      direction: axis,
//      这样才能铺开
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
//      用不上用_替代
      children: List.generate(count, (_) {
        return SizedBox(
            width: dashedWidth,
            height: dashedHeight,
//          这个东西是没有颜色的
//        为什么Dcoration
            child: DecoratedBox(
              decoration: BoxDecoration(color: color),
            )
        );
      }),
    );
  }
}

使用

class _HYHomeContentState extends State<HYHomeContent> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            width: 200,
            child: HYDashedLine(dashedWidth: 8,),
          ),
          Container(
            height: 200,
            child: HYDashedLine(axis: Axis.vertical, dashedHeight: 8,)
          )
        ],
      )
    );
  }
}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值