当前位置:网站首页>First experience of using fluent canvas
First experience of using fluent canvas
2022-04-23 19:14:00 【cjzcjl】
Preface :
Recent projects need to use Flutter do UI Interface , It involves the operation of drawing ,Flutter I've just been in touch for another week , Not very familiar with , So I'm going to make a small Demo Experience it. , Find the feel .
Demo The goal of :
Point to where , Where there is a little red dot, follow .
Implementation steps :
1、 First, this is an activity page , Therefore, first of all, the implementation of the page should inherit StatefulWidget. In addition, it is necessary to obtain the user's touch event , So you need to build Method used in Listener This one widget:

You can see , By passing in onPointDown、onPointerMove、onPointerUp Callback implementation of , Can be in widget When being touched, the corresponding touch event type and touch coordinates are received . Then use setState Summon the frame to widget Update the tree , The control that needs to use this coordinate variable will read the value of the new variable , To do all kinds of drawing .
My realization is as follows :
@override
Widget build(BuildContext context) {
DotPainter painter = DotPainter(_globalPosition);
return Listener(
child: CustomPaint(
size: Size.infinite,
painter: painter,
foregroundPainter: painter,
child: null,
),
onPointerMove: (PointerMoveEvent e) => {
setState(() {
_globalPosition = e.localPosition;
})
},
);
}
2、Canvas Use :
In the code in the previous step , There is one DotPainter Widget, This is my custom CustomPainter, Why use this CustomPainter Well ? Because of the need to use Canvas Drawing , You need to use CustomPaint This widget Attached to StatefulWidget in , from CustomPaint hold Canvas Send it to CustomPainter Of paint In the method , So I can get it Canvas, Called in the framework setState to update UI Tree time , Draw what you want to draw to Canvas in , And finally displayed on the page .
The specific code implementation is as follows :
import 'dart:ui';
import 'package:flutter/material.dart';
class DotPainter extends CustomPainter {
Offset mGlobalPosition;
Paint _mPaint = Paint();
DotPainter(
this.mGlobalPosition // Pass in the coordinates you are touching
);
@override
void paint(Canvas canvas, Size size) {
_mPaint..color = Colors.red
..strokeWidth = 100
..strokeCap = StrokeCap.round
..isAntiAlias = true;
_drawXyDot(canvas);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
//todo Temporary anencephaly true
return true;
}
void _drawXyDot(Canvas canvas) {
print('globalPosition == $mGlobalPosition');
if (mGlobalPosition == null) return;
canvas.drawPoints(PointMode.points, [mGlobalPosition], _mPaint);
}
}
Which draw things need to use “ paint brush ”, The specific use method is similar to that of Android , It is just to set various painting properties .
in addition shouldRepaint Used to control every time widget When the tree is updated , When to redraw this widget The content of , So as to avoid multiple useless drawings . What we need here is to keep drawing , So no brain return true Just fine .
When mGlobalPosition When updated externally ,setState Trigger widget Tree drawing , here paint Method called back , Brought canvas, Then call my private base note. drawXyDot, At this point, I call canvas Of drawPoints Method , stay mGlobalPosition Use at recorded coordinates mPaint Recorded brush color 、 Draw a point with attributes such as thickness . After being called repeatedly , This process becomes a point that moves with my finger .
The actual effect :
It was here

Press to drag elsewhere :

flutter Cross end debugging is still very cool , Write a set of UI Code , No matter in Android End or end web The performance of the end is relatively consistent , however web End press F12 to glance at , Obviously, I want to load a large set of 2d The engine can draw the interface , It didn't generate h5 Elements , The performance of the web page is a little different from its real performance , But it's not a bad thing .
版权声明
本文为[cjzcjl]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210600587455.html
边栏推荐
- 数据分析学习目录
- SSDB Foundation
- Raspberry pie uses root operation, and the graphical interface uses its own file manager
- js 计算时间差
- c1000k TCP 连接上限测试
- SQL Server database in clause and exists clause conversion
- SQL常用的命令
- SSDB foundation 3
- Audio signal processing and coding - 2.5.3 the discrete cosine transform
- One of the reasons why the WebView web page cannot be opened (and some WebView problem records encountered by myself)
猜你喜欢

Redis optimization series (III) solve common problems after master-slave configuration

网络协议之:sctp流控制传输协议

2022.04.23(LC_763_划分字母区间)

Installation, use and problem summary of binlog2sql tool

binlog2sql 工具安装使用及问题汇总

redis优化系列(三)解决主从配置后的常见问题

Openharmony open source developer growth plan, looking for new open source forces that change the world!

MySQL学习第五弹——事务及其操作特性详解

Simplified path (force buckle 71)

Why is PostgreSQL about to surpass SQL Server?
随机推荐
Client interns of a large factory share their experience face to face
Redis optimization series (III) solve common problems after master-slave configuration
MySQL学习第五弹——事务及其操作特性详解
SSDB foundation 1
Using 8266 as serial port debugging tool
Esp01s with Arduino development environment
The flyer realizes page Jump through routing routes
C1000k TCP connection upper limit test
An algorithm problem was encountered during the interview_ Find the mirrored word pairs in the dictionary
Installation, use and problem summary of binlog2sql tool
[advanced level 11 of C language -- character and string functions and their simulation implementation (2)]
剑指 Offer II 116. 省份数量-空间复杂度O(n),时间复杂度O(n)
White screen processing method of fulter startup page
openlayers 5.0 热力图
An idea of rendering pipeline based on FBO
From technical system to business insight, the closing chapter of the practice of small and medium-sized R & D team structure
The type initializer for ‘Gdip‘ threw an exception
static类变量快速入门
高层次人才一站式服务平台开发 人才综合服务平台系统
Screen right-click menu in souI