1
1
import 'dart:ui' as ui;
2
2
3
- import 'package:flutter/foundation.dart' ;
4
3
import 'package:flutter/material.dart' ;
5
4
import 'package:flutter/widgets.dart' ;
6
5
import 'package:gesture_password/circle_item_painter.dart' ;
7
6
8
7
class GesturePassword extends StatefulWidget {
9
- final ValueChanged <String > successCallback;
10
- final ValueChanged <String > selectedCallback;
11
- final VoidCallback failCallback;
8
+ final ValueChanged <String >? successCallback;
9
+ final ValueChanged <String >? selectedCallback;
10
+ final VoidCallback ? failCallback;
12
11
final ItemAttribute attribute;
13
12
final double height;
14
- final double width;
15
-
16
- GesturePassword (
17
- { @ required this .successCallback,
18
- this .failCallback,
19
- this .selectedCallback,
20
- this .attribute: ItemAttribute .normalAttribute,
21
- this .height: 300.0 ,
22
- this .width,
23
- });
13
+ final double ? width;
14
+
15
+ GesturePassword ({
16
+ required this .successCallback,
17
+ this .failCallback,
18
+ this .selectedCallback,
19
+ this .attribute = ItemAttribute .normalAttribute,
20
+ this .height = 300.0 ,
21
+ this .width,
22
+ });
24
23
25
24
@override
26
- _GesturePasswordState createState () => new _GesturePasswordState ();
25
+ _GesturePasswordState createState () => _GesturePasswordState ();
27
26
}
28
27
29
28
class _GesturePasswordState extends State <GesturePassword > {
30
29
Offset touchPoint = Offset .zero;
31
- List <Circle > circleList = new List <Circle >() ;
32
- List <Circle > lineList = new List <Circle >() ;
30
+ List <Circle > circleList = < Circle > [] ;
31
+ List <Circle > lineList = < Circle > [] ;
33
32
34
33
@override
35
34
void initState () {
36
- num hor = (widget.width?? MediaQueryData .fromWindow (ui.window).size.width) / 6 ;
35
+ num hor =
36
+ (widget.width ?? MediaQueryData .fromWindow (ui.window).size.width) / 6 ;
37
37
num ver = widget.height / 6 ;
38
38
//每个圆的中心点
39
39
for (int i = 0 ; i < 9 ; i++ ) {
40
40
num tempX = (i % 3 + 1 ) * 2 * hor - hor;
41
41
num tempY = (i ~ / 3 + 1 ) * 2 * ver - ver;
42
- circleList.add (new Circle (new Offset (tempX, tempY), i.toString ()));
42
+ circleList.add (Circle (Offset (tempX. toDouble () , tempY. toDouble () ), i.toString ()));
43
43
}
44
44
super .initState ();
45
45
}
46
46
47
47
@override
48
48
Widget build (BuildContext context) {
49
- var size = new Size (
50
- widget.width?? MediaQueryData .fromWindow (ui.window).size.width, widget.height);
51
- return new GestureDetector (
49
+ var size = Size (
50
+ widget.width ?? MediaQueryData .fromWindow (ui.window).size.width,
51
+ widget.height);
52
+ return GestureDetector (
52
53
onPanUpdate: (DragUpdateDetails details) {
53
54
setState (() {
54
- RenderBox box = context.findRenderObject ();
55
- touchPoint = box.globalToLocal (details.globalPosition);
55
+ RenderBox ? box = context.findRenderObject () as RenderBox ? ;
56
+ Offset ? offset = box? .globalToLocal (details.globalPosition);
57
+ touchPoint = offset ?? Offset .zero;
56
58
//防止绘画越界
57
59
if (touchPoint.dy < 0 ) {
58
- touchPoint = new Offset (touchPoint.dx, 0.0 );
60
+ touchPoint = Offset (touchPoint.dx, 0.0 );
59
61
}
60
62
if (touchPoint.dy > widget.height) {
61
- touchPoint = new Offset (touchPoint.dx, widget.height);
63
+ touchPoint = Offset (touchPoint.dx, widget.height);
62
64
}
63
- Circle circle = getOuterCircle (touchPoint);
65
+ Circle ? circle = getOuterCircle (touchPoint);
64
66
if (circle != null ) {
65
67
// print('circle.isUnSelected()${circle.isUnSelected()}');
66
68
if (circle.isUnSelected ()) {
67
69
lineList.add (circle);
68
70
circle.setState (Circle .CIRCLE_SELECTED );
69
71
if (widget.selectedCallback != null ) {
70
- widget.selectedCallback (getPassword ());
72
+ widget.selectedCallback? . call (getPassword ());
71
73
}
72
74
73
75
// print('circle.isUnSelected()2222${circle.isUnSelected()}');
@@ -80,13 +82,13 @@ class _GesturePasswordState extends State<GesturePassword> {
80
82
onPanEnd: (DragEndDetails details) {
81
83
setState (() {
82
84
if (circleList
83
- .where ((Circle itemCircle) => itemCircle.isSelected ())
84
- .length >=
85
+ .where ((Circle itemCircle) => itemCircle.isSelected ())
86
+ .length >=
85
87
4 ) {
86
- widget.successCallback (getPassword ());
88
+ widget.successCallback? . call (getPassword ());
87
89
} else {
88
90
if (widget.failCallback != null ) {
89
- widget.failCallback ();
91
+ widget.failCallback? . call ();
90
92
}
91
93
}
92
94
touchPoint = Offset .zero;
@@ -97,9 +99,9 @@ class _GesturePasswordState extends State<GesturePassword> {
97
99
}
98
100
});
99
101
},
100
- child: new CustomPaint (
102
+ child: CustomPaint (
101
103
size: size,
102
- painter: new CircleItemPainter (
104
+ painter: CircleItemPainter (
103
105
widget.attribute,
104
106
touchPoint,
105
107
circleList,
@@ -109,7 +111,7 @@ class _GesturePasswordState extends State<GesturePassword> {
109
111
}
110
112
111
113
///判断是否在圈里
112
- Circle getOuterCircle (Offset offset) {
114
+ Circle ? getOuterCircle (Offset offset) {
113
115
for (int i = 0 ; i < 9 ; i++ ) {
114
116
var cross = offset - circleList[i].offset;
115
117
if (cross.dx.abs () < widget.attribute.focusDistance &&
@@ -169,12 +171,12 @@ class ItemAttribute {
169
171
selectedColor: const Color (0xFF1565C0 ));
170
172
171
173
const ItemAttribute ({
172
- this .normalColor,
173
- this .selectedColor,
174
- this .lineStrokeWidth: 2.0 ,
175
- this .circleStrokeWidth: 2.0 ,
176
- this .smallCircleR: 10.0 ,
177
- this .bigCircleR: 30.0 ,
178
- this .focusDistance: 25.0 ,
174
+ required this .normalColor,
175
+ required this .selectedColor,
176
+ this .lineStrokeWidth = 2.0 ,
177
+ this .circleStrokeWidth = 2.0 ,
178
+ this .smallCircleR = 10.0 ,
179
+ this .bigCircleR = 30.0 ,
180
+ this .focusDistance = 25.0 ,
179
181
});
180
182
}
0 commit comments