Skip to content

Commit

Permalink
✨ auth at startup for desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
niuhuan committed Apr 8, 2024
1 parent 760c11c commit 5302988
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 40 deletions.
1 change: 1 addition & 0 deletions ci/version.info.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
v1.7.9
- [x] ✨ 下载批量删除
- [x] ✨ 桌面端密码

v1.7.8
- [x] ♻️ iOS可以使用FaceID进行解锁App
Expand Down
71 changes: 45 additions & 26 deletions lib/basic/config/Authentication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';

import 'package:flutter/material.dart';
import 'package:pikapika/basic/config/Platform.dart';
import 'package:pikapika/screens/DesktopAuthenticationScreen.dart';

import '../Common.dart';
import '../Method.dart';
Expand All @@ -10,45 +11,37 @@ const _propertyName = "authentication";
late bool _authentication;

Future<void> initAuthentication() async {
_authentication =
(await method.loadProperty(_propertyName, "false")) == "true";
if (Platform.isIOS || androidVersion >= 29) {
_authentication =
(await method.loadProperty(_propertyName, "false")) == "true";
}
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
_authentication = await needDesktopAuthentication();
}
}

bool currentAuthentication() {
return _authentication;
}

Future<void> _chooseAuthentication(BuildContext context) async {
if (await method.verifyAuthentication()) {
String? result =
await chooseListDialog<String>(context, "进入APP时验证身份", ["是", "否"]);
if (result != null) {
var target = result == "是";
await method.saveProperty(_propertyName, "$target");
_authentication = target;
}
Future<bool> verifyAuthentication(BuildContext context) async {
if (Platform.isIOS || androidVersion >= 29) {
return await method.verifyAuthentication();
}
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
return await Navigator.of(context).push(
MaterialPageRoute(builder: (context) => const VerifyPassword())) ==
true;
}
return false;
}

Widget authenticationSetting() {
if (Platform.isIOS) {
return StatefulBuilder(
builder: (BuildContext context, void Function(void Function()) setState) {
return ListTile(
title: const Text("进入APP时验证身份"),
subtitle: Text(_authentication ? "是" : "否"),
onTap: () async {
await _chooseAuthentication(context);
setState(() {});
},
);
},
);
} else if (androidVersion >= 29) {
if (Platform.isIOS || androidVersion >= 29) {
return StatefulBuilder(
builder: (BuildContext context, void Function(void Function()) setState) {
return ListTile(
title: const Text("进入APP时验证指纹(如果系统已经录入)"),
title: const Text("进入APP时验证身份(如果系统已经录入密码或指纹)"),
subtitle: Text(_authentication ? "是" : "否"),
onTap: () async {
await _chooseAuthentication(context);
Expand All @@ -58,5 +51,31 @@ Widget authenticationSetting() {
},
);
}
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
return StatefulBuilder(builder: (
BuildContext context,
void Function(void Function()) setState,
) {
return ListTile(
title: const Text("设置应用程序密码"),
onTap: () async {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => const SetPassword()));
},
);
});
}
return Container();
}

Future<void> _chooseAuthentication(BuildContext context) async {
if (await method.verifyAuthentication()) {
String? result =
await chooseListDialog<String>(context, "进入APP时验证身份", ["是", "否"]);
if (result != null) {
var target = result == "是";
await method.saveProperty(_propertyName, "$target");
_authentication = target;
}
}
}
122 changes: 122 additions & 0 deletions lib/screens/DesktopAuthenticationScreen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import 'package:flutter/material.dart';
import 'package:pikapika/basic/Method.dart';

const _key = "desktopAuthPassword";

Future<bool> needDesktopAuthentication() async {
return await method.loadProperty(_key, "") != "";
}

class VerifyPassword extends StatefulWidget {
const VerifyPassword({Key? key}) : super(key: key);

@override
State<StatefulWidget> createState() => _VerifyPasswordState();
}

class _VerifyPasswordState extends State<VerifyPassword> {
String _password = "";

@override
Widget build(BuildContext context) {
return Column(
children: [
TextField(
decoration: const InputDecoration(labelText: "当前密码"),
onChanged: (value) {
_password = value;
},
),
ElevatedButton(
onPressed: () async {
String savedPassword = await method.loadProperty(_key, "");
if (_password == savedPassword) {
Navigator.of(context).pop(true);
} else {
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text("密码错误")));
}
},
child: const Text("确定"),
),
],
);
}
}

class SetPassword extends StatefulWidget {
const SetPassword({Key? key}) : super(key: key);

@override
State<StatefulWidget> createState() => _SetPasswordState();
}

class _SetPasswordState extends State<SetPassword> {
String _password = "";
String _password2 = "";

@override
Widget build(BuildContext context) {
return Center(
child: Padding(
padding: const EdgeInsets.all(30),
child: Column(
children: [
const Text(
"密码初始化",
style: TextStyle(
height: 18,
),
),
Container(
height: 10,
),
TextField(
decoration: const InputDecoration(labelText: "密码"),
onChanged: (value) {
_password = value;
},
),
Container(
height: 10,
),
TextField(
decoration: const InputDecoration(labelText: "再次输入密码"),
onChanged: (value) {
_password2 = value;
},
),
Container(
height: 10,
),
Row(
children: [
ElevatedButton(
onPressed: () async {
Navigator.of(context).pop(false);
},
child: const Text("取消"),
),
Container(width: 10),
Expanded(
child: ElevatedButton(
onPressed: () async {
if (_password != _password2) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("两次输入的密码不一致")));
return;
}
await method.saveProperty(_key, _password);
Navigator.of(context).pop(true);
},
child: const Text("设置密码"),
),
),
],
),
],
),
),
);
}
}
2 changes: 1 addition & 1 deletion lib/screens/InitScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class _InitScreenState extends State<InitScreen> {
}

Future _goAuthentication() async {
if (await method.verifyAuthentication()) {
if (await verifyAuthentication(context)) {
_goApplication();
}
}
Expand Down
19 changes: 6 additions & 13 deletions lib/screens/components/PkzImages.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:pikapika/basic/Common.dart';
import 'package:pikapika/basic/Cross.dart';
import 'package:pikapika/basic/Method.dart';
import 'package:flutter_svg/svg.dart';
import 'package:pikapika/basic/config/ImageAddress.dart';
import 'dart:io';
import 'dart:ui' as ui show Codec;

import '../FilePhotoViewScreen.dart';
import 'Images.dart';

// 从本地加载图片
Expand Down Expand Up @@ -110,7 +101,6 @@ class _PkzImageState extends State<PkzImage> {
}
}


// 远端图片
class PkzLoadingImage extends StatefulWidget {
final String pkzPath;
Expand All @@ -135,7 +125,6 @@ class PkzLoadingImage extends StatefulWidget {
}

class _PkzLoadingImageState extends State<PkzLoadingImage> {

late bool _mock;
late Future<Uint8List> data;

Expand All @@ -147,8 +136,12 @@ class _PkzLoadingImageState extends State<PkzLoadingImage> {
final data = await method.loadPkzFile(widget.pkzPath, widget.path);
if (widget.onTrueSize != null) {
var decodedImage = await decodeImageFromList(data);
widget.onTrueSize!(Size(
decodedImage.width.toDouble(), decodedImage.height.toDouble(),),);
widget.onTrueSize!(
Size(
decodedImage.width.toDouble(),
decodedImage.height.toDouble(),
),
);
}
return data;
}();
Expand Down

0 comments on commit 5302988

Please sign in to comment.