当前位置:网站首页>初识 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使用起来并不复杂,但是真正复杂的是如何绘制图形,这个需要有比较好的数学知识支撑(当然,如果确实不会也可以搜索相关资料)。接下来的一篇我们来绘制一些常见的图形。 
边栏推荐
- 2022-2028 global herbal diet tea industry research and trend analysis report
- ArrayList分析1-循环、扩容、版本
- Basic data structure of redis
- 2022-2028 global single travel industry research and trend analysis report
- Rust book materials - yazhijia Library
- 在指南针上买基金安全吗?
- JS bubble sort and select sort
- 20220215 CTF misc buuctf the world in the mirror the use of stegsolve tool data extract
- 2022-06-30: what does the following golang code output? A:0; B:2; C: Running error. package main import “fmt“ func main()
- Kubernetes ---- pod configuration container start command
猜你喜欢

CentOS installation starts redis

Redis - how to understand publishing and subscribing

P4学习——p4runtime

Redis - cache penetration, cache breakdown, cache avalanche

Unit test concept and purpose
![[untitled]](/img/96/7f26614bbdcce71006e38ee34ab216.jpg)
[untitled]

Docsify building a personal minimalist knowledge warehouse

Host FL Studio fruit music production daw20.9

Random ball size, random motion collision

2022-2028 global PTFE lined valve industry research and trend analysis report
随机推荐
2022-2028 global ethylene oxide scrubber industry research and trend analysis report
Plot size and resolution with R markdown, knitr, pandoc, beamer
20220215 misc buctf easycap Wireshark tracks TCP flow hidden key (use of WinHex tool)
What does it mean to open an account online? Is it safe to open an account online?
2022就要过去一半了,挣钱好难
Kubernetes ---- pod configuration container start command
1009 product of polynomials (25 points) [PTA class A]
Why should VR panoramic shooting join us? Leverage resources to achieve win-win results
2022-2028 global single travel industry research and trend analysis report
How does the VR cloud exhibition hall bring vitality to offline entities? What are the functions?
How to close an open DNS resolver
The programmer's girlfriend gave me a fatigue driving test
[untitled]
2022-2028 global electric yacht industry research and trend analysis report
剑指 Offer 18. 删除链表的节点
Confirm() method of window
Tide - rust web framework based on async STD
Design e-commerce seckill system
20220215-ctf-misc-buuctf-ningen--binwalk analysis --dd command separation --archpr brute force cracking
NATs cluster deployment