How to add line dash in Flutter

前端 未结 10 1006
清歌不尽
清歌不尽 2021-02-07 02:54

How to make a line dash in Flutter like this?

10条回答
  •  逝去的感伤
    2021-02-07 03:21

    Create this class:

    class DotWidget extends StatelessWidget {
      final double totalWidth, dashWidth, emptyWidth, dashHeight;
    
      final Color dashColor;
    
      const DotWidget({
        this.totalWidth = 300,
        this.dashWidth = 10,
        this.emptyWidth = 5,
        this.dashHeight = 2,
        this.dashColor = Colors.black,
        Key key,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Row(
          mainAxisSize: MainAxisSize.min,
          children: List.generate(
            totalWidth ~/ (dashWidth + emptyWidth),
                (_) => Container(
              width: dashWidth,
              height: dashHeight,
              color: dashColor,
              margin: EdgeInsets.only(left: emptyWidth / 2, right: emptyWidth / 2),
            ),
          ),
        );
      }
    }
    

    Usage:

    Use it like any other widget

     child: DotWidget(
       dashColor: Colors.black,
       dashHeight: 2,
       dashWidth: 100,
     )
    

提交回复
热议问题