-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti3_screen.dart
171 lines (122 loc) · 3.55 KB
/
multi3_screen.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import 'dart:ui';
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:math';
class Multi3 extends StatefulWidget{
@override
_Multi3 createState() => _Multi3();
}
Color globalColor = Colors.black; // for debug
double x = 0;
double y = 0;
double width = 0;
double height = 0;
Map<String, Object> objectsArr = {}; //массив для нажатий c сервера
class _Multi3 extends State<Multi3> with WidgetsBindingObserver{
var rng = new Random();
int id1 = 0; // ID user
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this); //some events of the flutter app: like resume / stop
id1 = rng.nextInt(10000000);
}
@override
void didChangeAppLifecycleState(final AppLifecycleState state) {
if(state == AppLifecycleState.paused){
print("PAUSED");
objectsArr = {};
setState(() { });
}
if (state == AppLifecycleState.resumed)
{
print("RESUME");
}
}
@override
void dispose() {
super.dispose();
addictArr = {};
setState(() { });
}
@override
Widget build(BuildContext context) {
return Listener(
onPointerDown: (e) {
setState(() {
x = e.position.dx.round().toDouble();
y = (e.position.dy).round().toDouble();
// e.pointer.toString() + "-"+ id1.toString() - it's like unic ID of finger
objectsArr[e.pointer.toString() + "-"+ id1.toString()] =
Object(x, y, e.pointer.toString() + "-"+ id1.toString());
});
},
onPointerMove: (e) {
setState(() {
x = e.position.dx.round().toDouble();
y = (e.position.dy).round().toDouble();
objectsArr.update(e.pointer.toString() + "-"+ id1.toString(),
(value) => Object(x, y, e.pointer.toString() + "-"+ id1.toString()));
});
},
onPointerUp: (e) {
setState(() {
x = e.position.dx.round().toDouble();
y = (e.position.dy).round().toDouble();
objectsArr.remove(e.pointer.toString() + "-"+ id1.toString());
}
);
},
// Idk, but maybe it's using in cancel (like movile off)
onPointerCancel: (e) {
setState(() {
x = e.position.dx.round().toDouble();
y = (e.position.dy).round().toDouble();
});
},
child: Container(
color: globalColor,
child: CustomPaint(
painter: ShapePainter(),
child: Container(
decoration: BoxDecoration(
color: Colors.transparent,
),
),
),
),
);
}
}
class ShapePainter extends CustomPainter {
// Color for painting
var painter = Paint()
..color = Colors.red
..strokeWidth = 5
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round;
@override
void paint(Canvas canvas, Size size) {
// paint all circles
objectsArr.forEach((key, value) {
canvas.drawCircle(value.getOffset(), value.radius, painter);
});
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true; // must have repaint
}
}
class Object {
double x = 0;
double y = 0;
String id = "0";
Object(this.x, this.y, this.id);
void setXY(double x, double y){
this.x = x;
this.y = y;
}
Offset getOffset(){
return Offset(this.x, this.y);
}
}