11import 'dart:ui' as ui;
22
3- import 'package:flutter/foundation.dart' ;
43import 'package:flutter/material.dart' ;
54import 'package:flutter/widgets.dart' ;
65import 'package:gesture_password/circle_item_painter.dart' ;
76
87class 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;
1211 final ItemAttribute attribute;
1312 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+ });
2423
2524 @override
26- _GesturePasswordState createState () => new _GesturePasswordState ();
25+ _GesturePasswordState createState () => _GesturePasswordState ();
2726}
2827
2928class _GesturePasswordState extends State <GesturePassword > {
3029 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 > [] ;
3332
3433 @override
3534 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 ;
3737 num ver = widget.height / 6 ;
3838 //每个圆的中心点
3939 for (int i = 0 ; i < 9 ; i++ ) {
4040 num tempX = (i % 3 + 1 ) * 2 * hor - hor;
4141 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 ()));
4343 }
4444 super .initState ();
4545 }
4646
4747 @override
4848 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 (
5253 onPanUpdate: (DragUpdateDetails details) {
5354 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;
5658 //防止绘画越界
5759 if (touchPoint.dy < 0 ) {
58- touchPoint = new Offset (touchPoint.dx, 0.0 );
60+ touchPoint = Offset (touchPoint.dx, 0.0 );
5961 }
6062 if (touchPoint.dy > widget.height) {
61- touchPoint = new Offset (touchPoint.dx, widget.height);
63+ touchPoint = Offset (touchPoint.dx, widget.height);
6264 }
63- Circle circle = getOuterCircle (touchPoint);
65+ Circle ? circle = getOuterCircle (touchPoint);
6466 if (circle != null ) {
6567// print('circle.isUnSelected()${circle.isUnSelected()}');
6668 if (circle.isUnSelected ()) {
6769 lineList.add (circle);
6870 circle.setState (Circle .CIRCLE_SELECTED );
6971 if (widget.selectedCallback != null ) {
70- widget.selectedCallback (getPassword ());
72+ widget.selectedCallback? . call (getPassword ());
7173 }
7274
7375// print('circle.isUnSelected()2222${circle.isUnSelected()}');
@@ -80,13 +82,13 @@ class _GesturePasswordState extends State<GesturePassword> {
8082 onPanEnd: (DragEndDetails details) {
8183 setState (() {
8284 if (circleList
83- .where ((Circle itemCircle) => itemCircle.isSelected ())
84- .length >=
85+ .where ((Circle itemCircle) => itemCircle.isSelected ())
86+ .length >=
8587 4 ) {
86- widget.successCallback (getPassword ());
88+ widget.successCallback? . call (getPassword ());
8789 } else {
8890 if (widget.failCallback != null ) {
89- widget.failCallback ();
91+ widget.failCallback? . call ();
9092 }
9193 }
9294 touchPoint = Offset .zero;
@@ -97,9 +99,9 @@ class _GesturePasswordState extends State<GesturePassword> {
9799 }
98100 });
99101 },
100- child: new CustomPaint (
102+ child: CustomPaint (
101103 size: size,
102- painter: new CircleItemPainter (
104+ painter: CircleItemPainter (
103105 widget.attribute,
104106 touchPoint,
105107 circleList,
@@ -109,7 +111,7 @@ class _GesturePasswordState extends State<GesturePassword> {
109111 }
110112
111113 ///判断是否在圈里
112- Circle getOuterCircle (Offset offset) {
114+ Circle ? getOuterCircle (Offset offset) {
113115 for (int i = 0 ; i < 9 ; i++ ) {
114116 var cross = offset - circleList[i].offset;
115117 if (cross.dx.abs () < widget.attribute.focusDistance &&
@@ -169,12 +171,12 @@ class ItemAttribute {
169171 selectedColor: const Color (0xFF1565C0 ));
170172
171173 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 ,
179181 });
180182}
0 commit comments