当前位置:网站首页>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
边栏推荐
- An idea of rendering pipeline based on FBO
- 机器学习目录
- SSDB foundation 1
- The difference between ordinary inner class and static inner class
- c#:泛型反射
- Matlab 2019 installation of deep learning toolbox model for googlenet network
- Simplified path (force buckle 71)
- Openharmony open source developer growth plan, looking for new open source forces that change the world!
- Coolweather is revised and connected to the wind weather interface to realize the broken line diagram of temperature
- Disable Ctrl + Alt + Del
猜你喜欢

Simple use of navigation in jetpack

mysql_ Download and installation of Linux version

Switching power supply design sharing and power supply design skills diagram

为何PostgreSQL即将超越SQL Server?

ArcMap publishing slicing service

Some records used by VS2010

mysql_linux版本的下载及安装详解

Wechat applet part of the mobile phone Preview PDF did not respond
![[记录]TypeError: this.getOptions is not a function](/img/c9/0d92891b6beec3d6085bd3da516f00.png)
[记录]TypeError: this.getOptions is not a function

開關電源設計分享及電源設計技巧圖解
随机推荐
Accessing private members using templates
openlayers draw矩形
Speculation on the way to realize the smooth drag preview of video editing software
2022.04.23(LC_763_划分字母区间)
SSDB foundation 3
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
ArcMap connecting ArcGIS Server
SSDB基础
mysql_ Download and installation of Linux version
2022.04.23 (the best time for lc_714_to buy and sell stocks, including handling charges)
The flyer realizes page Jump through routing routes
2022.04.23(LC_714_买卖股票的最佳时机含手续费)
An 8266 crash
简化路径(力扣71)
Openlayers 5.0 reload the map when the map container size changes
Audio signal processing and coding - 2.5.3 the discrete cosine transform
js 计算时间差
[advanced level 11 of C language -- character and string functions and their simulation implementation (2)]
Tencent map and high logo removal method
Using bafayun to control the computer