当前位置:网站首页>初识 Flutter 的绘图组件 — CustomPaint
初识 Flutter 的绘图组件 — CustomPaint
2022-07-01 00:33:00 【岛上码农@公众号同名】
简介
在有些场景中,我们会需要绘制一些高度定制化的组件,比如 UI 设计师给我们出了个难题 —— 弄一个奇形怪状的边框。看在 UI 设计师是一个漂亮小姐姐的份上,又不好意思说这个做不了(那样也很没面子)。这个时候我们就不能直接使用 Flutter 自带的那些组件了,而是需要手动绘制组件,那就会需要用到 CuntomPaint 组件。CustomPaint 组件和前端的 Canvas差不多,允许我们在一个画布上绘制各种元素,包括点、线、矩形、圆弧、文字、图片等等。
CustomPaint 介绍
CustomPaint是一个 Widget,其中有三个重要的参数:
CustomPaint(
child: childWidget(),
foregroundPainter: foregroundPainter(),
painter: backgroundPainter(),
)
child:CustomPaint的子组件;painter和foregroundPainter:都是CustomPainter类,用于定义canvas绘制的内容。区别在于,首先是执行painter的绘制指令。然后是在背景上渲染child子组件。最后,foregroundPainter的内容会绘制在child上一层。

CustomPainter提供了一个paint绘图方法供我们绘制图形,该方法携带canvas 和size两个参数,其中 canvas 是画布,size 是画布大小。canvas 提供了很多绘制图形的方法,比如绘制路径、矩形、圆形和线条等等。
CustomPainter 示例
有了上面的基础概念后,我们来一个简单的示例来演示三个参数的层级关系。我们在backgroundPainter 上绘制一个蓝色的正方形,然后传入一个原型图片组件作为 child,最后是在 foregroundPainter 上绘制一个半透明的红色圆形遮住一部分图片。最终实现效果如下图,这样我们可以很好地了解三个元素的关系。
完整代码如下:
import 'dart:ui';
import 'package:flutter/material.dart';
class BasicPaintPage extends StatelessWidget {
const BasicPaintPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return CustomPaint(
child: Center(
child: ClipOval(
child: Image.asset(
'images/beauty.jpeg',
width: 200,
height: 200,
fit: BoxFit.fitWidth,
),
),
),
painter: BackgroundPainter(),
foregroundPainter: ForegroundPainter(),
);
}
}
class BackgroundPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
canvas.drawColor(Color(0xFFF1F1F1), BlendMode.color);
var center = size / 2;
var paint = Paint()..color = Color(0xFF2080E5);
paint.strokeWidth = 2.0;
canvas.drawRect(
Rect.fromLTWH(center.width - 120, center.height - 120, 240, 240),
paint,
);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return false;
}
}
class ForegroundPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var center = size / 2;
var paint = Paint()..color = Color(0x80F53010);
paint.strokeWidth = 2.0;
canvas.drawCircle(
Offset(center.width, center.height),
100,
paint,
);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return false;
}
}
其中在背景绘制中我们制定了canvas 的背景色,即下面这行代码:
canvas.drawColor(Color(0xFFF1F1F1), BlendMode.color);
其他代码就相对好理解了,BackgroundPainter 绘制了一个蓝色正方形,child 是一个居中的圆形图片,而 ForegroundPainter 则是在中间绘制了一个和圆形图片同等大小的,半透明的红色圆圈。于是就得到了我们想要的效果。
总结
本篇介绍了 CustomPaint 的基本使用,可以看到 CustomPaint使用起来并不复杂,但是真正复杂的是如何绘制图形,这个需要有比较好的数学知识支撑(当然,如果确实不会也可以搜索相关资料)。接下来的一篇我们来绘制一些常见的图形。 
边栏推荐
- Is it safe to open a stock account of Huatai Securities online?
- Pytorch auto derivation
- 剑指 Offer 19. 正则表达式匹配
- Combining online and offline, VR panorama is a good way to transform furniture online!
- Explain kubernetes backup and recovery tools velero | learn more about carina series phase III
- Longest valid bracket
- File reading and writing for rust file system processing - rust Practice Guide
- MySQL variables, stored procedures and functions
- MySQL storage engine
- ABAQUS 2022 software installation package and installation tutorial
猜你喜欢

Fh6908a negative pole turn off synchronous rectification analog low voltage drop diode control IC chip tsot23-6 ultra low power rectifier 1W power consumption < 100ua static replacement mp6908

优质的水泵 SolidWorks模型素材推荐,不容错过

IFLYTEK active competition summary! (12)

Why should VR panoramic shooting join us? Leverage resources to achieve win-win results

VR panorama adds contrast function to make the display of differentiation effect more intuitive!

2022-2028 global PTFE lined valve industry research and trend analysis report

Bridge emqx cloud data to AWS IOT through the public network

2022-2028 global herbal diet tea industry research and trend analysis report

Redis - sentinel mode
![[UML] UML class diagram](/img/6f/30bd15967103969e600d69e618d8bf.png)
[UML] UML class diagram
随机推荐
NE555波形发生器手把手教程之NE555内部结构(一)
left join左连接匹配数据为NULL时显示指定值
The difference between union and union all in MySQL
Vmware16 installing win11 virtual machine (the most complete step + stepping on the pit)
2022-2028 global capsule shell industry research and trend analysis report
Docsify building a personal minimalist knowledge warehouse
How do it outsourcing resident personnel position their pain points?
Stack frame
【日常记录】——对BigDecimal除法运算时遇到的Bug
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
Solving the weird problem that the query conditions affect the value of query fields in MySQL query
20220215 CTF misc buuctf the world in the mirror the use of stegsolve tool data extract
Techo youth 2022 academic year college open class: behind the live broadcast of Lianmai, explore how to apply audio and video technology
[untitled]
P4学习——p4runtime
Search rotation sort array
Manage edge browser settings (ie mode, homepage binding, etc.) through group policy in the enterprise
leetcode 474. Ones and Zeroes 一和零(中等)
Redis - how to understand publishing and subscribing
PyTorch安装并使用gpu加速