Skip to content

Commit eaf40fc

Browse files
10456CarGuo
10456
authored andcommitted
update demo gesture password
1 parent 23c939b commit eaf40fc

File tree

8 files changed

+501
-0
lines changed

8 files changed

+501
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,18 @@ Map<String, WidgetBuilder> routers = {
610610
return un_bounded_listview.UnboundedListViewDemoPage();
611611
});
612612
},
613+
"PageView嵌套PageView": (context) {
614+
return ContainerAsyncRouterPage(
615+
pageview_in_pageview_demo_page.loadLibrary(), (context) {
616+
return pageview_in_pageview_demo_page.PageViewInPageViewDemoPage();
617+
});
618+
},
619+
"手势密码": (context) {
620+
return ContainerAsyncRouterPage(gesture_password_demo_page.loadLibrary(),
621+
(context) {
622+
return gesture_password_demo_page.GesturePasswordDemoPage();
623+
});
624+
},
613625
};
614626
615627
```

lib/main.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ import 'package:gsy_flutter_demo/widget/un_bounded_listview.dart'
212212
import 'package:gsy_flutter_demo/widget/pageview_in_pageview_demo_page.dart'
213213
deferred as pageview_in_pageview_demo_page;
214214

215+
import 'package:gsy_flutter_demo/widget/gesture_password/gesture_password_demo_page.dart'
216+
deferred as gesture_password_demo_page;
217+
215218
import 'package:window_location_href/window_location_href.dart';
216219

217220
void main() => runApp(const MyApp());
@@ -917,6 +920,12 @@ Map<String, WidgetBuilder> routers = {
917920
return pageview_in_pageview_demo_page.PageViewInPageViewDemoPage();
918921
});
919922
},
923+
"手势密码": (context) {
924+
return ContainerAsyncRouterPage(gesture_password_demo_page.loadLibrary(),
925+
(context) {
926+
return gesture_password_demo_page.GesturePasswordDemoPage();
927+
});
928+
},
920929
};
921930

922931
enum Cat {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:gsy_flutter_demo/widget/gesture_password/gesture_password_view.dart';
3+
import 'package:gsy_flutter_demo/widget/link_sliver/link_flexible_space_bar.dart';
4+
5+
class GesturePasswordDemoPage extends StatefulWidget {
6+
const GesturePasswordDemoPage({super.key});
7+
8+
@override
9+
_GesturePasswordDemoState createState() => _GesturePasswordDemoState();
10+
}
11+
12+
class _GesturePasswordDemoState extends State<GesturePasswordDemoPage> {
13+
String _pwd = '';
14+
15+
@override
16+
Widget build(BuildContext context) {
17+
return Scaffold(
18+
appBar: AppBar(
19+
title: const Text("手势密码"),
20+
),
21+
body: Center(
22+
child: Column(
23+
mainAxisAlignment: MainAxisAlignment.center,
24+
children: [
25+
SizedBox(
26+
width: 300,
27+
height: 300,
28+
child: GesturePasswordView(
29+
pathWidth: 6,
30+
frameRadius: 30,
31+
onDone: (value) {
32+
setState(() {
33+
_pwd = value.join();
34+
});
35+
},
36+
),
37+
),
38+
Text("当前密码: $_pwd"),
39+
],
40+
),
41+
),
42+
);
43+
}
44+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import 'package:flutter/material.dart';
2+
3+
import 'src/gesture_view_controller.dart';
4+
import 'src/gesture_view_path.dart';
5+
import 'src/gesture_view_point.dart';
6+
7+
class GesturePasswordView extends StatefulWidget {
8+
9+
/// 圆圈半径
10+
final double frameRadius;
11+
12+
/// 圆圈中心点半径
13+
final double pointRadius;
14+
15+
/// 圆圈普通状态下颜色
16+
final Color color;
17+
18+
/// 圆圈选中颜色
19+
final Color highlightColor;
20+
21+
/// 连线颜色
22+
final Color pathColor;
23+
24+
/// 连线半径
25+
final double pathWidth;
26+
27+
/// 手势结果
28+
final Function(List<int>)? onDone;
29+
30+
const GesturePasswordView({
31+
super.key,
32+
this.pointRadius = 10,
33+
this.frameRadius = 40,
34+
this.color = Colors.grey,
35+
this.highlightColor = Colors.blue,
36+
this.pathColor = Colors.blue,
37+
this.onDone,
38+
this.pathWidth = 5,
39+
});
40+
41+
@override
42+
State<StatefulWidget> createState() => _GesturePasswordState();
43+
}
44+
45+
class _GesturePasswordState extends State<GesturePasswordView> {
46+
final GestureViewController controller = GestureViewController();
47+
48+
@override
49+
void initState() {
50+
controller.initParameters(
51+
pointRadius: widget.pointRadius,
52+
frameRadius: widget.frameRadius,
53+
color: widget.color,
54+
highlightColor: widget.highlightColor,
55+
pathColor: widget.pathColor,
56+
onFinishGesture: widget.onDone,
57+
pathWidth: widget.pathWidth,
58+
updateView: (){
59+
setState(() {});
60+
}
61+
);
62+
WidgetsBinding.instance.addPostFrameCallback((_) => controller.setPointValues());
63+
super.initState();
64+
}
65+
66+
@override
67+
Widget build(BuildContext context) {
68+
return SizedBox(
69+
key: controller.globalKey,
70+
width: double.infinity,
71+
height: double.infinity,
72+
child: Stack(
73+
children: [
74+
GestureDotsPanelWidget(points: controller.point),
75+
GestureViewPathWidget(
76+
points: controller.pathPoint,
77+
pathWidth: controller.pathWidth,
78+
color: controller.pathColor,
79+
onPanDown: controller.onPanDown,
80+
onPanEnd: controller.onPanEnd,
81+
onPanUpdate: controller.onPanUpdate,
82+
),
83+
],
84+
),
85+
);
86+
}
87+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import 'package:flutter/material.dart';
2+
3+
import 'gesture_view_model.dart';
4+
5+
class GestureViewController {
6+
final List<GesturePasswordPointModel> _points = [];
7+
final List<Offset> _pathPoint = [];
8+
final GlobalKey globalKey = GlobalKey();
9+
double _frameRadius = 0.0;
10+
double _pointRadius = 0.0;
11+
Color _color = Colors.grey;
12+
Color _highlightColor = Colors.blue;
13+
Color _pathColor = Colors.blue;
14+
Function(List<int>)? _onFinishGesture;
15+
double _pathWidth = 5;
16+
Function()? _updateView;
17+
Offset? _firstPoint;
18+
Offset? _movePoint;
19+
List<int> _result = [];
20+
}
21+
22+
extension Data on GestureViewController {
23+
List<GesturePasswordPointModel> get point => _points;
24+
25+
List<Offset> get pathPoint {
26+
List<Offset> tempPathPoint = [];
27+
tempPathPoint.addAll(_pathPoint);
28+
if (_movePoint != null) {
29+
tempPathPoint.add(_movePoint!);
30+
}
31+
return tempPathPoint;
32+
}
33+
34+
Color get pathColor => _pathColor;
35+
36+
double get pathWidth => _pathWidth;
37+
38+
}
39+
40+
extension Private on GestureViewController {
41+
void _initPoint() {
42+
_points.addAll(List.generate(
43+
9,
44+
(index) => GesturePasswordPointModel(
45+
index: index,
46+
frameRadius: _frameRadius,
47+
pointRadius: _pointRadius,
48+
color: _color,
49+
highlightColor: _highlightColor,
50+
pathColor: _pathColor,
51+
),
52+
));
53+
}
54+
55+
double _getPointWidth(double width) => width / 3;
56+
}
57+
58+
extension Public on GestureViewController {
59+
void initParameters({
60+
double frameRadius = 0.0,
61+
double pointRadius = 0.0,
62+
Color color = Colors.grey,
63+
Color highlightColor = Colors.blue,
64+
Color pathColor = Colors.blue,
65+
Function(List<int>)? onFinishGesture,
66+
Function()? updateView,
67+
double pathWidth = 5,
68+
}) {
69+
_frameRadius = frameRadius;
70+
_pointRadius = pointRadius;
71+
_color = color;
72+
_highlightColor = highlightColor;
73+
_pathColor = pathColor;
74+
_onFinishGesture = onFinishGesture;
75+
_updateView = updateView;
76+
_pathWidth = pathWidth;
77+
_initPoint();
78+
}
79+
80+
void setPointValues() {
81+
try {
82+
Size size = globalKey.currentContext?.size ?? Size.zero;
83+
double pointWidth = _getPointWidth(size.width);
84+
List<Offset> pointCenter = [];
85+
for (int x = 1; x <= 3; x++) {
86+
for (int y = 1; y <= 3; y++) {
87+
Offset center = Offset((y - 1) * pointWidth + pointWidth / 2,
88+
(x - 1) * pointWidth + pointWidth / 2);
89+
pointCenter.add(center);
90+
}
91+
}
92+
for (int index = 0; index < pointCenter.length; index++) {
93+
_points[index].centerPoint = pointCenter[index];
94+
}
95+
} catch (_) {}
96+
}
97+
}
98+
99+
extension Tap on GestureViewController {
100+
void onPanDown(DragDownDetails e) {
101+
for (var item in _points) {
102+
if (item.containPoint(e.localPosition)) {
103+
item.selected = true;
104+
_firstPoint = e.localPosition;
105+
_pathPoint.add(item.centerPoint);
106+
_updateView?.call();
107+
_result.add(item.index);
108+
break;
109+
}
110+
}
111+
}
112+
113+
void onPanUpdate(DragUpdateDetails e) {
114+
if (_firstPoint == null) return;
115+
_movePoint = e.localPosition;
116+
for (var item in _points) {
117+
if (item.containPoint(e.localPosition)) {
118+
if (!item.selected) {
119+
item.selected = true;
120+
_pathPoint.add(item.centerPoint);
121+
_result.add(item.index);
122+
}
123+
break;
124+
}
125+
}
126+
_updateView?.call();
127+
}
128+
129+
void onPanEnd(DragEndDetails e) {
130+
_firstPoint = null;
131+
_movePoint = null;
132+
133+
_onFinishGesture?.call(_result);
134+
_result.clear();
135+
for (var element in _points) {
136+
element.selected = false;
137+
}
138+
_pathPoint.clear();
139+
_updateView?.call();
140+
}
141+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
import 'dart:math';
3+
import 'package:flutter/material.dart';
4+
5+
class GesturePasswordPointModel {
6+
Offset centerPoint = Offset.zero;
7+
bool selected = false;
8+
double frameRadius;
9+
double pointRadius;
10+
Color color = Colors.grey;
11+
Color highlightColor = Colors.blue;
12+
Color pathColor = Colors.blue;
13+
int index = 0;
14+
15+
GesturePasswordPointModel({
16+
this.index = 0,
17+
this.centerPoint = Offset.zero,
18+
this.frameRadius = 0.0,
19+
this.pointRadius = 0.0,
20+
this.selected = false,
21+
this.color = Colors.grey,
22+
this.highlightColor = Colors.blue,
23+
this.pathColor = Colors.blue,
24+
});
25+
26+
27+
Color get pointColor{
28+
return selected ? highlightColor : color;
29+
}
30+
Color get frameColor => selected ? highlightColor : color;
31+
32+
bool containPoint(Offset offset){
33+
return distanceTo(offset, centerPoint) <= frameRadius;
34+
}
35+
36+
37+
double distanceTo(Offset f1, Offset f2){
38+
var dx= f1.dx - f2.dx;
39+
var dy= f1.dy - f2.dy;
40+
return sqrt(dx * dx + dy * dy);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)