当前位置:网站首页>Get to know the drawing component of flutter - custompaint
Get to know the drawing component of flutter - custompaint
2022-07-01 00:44:00 【Island manong @ official account with the same name】
brief introduction
In some scenes , We will need to draw some highly customized components , such as UI The designer gave us a problem —— Make a strange frame . witness UI The designer is for the sake of a beautiful little sister , I'm sorry to say this can't be done ( That's also very shameful ). At this time, we can't directly use Flutter The self-contained components , Instead, you need to draw the components manually , Then you need to use CuntomPaint Components .CustomPaint Components and front end Canvas almost , Allows us to draw various elements on a canvas , Including some 、 Line 、 rectangular 、 arc 、 written words 、 Pictures, etc. .
CustomPaint Introduce
CustomPaint It's a Widget, There are three important parameters :
CustomPaint(
child: childWidget(),
foregroundPainter: foregroundPainter(),
painter: backgroundPainter(),
)
child:CustomPaintThe child components ;painterandforegroundPainter: All areCustomPainterclass , Used for definitioncanvasThe content of the drawing . The difference lies in , The first is executionpainterThe drawing instructions of . Then render on the backgroundchildChild components . Last ,foregroundPainterThe content of will be drawn inchildupper story .

CustomPainter Provides a paint The drawing method allows us to draw graphics , The method carries canvas and size Two parameters , among canvas It's the canvas ,size It's the size of the canvas .canvas Provides many ways to draw graphics , For example, draw a path 、 rectangular 、 Circles and lines, etc .
CustomPainter Example
With the above basic concepts , Let's take a simple example to demonstrate the hierarchical relationship of three parameters . We are backgroundPainter Draw a blue square on , Then pass in a prototype image component as child, Finally, in foregroundPainter Draw a translucent red circle on the top to cover part of the picture . The final effect is as follows , In this way, we can well understand the relationship between the three elements .
The complete code is as follows :
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;
}
}
In the background rendering, we formulate canvas Background color of , This is the following line of code :
canvas.drawColor(Color(0xFFF1F1F1), BlendMode.color);
Other code is relatively easy to understand ,BackgroundPainter Drew a blue square ,child It's a centered circular picture , and ForegroundPainter In the middle, a picture of the same size as the circular picture is drawn , Translucent red circle . So we get the effect we want .
summary
This article introduces CustomPaint Basic use of , You can see CustomPaint It's not complicated to use , But what's really complicated is how to draw graphics , This needs to be supported by good mathematical knowledge ( Of course , If you really don't, you can also search for relevant information ). In the next article, let's draw some common graphics . 
边栏推荐
- 2022-2028 global ultra high purity electrolytic iron powder industry research and trend analysis report
- 初识 Flutter 的绘图组件 — CustomPaint
- The principle of journal node
- leetcode 474. Ones and zeroes (medium)
- CentOS installation starts redis
- 2022-2028 global single travel industry research and trend analysis report
- The principle and related problems of acid in MySQL
- C#生成putty格式的ppk文件(支持passphrase)
- Oracle临时表详解
- 2022-2028 global encrypted external hard disk industry research and trend analysis report
猜你喜欢

2022-2028 global public address fire alarm system industry research and trend analysis report

HDU 2488 A Knight's Journey(DFS)

The full technology stack, full scene and full role cloud native series training was launched to help enterprises build a hard core cloud native technology team

2022-2028 global rampant travel industry research and trend analysis report

2022-2028 global carbon fiber room scraper system industry research and trend analysis report

Host FL Studio fruit music production daw20.9

Redis - how to understand publishing and subscribing
![[UML] UML class diagram](/img/6f/30bd15967103969e600d69e618d8bf.png)
[UML] UML class diagram

Deployment of mini version message queue based on redis6.0

给按钮的边框和文字设置不同的背景色
随机推荐
Plot size and resolution with R markdown, knitr, pandoc, beamer
问题解决:如何管理线程私有(thread_local)的指针变量
Gateway service gateway
Pytorch installs and uses GPU acceleration
ABAQUS 2022 latest edition - perfect realistic simulation solution
CSDN常用复杂公式模板记录
ArrayList分析1-循环、扩容、版本
2022-2028 global public address fire alarm system industry research and trend analysis report
MySQL variables, stored procedures and functions
BeanUtils. Copyproperties() vs. mapstruct
什么是产品思维
Ranger plug-in development (Part 2)
剑指 Offer 19. 正则表达式匹配
Line number of Jenkins pipeline script execution exception
A detailed explanation of the implementation principle of go Distributed Link Tracking
ArrayList分析1-循环、扩容、版本
Authentication principle of Ranger plug-in
Teach you how to use Hal library to get started -- become a lighting master
[designmode] factory pattern
Oracle-表的创建与管理