最近Flutter項目新需求中須要實現圓弧樣式,以下圖所示:html
畫布組件CustomPaint,繪製內容經過painter和foregroundPainter。painter繪製在child以前,foregroundPainter繪製在child以後,所以child內容覆蓋在painter上層,foregroundPainter內容覆蓋在child上層。前端
CustomPaint(
painter: CanvasPainter(),
foregroundPainter: ForegroundPainter(),
child: Container(
height: 50,
decoration: BoxDecoration(
border: Border.all(color: Colors.black, width: 5),
),
child: Text("我是CustomPaint的child"),
),
),
複製代碼
繪製部分的實現由CustomPainter完成。首先建立一個繼承CustomPainter的類對象。git
class DemoPainter extends CustomPainter{
@override
void paint(Canvas canvas, Size size) {
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return null;
}
}
複製代碼
1度 = pi / 180,因此起始度數 = 度數 * pi / 180。github
drawArc方法0度位置是在圓點水平位置左側正方向是順時針,因此圓弧繪製第一個起始度數參數爲-240度(-240 * (pi / 180)),已知0度位置並知道360度位置。-240度位置距離圓點垂直位置下方度數爲30,缺口總共度數爲60。所以第二個度數參數爲 300 * (pi / 180)。canvas
繪製圓弧使用drawArc方法,設置繪製圓形尺寸(圓心,半徑)、起始角度、圓弧角度、是否閉合。bash
@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint();
paint.style = PaintingStyle.stroke;
paint.strokeWidth = 5;
paint.strokeCap = StrokeCap.round;
paint.color = Colors.white;
canvas.drawArc(
Rect.fromCircle(
center: Offset(size.width / 2, size.height / 2), radius: 70),
-240 * (pi / 180),
300 * (pi / 180),
false,
paint);
paint.strokeWidth = 2;
canvas.drawArc(
Rect.fromCircle(
center: Offset(size.width / 2, size.height / 2), radius: 65),
-240 * (pi / 180),
300 * (pi / 180),
false,
paint);
paint.strokeWidth = 1;
canvas.drawArc(
Rect.fromCircle(
center: Offset(size.width / 2, size.height / 2), radius: 60),
-240 * (pi / 180),
300 * (pi / 180),
false,
paint);
}
複製代碼
另外CustomPainter還有child,能夠經過Stack將文本內容經過Text居中顯示,固然UI中間文本和按鈕固然也能夠用畫布繪製的方式實現,完整畫布代碼以下:ide
DemoPainter(
painter: CanvasPainter(),
foregroundPainter: ForegroundPainter(),
child: Container(
height: 50,
decoration: BoxDecoration(
border: Border.all(color: Colors.black, width: 5),
),
child: Stack(
children: <Widget>[
Text("我是CustomPaint的child"),
Center(
child: Text(
"96",
style: TextStyle(
color: Colors.white,
fontSize: 30,
fontWeight: FontWeight.bold,
),
),
)
],
),
),
),
複製代碼
最後的最後介紹兩個優秀的Flutter圖表開源庫Syncfusion Flutter Charts、Flutter Charting 。你會驚喜的發現經過畫布實現圖表功能原來能夠這麼酷炫。ui