当前位置:网站首页>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
边栏推荐
- Using Visual Studio code to develop Arduino
- Openlayers 5.0 discrete aggregation points
- An 8266 crash
- The type initializer for ‘Gdip‘ threw an exception
- Yyds dry goods inventory stringprep --- Internet string preparation
- [report] Microsoft: application of deep learning methods in speech enhancement
- SSDB Foundation
- Codeforces Round #784 (Div. 4)
- Openlayers 5.0 reload the map when the map container size changes
- openlayers 5.0 离散聚合点
猜你喜欢
WebView opens H5 video and displays gray background or black triangle button. Problem solved
2022.04.23(LC_763_划分字母区间)
12个例子夯实promise基础
[报告] Microsoft :Application of deep learning methods in speech enhancement
The difference between ordinary inner class and static inner class
微搭低代码零基础入门课(第三课)
开关电源设计分享及电源设计技巧图解
Esp01s with Arduino development environment
Keysight has chosen what equipment to buy for you
Raspberry pie 18b20 temperature
随机推荐
Network protocol: SCTP flow control transmission protocol
SSDB基础3
An algorithm problem was encountered during the interview_ Find the mirrored word pairs in the dictionary
在渤海期货办理开户安全吗。
Client interns of a large factory share their experience face to face
Getting started with vcpkg
開關電源設計分享及電源設計技巧圖解
Openlayers 5.0 reload the map when the map container size changes
std::stoi stol stoul stoll stof stod
12个例子夯实promise基础
2022.04.23(LC_763_划分字母区间)
arcgis js api dojoConfig配置
The type initializer for ‘Gdip‘ threw an exception
[play with lighthouse] Tencent cloud lightweight server builds a full platform video analysis video download website
Solutions such as unknown or garbled code or certificate problem prompt in Charles's mobile phone packet capture, actual measurement.
UML类图几种关系的总结
WebView opens H5 video and displays gray background or black triangle button. Problem solved
MySQL restores or rolls back data through binlog
Using 8266 as serial port debugging tool
ArcMap connecting ArcGIS Server