Skip to content

Commit

Permalink
PIN Lock added
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinX committed Feb 16, 2021
1 parent 4bbba9d commit d9ee788
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
37 changes: 37 additions & 0 deletions lib/views/home.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:yapgp/models/config.dart';
import 'package:yapgp/views/decrypt.dart';
import 'package:yapgp/views/settings.dart';
import 'package:flutter_screen_lock/lock_screen.dart';
import 'package:local_auth/local_auth.dart';

import 'contacts.dart';
import 'keys.dart';
Expand All @@ -13,9 +16,43 @@ class Home extends StatefulWidget {
}

class HomeState extends State<Home> {
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();

@override
void initState() {
super.initState();

_prefs.then((SharedPreferences prefs){
String isLocked = prefs.getString("pinlock");
if(isLocked != null && isLocked.isNotEmpty) {
Future.delayed(Duration.zero, (){
showLockScreen(
context: context,
correctString: isLocked,
canBiometric: true,
showBiometricFirst: true,
biometricAuthenticate: (context) async {
final localAuth = LocalAuthentication();
final didAuthenticate =
await localAuth.authenticateWithBiometrics(
localizedReason: 'Please authenticate');
if (didAuthenticate) {
return true;
}
return false;
},
onUnlocked: () => {},
canCancel: false
);
});
}
});

}

@override
Widget build(BuildContext context) {

return DefaultTabController(
length: 2,
child: Scaffold(
Expand Down
36 changes: 36 additions & 0 deletions lib/views/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:yapgp/main.dart';
import 'package:yapgp/models/config.dart';
import 'package:flutter_screen_lock/lock_screen.dart';

class Settings extends StatefulWidget {

Expand All @@ -16,6 +17,7 @@ class SettingsState extends State<Settings> {
bool useLightTheme = false;
String _keyType = Config.DEFAULT_KEY_TYPE;
int _keyLength = Config.DEFAULT_KEY_LENGTH;
String _pinLock = "";

@override
void initState() {
Expand All @@ -25,6 +27,10 @@ class SettingsState extends State<Settings> {
useLightTheme = (prefs.getInt("light_mode") != null) as bool;
_keyLength = prefs.getInt("keyLength");
_keyType = prefs.getString("keyType");
_pinLock = prefs.getString("pinlock");
if(_pinLock == null) {
_pinLock = "";
}
});
});
}
Expand Down Expand Up @@ -151,6 +157,29 @@ class SettingsState extends State<Settings> {
);
}

Future<void> _changePinLock(BuildContext context) async {
final SharedPreferences prefs = await _prefs;
if(_pinLock.isEmpty) {
showConfirmPasscode(
context: context,
confirmTitle: 'Confirm the passcode.',
onCompleted: (context, verifyCode) {
setState(() {
_pinLock = verifyCode;
});
// Please close yourself
Navigator.of(context).maybePop();
prefs.setString("pinlock", _pinLock);
},
);
return;
}
setState(() {
_pinLock = "";
});
prefs.remove("pinlock");
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -192,6 +221,13 @@ class SettingsState extends State<Settings> {
_changeKeyLengthDialog(context);
},
),
ListTile(
title: Text("PIN Lock"),
subtitle: Text(_pinLock.isEmpty ? "Not set" : _pinLock),
onTap: () {
_changePinLock(context);
},
),
],
)
);
Expand Down
14 changes: 14 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.11"
flutter_screen_lock:
dependency: "direct main"
description:
name: flutter_screen_lock
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.6"
flutter_secure_storage:
dependency: "direct main"
description:
Expand Down Expand Up @@ -289,6 +296,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
local_auth:
dependency: "direct main"
description:
name: local_auth
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.3+4"
logging:
dependency: transitive
description:
Expand Down
4 changes: 3 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.5+9
version: 1.0.5+10

environment:
sdk: ">=2.7.0 <3.0.0"
Expand All @@ -25,6 +25,8 @@ dependencies:
shared_preferences: ^0.5.12+4
flutter_form_builder: ^3.13.2
flutter_secure_storage: ^3.3.5
flutter_screen_lock: ^1.2.6
local_auth: ^0.6.3+4
flutter:
sdk: flutter

Expand Down

0 comments on commit d9ee788

Please sign in to comment.