Flutter CustomPaint繪制widget使用示例
CustomPaint 介紹
Flutter CustomPaint 提供了一個(gè) canvas,可以在繪制階段在上面進(jìn)行繪制內(nèi)容。
需要繪制時(shí),CustomPaint 首先要求它的 painter 在當(dāng)前畫(huà)布上繪畫(huà),然后它繪畫(huà)它的 child,在繪畫(huà)完它的 child 之后,要求他的 foregroundPainter 繪畫(huà)。
需要在從原點(diǎn)開(kāi)始并包含給定大小的區(qū)域的矩形內(nèi)作畫(huà)。 (如果在這些邊界之外繪畫(huà),則分配的內(nèi)存可能不足以光柵化繪畫(huà)命令,并且結(jié)果行為未定義。)要保證在這些邊界內(nèi)繪畫(huà),請(qǐng)考慮使用 ClipRect。
使用 CustomPaint
const CustomPaint({ super.key, this.painter, this.foregroundPainter, this.size = Size.zero, this.isComplex = false, this.willChange = false, super.child, })
最重要就是這個(gè) 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; } }
需要重寫(xiě)兩個(gè)方法,在 paint 方法中進(jìn)行繪制。如果繪制不受外界影響,shouldRepaint 返回 false 就好。
CustomPaint(painter: MyPainter(),);
把 MyPainter 賦值給參數(shù) painter 就好了。
size 的大小。
如果 CustomPaint 的約束可以為 0 的話(huà),那么 size 的值 Size(0,0)
,就是說(shuō),size 的值總是 Constrains 的最小 width,最小 height。有兩種辦法可以改變 size。
- 可以給 CustomPaint 加上約束,比如加一個(gè) 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ù)無(wú)效,只有 在loose 約束下 ,size 參數(shù)才能起到作用。
isComplex
是否提示應(yīng)該緩存該層的繪畫(huà)。如果 為false,則合成器將自己來(lái)決定這一層是否應(yīng)該緩存。
willChange
是否應(yīng)該告知緩存層這幅畫(huà)在下一幀中可能會(huì)發(fā)生變化。如果 isComplex 為 true,才需要考慮這個(gè)參數(shù)。
foregroundPainter
默認(rèn)繪制的層是在 child 之下,foregroundPainter 在 child 之上。
動(dòng)畫(huà)
CustomPainter 有一個(gè) 可選的參數(shù) Listenable? repaint
,是用來(lái)做動(dòng)畫(huà)的。
舉個(gè)例子。
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), )); } }
會(huì)看到一條紅色的直線由短變長(zhǎng)。
以上就是Flutter CustomPaint繪制widget使用示例的詳細(xì)內(nèi)容,更多關(guān)于Flutter CustomPaint繪制widget的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Flutter交互并使用小工具管理其狀態(tài)widget的state詳解
- Flutter框架解決盒約束widget和assets里加載資產(chǎn)技術(shù)
- Flutter?Widget開(kāi)發(fā)之Focus組件圖文詳解
- Flutter Widget開(kāi)發(fā)Shortcuts快捷鍵實(shí)例
- Flutter?Widget之NavigationBar使用詳解
- Flutter?Widget之FutureBuilder使用示例詳解
- Flutter?Widget?之package?mason實(shí)現(xiàn)詳解
- Flutter Widget移動(dòng)UI框架使用Material和密匙Key實(shí)戰(zhàn)
相關(guān)文章
iOS動(dòng)畫(huà)實(shí)現(xiàn)雨花與櫻花特效
小編今天為大家?guī)?lái)一場(chǎng)淅淅瀝瀝的夜空之雨和滿(mǎn)天飛舞的櫻花之戀,希望能在炎炎夏日為您帶來(lái)一絲清爽的涼意!學(xué)習(xí)iOS動(dòng)畫(huà)的小伙伴們可以參考學(xué)習(xí)。2016-08-08IOS 避免self循環(huán)引用的方法的實(shí)例詳解
這篇文章主要介紹了IOS 避免self循環(huán)引用的方法的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09iOS中關(guān)于模塊化開(kāi)發(fā)解決方案(純干貨)
這篇文章主要介紹了iOS中關(guān)于模塊化開(kāi)發(fā)解決方案(純干貨)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09iOS UITextField最大字符數(shù)和字節(jié)數(shù)的限制詳解
在開(kāi)發(fā)中我們經(jīng)常遇到這樣的需求:在UITextField或者UITextView中限制用戶(hù)可以輸入的最大字符數(shù)。但在UITextView , UITextfield 中有很多坑,網(wǎng)上的方法也很多。但是并不是很全面吧,這里全面進(jìn)行了總結(jié),有需要的朋友們可以參考借鑒,下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2016-11-11淺談IOS中AFNetworking網(wǎng)絡(luò)請(qǐng)求的get和post步驟
本篇文章主要介紹了淺談IOS中AFNetworking網(wǎng)絡(luò)請(qǐng)求的get和post步驟的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02