Skip to content

Commit 0577a76

Browse files
authored
Merge pull request #9 from StanleyCocos/feature/update
update to flutter 3.0
2 parents eae8ef9 + 99d2b3f commit 0577a76

7 files changed

+88
-78
lines changed

example/lib/main.dart

+7-10
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,33 @@ void main() => runApp(new MyApp());
77
class MyApp extends StatelessWidget {
88
@override
99
Widget build(BuildContext context) {
10-
return new MaterialApp(
10+
return MaterialApp(
1111
title: 'Flutter Demo',
12-
theme: new ThemeData(
12+
theme: ThemeData(
1313
primarySwatch: Colors.blue,
1414
),
15-
home: new MyHomePage(title: 'Flutter Demo Home Page'),
15+
home: MyHomePage(title: 'Flutter Demo Home Page'),
1616
);
1717
}
1818
}
1919

2020
class MyHomePage extends StatefulWidget {
21-
MyHomePage({Key key, this.title}) : super(key: key);
21+
MyHomePage({Key? key, required this.title}) : super(key: key);
2222

2323
final String title;
2424

2525
@override
26-
_MyHomePageState createState() => new _MyHomePageState();
26+
_MyHomePageState createState() => _MyHomePageState();
2727
}
2828

2929
class _MyHomePageState extends State<MyHomePage> {
3030
GlobalKey<MiniGesturePasswordState> miniGesturePassword =
3131
new GlobalKey<MiniGesturePasswordState>();
3232

33-
GlobalKey<ScaffoldState> scaffoldState = new GlobalKey<ScaffoldState>();
34-
3533
@override
3634
Widget build(BuildContext context) {
3735
return new MaterialApp(
3836
home: new Scaffold(
39-
key: scaffoldState,
4037
appBar: new AppBar(
4138
title: new Text('Plugin example app'),
4239
),
@@ -53,13 +50,13 @@ class _MyHomePageState extends State<MyHomePage> {
5350
width: 200.0,
5451
successCallback: (s) {
5552
print("successCallback$s");
56-
scaffoldState.currentState?.showSnackBar(new SnackBar(
53+
ScaffoldMessenger.of(context).showSnackBar(new SnackBar(
5754
content: new Text('successCallback:$s')));
5855
miniGesturePassword.currentState?.setSelected('');
5956
},
6057
failCallback: () {
6158
print('failCallback');
62-
scaffoldState.currentState?.showSnackBar(
59+
ScaffoldMessenger.of(context).showSnackBar(
6360
new SnackBar(content: new Text('failCallback')));
6461
miniGesturePassword.currentState?.setSelected('');
6562
},

example/pubspec.yaml

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
name: example
22
description: A new Flutter project.
33

4+
environment:
5+
sdk: ">=2.15.0 <4.0.0"
6+
flutter: ">=3.0.0"
7+
48
dependencies:
59
flutter:
610
sdk: flutter
711

812
# The following adds the Cupertino Icons font to your application.
913
# Use with the CupertinoIcons class for iOS style icons.
10-
cupertino_icons: ^0.1.2
14+
cupertino_icons: ^1.0.5
1115
gesture_password:
1216
path: ../
1317

lib/circle_item_painter.dart

+9-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import 'package:gesture_password/gesture_password.dart';
44

55
class CircleItemPainter extends CustomPainter {
66
CircleItemPainter(
7-
this.itemAttribute, this.touchPoint, this.circleList, this.lineList);
7+
this.itemAttribute,
8+
this.touchPoint,
9+
this.circleList,
10+
this.lineList,
11+
);
812

913
final Offset touchPoint;
1014
final List<Circle> circleList;
@@ -14,23 +18,23 @@ class CircleItemPainter extends CustomPainter {
1418
@override
1519
void paint(Canvas canvas, Size size) {
1620
//没选中小圆
17-
final normalCirclePaint = new Paint()
21+
final normalCirclePaint = Paint()
1822
..color = itemAttribute.normalColor
1923
..style = PaintingStyle.fill;
2024

2125
//选中小圆
22-
final selectedCirclePaint = new Paint()
26+
final selectedCirclePaint = Paint()
2327
..color = itemAttribute.selectedColor
2428
..style = PaintingStyle.fill;
2529

2630
//选中大圆
27-
final selectedBigCirclePaint = new Paint()
31+
final selectedBigCirclePaint = Paint()
2832
..color = itemAttribute.selectedColor
2933
..style = PaintingStyle.stroke
3034
..strokeWidth = itemAttribute.circleStrokeWidth;
3135

3236
//线
33-
final linePaint = new Paint()
37+
final linePaint = Paint()
3438
..color = itemAttribute.selectedColor
3539
..style = PaintingStyle.fill
3640
..strokeWidth = itemAttribute.lineStrokeWidth;

lib/gesture_password.dart

+44-42
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,75 @@
11
import 'dart:ui' as ui;
22

3-
import 'package:flutter/foundation.dart';
43
import 'package:flutter/material.dart';
54
import 'package:flutter/widgets.dart';
65
import 'package:gesture_password/circle_item_painter.dart';
76

87
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;
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

2928
class _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
}

lib/mini_circle_view.dart

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import 'package:flutter/widgets.dart';
33
import 'package:gesture_password/mini_gesture_password.dart';
44

55
class MiniCircleView extends CustomPainter {
6-
MiniCircleView(this.itemAttribute, this.circleList, this.selectedStr);
6+
MiniCircleView(
7+
this.itemAttribute,
8+
this.circleList,
9+
this.selectedStr,
10+
);
711

812
final List<Circle> circleList;
913
final MiniItemAttribute itemAttribute;
@@ -12,12 +16,12 @@ class MiniCircleView extends CustomPainter {
1216
@override
1317
void paint(Canvas canvas, Size size) {
1418
//没选中小圆
15-
final normalCirclePaint = new Paint()
19+
final normalCirclePaint = Paint()
1620
..color = itemAttribute.normalColor
1721
..style = PaintingStyle.fill;
1822

1923
//选中小圆
20-
final selectedCirclePaint = new Paint()
24+
final selectedCirclePaint = Paint()
2125
..color = itemAttribute.selectedColor
2226
..style = PaintingStyle.fill;
2327

lib/mini_gesture_password.dart

+15-16
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ class MiniGesturePassword extends StatefulWidget {
66
final MiniItemAttribute attribute;
77
final double height;
88

9-
MiniGesturePassword(
10-
{Key key,
11-
this.attribute: MiniItemAttribute.normalAttribute,
12-
this.height: 60.0})
13-
: super(key: key);
9+
MiniGesturePassword({
10+
Key? key,
11+
this.attribute = MiniItemAttribute.normalAttribute,
12+
this.height = 60.0,
13+
}) : super(key: key);
1414

1515
@override
16-
MiniGesturePasswordState createState() => new MiniGesturePasswordState();
16+
MiniGesturePasswordState createState() => MiniGesturePasswordState();
1717
}
1818

1919
class MiniGesturePasswordState extends State<MiniGesturePassword> {
2020
Offset touchPoint = Offset.zero;
21-
List<Circle> circleList = new List<Circle>();
21+
List<Circle> circleList = <Circle>[];
2222
String selectedStr = '';
2323

2424
@override
@@ -29,7 +29,7 @@ class MiniGesturePasswordState extends State<MiniGesturePassword> {
2929
for (int i = 0; i < 9; i++) {
3030
num tempX = (i % 3 + 1) * 2 * hor - hor;
3131
num tempY = (i ~/ 3 + 1) * 2 * ver - ver;
32-
circleList.add(new Circle(new Offset(tempX, tempY), i));
32+
circleList.add(Circle(Offset(tempX.toDouble(), tempY.toDouble()), i));
3333
}
3434
super.initState();
3535
}
@@ -42,12 +42,11 @@ class MiniGesturePasswordState extends State<MiniGesturePassword> {
4242

4343
@override
4444
Widget build(BuildContext context) {
45-
var size = new Size(widget.height, widget.height);
46-
return new Container(
47-
child: new CustomPaint(
45+
var size = Size(widget.height, widget.height);
46+
return Container(
47+
child: CustomPaint(
4848
size: size,
49-
painter:
50-
new MiniCircleView(widget.attribute, circleList, selectedStr)),
49+
painter: MiniCircleView(widget.attribute, circleList, selectedStr)),
5150
);
5251
}
5352
}
@@ -87,8 +86,8 @@ class MiniItemAttribute {
8786
selectedColor: const Color(0xFF1565C0));
8887

8988
const MiniItemAttribute({
90-
this.normalColor,
91-
this.selectedColor,
92-
this.smallCircleR: 6.0,
89+
required this.normalColor,
90+
required this.selectedColor,
91+
this.smallCircleR = 6.0,
9392
});
9493
}

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: zhangruiyu <[email protected]>
55
homepage: https://github.com/zhangruiyu/flutter_gesture_password
66

77
environment:
8-
sdk: ">=2.3.0 <3.0.0"
8+
sdk: ">=2.15.0 <4.0.0"
99

1010

1111
dependencies:

0 commit comments

Comments
 (0)