html的弯曲的虚线设置,Flutter绘制弯曲虚线

效果

1460000019014513

开始

修改main.dart文件:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

@override

Widget build(BuildContext context) {

return MaterialApp(

title: "Drawing Shapes",

home: DrawingPage(),

);

}

}

class DrawingPage extends StatelessWidget {

@override

Widget build(BuildContext context) {

return Scaffold(

appBar: AppBar(

title: Text("Curved Line"),

),

body: CustomPaint(

painter: CurvePainter(),

child: Container(),

),

);

}

}

class CurvePainter extends CustomPainter {

@override

void paint(Canvas canvas, Size size) {

}

@override

bool shouldRepaint(CustomPainter oldDelegate) {

return false;

}

}

接下来我们在CurvePainter中实现绘制:

这里使用贝塞尔曲线绘制,需要4个点,分别是:起点,终点和两个控制点,像图中那样,移动控制点就能改变曲线形状。你可以在在线工具中尝试:

1460000019014514

我们使用 cubicTo方法设置路径path:

void cubicTo(double x1, double y1, double x2, double y2, double x3, double y3)

函数绘制路径从当前点到(x3,y3),控制点是(x1,y1),(x2,y2)

正如你看到的,cubicTo 方法接受3个点作为参数,前两个代表控制点,最后一个是结束点。开始点就是你的pen当前坐在canvas中的点。

canvas的坐标系是左上角是(0,0)。右下角是(size.width, size.height)。所以可以尝试调整4个点:

void paint(Canvas canvas, Size size) {

var paint = Paint();

paint.color = Colors.lightBlue;

paint.style = PaintingStyle.stroke;

paint.strokeWidth = 3;

var startPoint = Offset(0, size.height / 2);

var controlPoint1 = Offset(size.width / 4, size.height / 3);

var controlPoint2 = Offset(3 * size.width / 4, size.height / 3);

var endPoint = Offset(size.width, size.height / 2);

var path = Path();

path.moveTo(startPoint.dx, startPoint.dy);

path.cubicTo(controlPoint1.dx, controlPoint1.dy,

controlPoint2.dx, controlPoint2.dy,

endPoint.dx, endPoint.dy);

canvas.drawPath(path, paint);

}

paint对象就是相当于我们的笔,被设置了蓝色和宽度为3.

我们使用path表述贝塞尔曲线。moveTo方法移动笔到开始点的位置。最后使用drawPath方法绘制出来。

1460000019014515

虚线

要使用它现在 pubspec.yml文件添加这个库:

dependencies:

flutter:

sdk: flutter

# The following adds the Cupertino Icons font to your application.

# Use with the CupertinoIcons class for iOS style icons.

cupertino_icons: ^0.1.2

path_drawing: ^0.4.0

运行“flutter packages get”获得库,然后导入头文件就可以使用了:

import 'package:path_drawing/path_drawing.dart';

把上面的路径用dashPath包裹一下就可以了:

canvas.drawPath(

dashPath(

path,

dashArray: CircularIntervalList([15.0, 10.5]),

),

paint,

);

dashPath方法接受两个参数,第一个就是要绘制的path,第二个参数定义每一段虚线的长度和两个虚线间的间隔长度。结果如下:

1460000019014516

Flutter绘制虚线可以使用 `CustomPaint` 和 `Path` 绘制。 下面是一个绘制虚线的示例代码: ```dart import 'dart:ui'; import 'package:flutter/material.dart'; class DashedLine extends StatelessWidget { final double dashedWidth; final double dashedHeight; final Color color; const DashedLine({ Key key, @required this.dashedWidth, @required this.dashedHeight, this.color = Colors.grey, }) : super(key: key); @override Widget build(BuildContext context) { return CustomPaint( painter: _DashedLinePainter( dashedWidth: dashedWidth, dashedHeight: dashedHeight, color: color, ), ); } } class _DashedLinePainter extends CustomPainter { final double dashedWidth; final double dashedHeight; final Color color; _DashedLinePainter({ @required this.dashedWidth, @required this.dashedHeight, @required this.color, }); @override void paint(Canvas canvas, Size size) { final paint = Paint() ..color = color ..strokeWidth = dashedHeight ..style = PaintingStyle.stroke ..strokeCap = StrokeCap.round; final path = Path(); double startX = 0.0; double endX = dashedWidth; while (endX < size.width) { path.moveTo(startX, dashedHeight / 2); path.lineTo(endX, dashedHeight / 2); startX += dashedWidth * 2; endX += dashedWidth * 2; } canvas.drawPath(path, paint); } @override bool shouldRepaint(_DashedLinePainter oldDelegate) { return oldDelegate.dashedWidth != dashedWidth || oldDelegate.dashedHeight != dashedHeight || oldDelegate.color != color; } } ``` 使用方式: ```dart DashedLine( dashedWidth: 5, dashedHeight: 1, color: Colors.grey, ) ``` 其中,`dashedWidth` 表示虚线的宽度,`dashedHeight` 表示虚线的高度,`color` 表示虚线的颜色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值