網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一.組合widget實(shí)現(xiàn)
1.android和flutter自定義控件對(duì)比
Android中,一般會(huì)繼承View或已經(jīng)存在的某個(gè)控件,然后覆蓋draw方法來(lái)實(shí)現(xiàn)自定義View。在Flutter中,一個(gè)自定義widget通常是通過(guò)組合其它widget來(lái)實(shí)現(xiàn)的,而不是繼承。下面看看如何構(gòu)建持有一個(gè)label的CustomButton。這是通過(guò)將Text與RaisedButton組合來(lái)實(shí)現(xiàn)的,而不是繼承RaisedButton并重寫(xiě)其繪制方法實(shí)現(xiàn),eg :custombuttontest.dart
import 'package:flutter/material.dart'; class CustomButtonTest extends StatelessWidget{ final String textStr; CustomButtonTest(this.textStr); @override Widget build(BuildContext context) { return RaisedButton( onPressed: (){}, child: Text( textStr, textAlign: TextAlign.center, ) ); } }
上面定義好組件之后,可直接在調(diào)用build的方法中現(xiàn)實(shí),eg :
@override Widget build(BuildContext context) { return new Center( child: new CustomButton("Custom Button"), ); } }
二.通過(guò)自定義CustomPainter實(shí)現(xiàn)widgets
1.CustomPainter主要屬性介紹,和Android開(kāi)發(fā)中的自定義View類(lèi)似,F(xiàn)lutter中的繪制也是依靠Canvas和Paint來(lái)實(shí)現(xiàn)的
1).Canvas //畫(huà)布,為開(kāi)發(fā)者提供了點(diǎn)、線、矩形、圓形、嵌套矩形等繪制方法。
2).Paint //畫(huà)筆,可以設(shè)置抗鋸齒,畫(huà)筆顏色,粗細(xì),填充模式等屬性,繪制時(shí)可以定義多個(gè)畫(huà)筆以滿足不同的繪制需求。eg:
Paint _paint = new Paint() ..color = Colors.red // 畫(huà)筆顏色 ..strokeCap = StrokeCap.round //畫(huà)筆筆觸類(lèi)型,包括(1.round-畫(huà)筆筆觸呈半圓形輪廓開(kāi)始和結(jié)束;2.butt-筆觸開(kāi)始和結(jié)束邊緣平坦,沒(méi)有外延;3.square-筆觸開(kāi)始和結(jié)束邊緣平坦,向外延伸長(zhǎng)度為畫(huà)筆寬度的一半) ..isAntiAlias = true //是否啟動(dòng)抗鋸齒 ..style=PaintingStyle.fill //繪畫(huà)風(fēng)格,默認(rèn)為填充,有fill和stroke兩種 ..blendMode=BlendMode.exclusion //顏色混合模式 ..colorFilter=ColorFilter.mode(Colors.blueAccent, BlendMode.exclusion)//顏色渲染模式 ..maskFilter=MaskFilter.blur(BlurStyle.inner, 3.0)//模糊遮罩效果 ..filterQuality=FilterQuality.high//顏色渲染模式的質(zhì)量 ..strokeWidth = 15.0;//畫(huà)筆的寬度復(fù)制代碼
3).Offset //坐標(biāo),可以用來(lái)表示某個(gè)點(diǎn)在畫(huà)布中的坐標(biāo)位置。
4).Rect //矩形,在圖形的繪制中,一般都是分區(qū)域繪制的,這個(gè)區(qū)域一般都是一個(gè)矩形,在繪制中通常使用Rect來(lái)存儲(chǔ)繪制的位置信息。
5).坐標(biāo)系 //在Flutter中,坐標(biāo)系原點(diǎn)(0,0)位于左上角,X軸向右變大,Y軸向下變大。
2.painting.dart中的主要方法,eg:
void drawRect(Rect rect, Paint paint) {...} //畫(huà)矩形 void drawLine(Offset p1, Offset p2, Paint paint) {...} //畫(huà)線 void drawPoints(PointMode pointMode, List<Offset> points, Paint paint) {...} //畫(huà)點(diǎn) void drawCircle(Offset c, double radius, Paint paint) {...} //畫(huà)圓 void drawArc(Rect rect, double startAngle, double sweepAngle, bool useCenter, Paint paint) {...} //畫(huà)圓弧
三.餅狀圖piechart.dart代碼展示
import 'dart:math'; import 'package:flutter/material.dart'; class PieChartTest extends StatelessWidget{ const PieChartTest({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('pie chart'), ), body:Container( alignment: Alignment.center, child: CustomPaint( size: const Size(300, 300), painter: PieChartPainter(), ), ) ); } } class PieChartPainter extends CustomPainter{ Paint getColoredPaint(Color color){ Paint paint=Paint(); paint.color=color; return paint; } @override void paint(Canvas canvas, Size size) { double wheelSize=min(size.width, size.height)/2; double nbElem=8; double radius=(2*pi)/nbElem; Rect boundingRect=Rect.fromCircle(center: Offset(wheelSize,wheelSize), radius: wheelSize); canvas.drawArc(boundingRect, 0, radius, true, getColoredPaint(Colors.orange)); canvas.drawArc(boundingRect, radius, radius, true, getColoredPaint(Colors.black)); canvas.drawArc(boundingRect, radius*2, radius, true, getColoredPaint(Colors.green)); canvas.drawArc(boundingRect, radius*3, radius, true, getColoredPaint(Colors.red)); canvas.drawArc(boundingRect, radius*4, radius, true, getColoredPaint(Colors.blue)); canvas.drawArc(boundingRect, radius*5, radius, true, getColoredPaint(Colors.yellow)); canvas.drawArc(boundingRect, radius*6, radius, true, getColoredPaint(Colors.purple)); canvas.drawArc(boundingRect, radius*7, radius, true, getColoredPaint(Colors.white)); } @override bool shouldRepaint(covariant CustomPainter oldDelegate)=>oldDelegate!=this; }
四.實(shí)際效果圖,eg:
附:Flutter中父widget調(diào)用子widget的方法
一、定義globalKey,注意<>中的是State類(lèi)。
final _childWidgetKey = GlobalKey();
二、在父頁(yè)面初始化子widget
ChildPage(key:_receiveKey),
三、
class ChildPage extends StatefulWidget {undefined ChildPage({Key key}) : super(key: key); ???????@override ChildPageState createState() => ChildPageState(); }
四、在父界面調(diào)用子widget中的方法
_childWidgetKey.currentState.onRefresh();
總結(jié)
原文鏈接:https://blog.csdn.net/j086924/article/details/122406209
相關(guān)推薦
- 2022-04-05 關(guān)于Unity中RectTransform與transform的區(qū)別_C#教程
- 2022-05-10 remote: error: GE007: Your push would publish a pr
- 2022-12-15 Redis分布式鎖如何設(shè)置超時(shí)時(shí)間_Redis
- 2023-09-12 Spring AOP 登錄日志
- 2022-05-12 android ViewModel+LiveData簡(jiǎn)單使用
- 2023-03-21 C++將字符串格式化的幾種方式總結(jié)_C 語(yǔ)言
- 2022-07-07 C++如何將二叉搜索樹(shù)轉(zhuǎn)換成雙向循環(huán)鏈表(雙指針或數(shù)組)_C 語(yǔ)言
- 2022-10-22 Python常用工具類(lèi)之a(chǎn)dbtool示例代碼_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支