当前位置:网站首页>Flutter 手势监听和画板实现
Flutter 手势监听和画板实现
2022-07-29 04:40:00 【曲黎雪】
直接代码环节
核心逻辑就是监听用户手势,然后使用drawPath的方式,来绘制用户的手势路径
代码里都有注释,后面可以自定义画笔颜色和画笔粗细
import 'package:flutter/material.dart';
///画板的实现逻辑
///
///首先是绘制部分
///想法是使用path的方式,也就是两个点
///一个是moveTo,一个是lineTo
///
/// 手势部分,
/// 监听手指落下,和手指抬起即可
class CanvasPage extends StatefulWidget {
const CanvasPage({Key? key}) : super(key: key);
@override
State<CanvasPage> createState() => _CanvasPageState();
}
class _CanvasPageState extends State<CanvasPage> {
Path path = Path();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('手势监听+画板')),
body: Listener(
///落下
onPointerDown: (p) {
path.moveTo(p.localPosition.dx, p.localPosition.dy);
setState(() {});
},
///移动
onPointerMove: (p) {
path.lineTo(p.localPosition.dx, p.localPosition.dy);
setState(() {});
},
///离开
onPointerUp: (p) {
path.moveTo(p.localPosition.dx, p.localPosition.dy);
path.close();
setState(() {});
},
child: CustomPaint(
foregroundPainter: CanvasPaint(path: path),
child: Container(
color: Colors.transparent,
),
),
),
);
}
}
class CanvasPaint extends CustomPainter {
Path? path;
///画笔颜色
Color? color;
///画笔粗细
double? width;
CanvasPaint(
{required this.path, this.color = Colors.black, this.width = 5.0});
@override
void paint(Canvas canvas, Size size) {
canvas.drawColor(Colors.transparent, BlendMode.color);
Paint paint = Paint()
..color = color!
..strokeWidth = width!
..style = PaintingStyle.stroke;
canvas.drawPath(path!, paint);
}
///是否需要重新绘制
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}
效果图

边栏推荐
- 删除word文档中的空白页
- Mujoco and mujoco_ Install libxcursor.so 1:NO such dictionary
- Sign the college entrance examination
- Pycharm reports an error when connecting to the virtual machine database
- 使用近场探头和电流探头进行EMI干扰排查
- Makefile(make)常见规则(二)
- Pytorch fixed random seed & recurrence model
- settings.xml
- Pytorch GPU and CPU models load each other
- UE 在场景或UMG中播放视频
猜你喜欢

Use of construction methods

MySQL - deep parsing of MySQL index data structure

Mujoco and mujoco_ Install libxcursor.so 1:NO such dictionary

Deep analysis of data storage in memory (Advanced C language)

Hengxing Ketong invites you to the 24th China expressway informatization conference and technical product exhibition in Hunan

Classes and objects (III)

Vscode one click compilation and debugging

【Express连接MySQL数据库】

WebRTC实现简单音视频通话功能

MySQL - clustered index and secondary index
随机推荐
钉钉对话框文子转换成图片 不能复制粘贴到文档上
谷歌浏览器 打开网页出现 out of memory
安装spinning up教程里与mujoco对应的gym,报错mjpro150
Record the Niua packaging deployment project
Ethernet of network
删除word文档中的空白页
Mujoco and mujoco_ Install libxcursor.so 1:NO such dictionary
Auto.js脚本开发环境搭建
[QT learning notes] * insert pictures in the window
恒星科通邀您“湘”约第24届中国高速公路信息化大会暨技术产品展示会
Make a virtual human with zego avatar | virtual anchor live broadcast solution
盒子水平垂直居中布局(总结)
MySQL - deep parsing of MySQL index data structure
Sign the college entrance examination
TypeError: Cannot read properties of undefined (reading ‘then‘)
Mongo Shell交互式命令窗口
C language: typedef knowledge points summary
【Express连接MySQL数据库】
Vscode configuration makefile compilation
WebRTC实现简单音视频通话功能