How to add line dash in Flutter

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

How to make a line dash in Flutter like this?

10条回答
  •  时光说笑
    2021-02-07 03:11

    Try this,

    class DotDivider extends StatelessWidget {
      final double width;
      final double height;
      final double gap;
      final Color color;
      final double lineHeight;
    
      const DotDivider(
          {this.height = 1.0,
          this.color = Colors.black,
          this.width = 2.0,
          this.gap = 2.0,
          this.lineHeight = 10.0});
    
      @override
      Widget build(BuildContext context) {
        return LayoutBuilder(
          builder: (BuildContext context, BoxConstraints constraints) {
            final boxWidth = constraints.constrainWidth();
            final dashWidth = width;
            final dashHeight = height;
            final dashCount = (boxWidth / dashWidth).floor();
            return Container(
              height: (lineHeight * 2) + height,
              child: ListView.builder(
                physics: NeverScrollableScrollPhysics(),
                scrollDirection: Axis.horizontal,
                itemCount: dashCount,
                itemBuilder: (BuildContext context, int index) => Center(
                  child: Container(
                    width: dashWidth,
                    height: dashHeight,
                    margin:
                        EdgeInsets.symmetric(vertical: lineHeight, horizontal: gap),
                    decoration: BoxDecoration(color: color),
                  ),
                ),
              ),
            );
          },
        );
      }
    }
    

提交回复
热议问题