Flutter CustomPaint繪制widget使用示例
CustomPaint 介紹
Flutter CustomPaint 提供了一個 canvas,可以在繪制階段在上面進行繪制內(nèi)容。
需要繪制時,CustomPaint 首先要求它的 painter 在當(dāng)前畫布上繪畫,然后它繪畫它的 child,在繪畫完它的 child 之后,要求他的 foregroundPainter 繪畫。
需要在從原點開始并包含給定大小的區(qū)域的矩形內(nèi)作畫。 (如果在這些邊界之外繪畫,則分配的內(nèi)存可能不足以光柵化繪畫命令,并且結(jié)果行為未定義。)要保證在這些邊界內(nèi)繪畫,請考慮使用 ClipRect。
使用 CustomPaint
const CustomPaint({
super.key,
this.painter,
this.foregroundPainter,
this.size = Size.zero,
this.isComplex = false,
this.willChange = false,
super.child,
})
最重要就是這個 painter,painter 需要自定義。
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
..color = Colors.red
..style = PaintingStyle.stroke
..strokeWidth = 1.0;
canvas.drawLine(
Offset(0, 10),
Offset(
100,
10,
),
paint);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return false;
}
}
需要重寫兩個方法,在 paint 方法中進行繪制。如果繪制不受外界影響,shouldRepaint 返回 false 就好。
CustomPaint(painter: MyPainter(),);
把 MyPainter 賦值給參數(shù) painter 就好了。
size 的大小。
如果 CustomPaint 的約束可以為 0 的話,那么 size 的值 Size(0,0),就是說,size 的值總是 Constrains 的最小 width,最小 height。有兩種辦法可以改變 size。
- 可以給 CustomPaint 加上約束,比如加一個 SizedBox
SizedBox(
height: 20,
width: 20,
child: CustomPaint(
painter: MyPainter(),
))
- 可以直接指定 size 參數(shù)。
SizedBox(
height: 20,
width: 20,
child: CustomPaint(
size:Size(30,30),
painter: MyPainter(),
))
size 參數(shù)可以在 constrains 的范圍內(nèi)起到作用。在本例中,約束為 20, size 為 30,最后傳給 paint 方法的 size 為 20。 tight 約束下 size 參數(shù)無效,只有 在loose 約束下 ,size 參數(shù)才能起到作用。
isComplex
是否提示應(yīng)該緩存該層的繪畫。如果 為false,則合成器將自己來決定這一層是否應(yīng)該緩存。
willChange
是否應(yīng)該告知緩存層這幅畫在下一幀中可能會發(fā)生變化。如果 isComplex 為 true,才需要考慮這個參數(shù)。
foregroundPainter
默認(rèn)繪制的層是在 child 之下,foregroundPainter 在 child 之上。
動畫
CustomPainter 有一個 可選的參數(shù) Listenable? repaint ,是用來做動畫的。
舉個例子。
class MyPainter extends CustomPainter {
MyPainter(Animation<double> animation) :_animation=animation, super(repaint: animation);
final Animation<double> _animation;
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
..color = Colors.red
..style = PaintingStyle.stroke
..strokeWidth = 1.0;
canvas.drawLine(
Offset(0, 10),
Offset(
100*_animation.value,
10,
),
paint);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return false;
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({super.key});
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> with SingleTickerProviderStateMixin{
late AnimationController controller=
AnimationController(duration: Duration(seconds: 2),vsync: this)..repeat();
@override
Widget build(BuildContext context) {
return SizedBox(
height: 20,
width: 20,
child: CustomPaint(
painter: MyPainter(controller.view),
));
}
}
會看到一條紅色的直線由短變長。
以上就是Flutter CustomPaint繪制widget使用示例的詳細(xì)內(nèi)容,更多關(guān)于Flutter CustomPaint繪制widget的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
iOS中關(guān)于模塊化開發(fā)解決方案(純干貨)
這篇文章主要介紹了iOS中關(guān)于模塊化開發(fā)解決方案(純干貨)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
iOS UITextField最大字符數(shù)和字節(jié)數(shù)的限制詳解
在開發(fā)中我們經(jīng)常遇到這樣的需求:在UITextField或者UITextView中限制用戶可以輸入的最大字符數(shù)。但在UITextView , UITextfield 中有很多坑,網(wǎng)上的方法也很多。但是并不是很全面吧,這里全面進行了總結(jié),有需要的朋友們可以參考借鑒,下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2016-11-11
淺談IOS中AFNetworking網(wǎng)絡(luò)請求的get和post步驟
本篇文章主要介紹了淺談IOS中AFNetworking網(wǎng)絡(luò)請求的get和post步驟的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02

