当前位置:网站首页>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
边栏推荐
- Some records used by VS2010
- One stop service platform for high-level talents and development of comprehensive service platform system for talents
- openlayers 5.0 当地图容器大小改变时,重新加载地图
- 浅谈c语言指针的强制转换
- UML类图几种关系的总结
- Getting started with vcpkg
- ArcMap连接 arcgis server
- C: generic reflection
- 网络协议之:sctp流控制传输协议
- Parsing headless jsonarray arrays
猜你喜欢

An idea of rendering pipeline based on FBO

2021-2022-2 ACM集训队每周程序设计竞赛(8)题解

ArcMap连接 arcgis server
![[today in history] April 23: the first video uploaded on YouTube; Netease cloud music officially launched; The inventor of digital audio player was born](/img/0a/ed4eab6589e1c072edc247463e889e.png)
[today in history] April 23: the first video uploaded on YouTube; Netease cloud music officially launched; The inventor of digital audio player was born

Wechat applet part of the mobile phone Preview PDF did not respond

2022.04.23(LC_714_买卖股票的最佳时机含手续费)

该买什么设备,Keysight 给你挑好了
![[报告] Microsoft :Application of deep learning methods in speech enhancement](/img/29/2d2addd826359fdb0920e06ebedd29.png)
[报告] Microsoft :Application of deep learning methods in speech enhancement

After opening the original normal project, the dependency package displays red and does not exist.

Esp01s with Arduino development environment
随机推荐
Xlslib use
剑指 Offer II 116. 省份数量-空间复杂度O(n),时间复杂度O(n)
openlayers 5.0 加载arcgis server 切片服务
How about CICC wealth? Is it safe to open an account up there
Disable Ctrl + Alt + Del
JS controls the file type and size when uploading files
【玩转Lighthouse】腾讯云轻量服务器搭建全平台视频解析视频下载网站
SQL of contention for system time plus time in ocrale database
mysql_linux版本的下载及安装详解
SQL server requires to query the information of all employees with surname 'Wang'
ArcMap connecting ArcGIS Server
C1000k TCP connection upper limit test
c#:泛型反射
The platinum library cannot search the debug process records of some projection devices
How to uninstall easyton
Openharmony open source developer growth plan, looking for new open source forces that change the world!
Openlayers 5.0 thermal diagram
MySQL学习第五弹——事务及其操作特性详解
The type initializer for ‘Gdip‘ threw an exception
Wechat applet part of the mobile phone Preview PDF did not respond