From d9ee788bbe7d786e3450ca8b1cee3e1b5b84d1f9 Mon Sep 17 00:00:00 2001 From: Marin Basic Date: Tue, 16 Feb 2021 16:32:49 +0100 Subject: [PATCH] PIN Lock added --- lib/views/home.dart | 37 +++++++++++++++++++++++++++++++++++++ lib/views/settings.dart | 36 ++++++++++++++++++++++++++++++++++++ pubspec.lock | 14 ++++++++++++++ pubspec.yaml | 4 +++- 4 files changed, 90 insertions(+), 1 deletion(-) diff --git a/lib/views/home.dart b/lib/views/home.dart index 8408399..a911833 100644 --- a/lib/views/home.dart +++ b/lib/views/home.dart @@ -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'; @@ -13,9 +16,43 @@ class Home extends StatefulWidget { } class HomeState extends State { + Future _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( diff --git a/lib/views/settings.dart b/lib/views/settings.dart index 4ccd37f..7aa8780 100644 --- a/lib/views/settings.dart +++ b/lib/views/settings.dart @@ -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 { @@ -16,6 +17,7 @@ class SettingsState extends State { bool useLightTheme = false; String _keyType = Config.DEFAULT_KEY_TYPE; int _keyLength = Config.DEFAULT_KEY_LENGTH; + String _pinLock = ""; @override void initState() { @@ -25,6 +27,10 @@ class SettingsState extends State { useLightTheme = (prefs.getInt("light_mode") != null) as bool; _keyLength = prefs.getInt("keyLength"); _keyType = prefs.getString("keyType"); + _pinLock = prefs.getString("pinlock"); + if(_pinLock == null) { + _pinLock = ""; + } }); }); } @@ -151,6 +157,29 @@ class SettingsState extends State { ); } + Future _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( @@ -192,6 +221,13 @@ class SettingsState extends State { _changeKeyLengthDialog(context); }, ), + ListTile( + title: Text("PIN Lock"), + subtitle: Text(_pinLock.isEmpty ? "Not set" : _pinLock), + onTap: () { + _changePinLock(context); + }, + ), ], ) ); diff --git a/pubspec.lock b/pubspec.lock index db53ab8..11194ab 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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: @@ -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: diff --git a/pubspec.yaml b/pubspec.yaml index a9071c7..24866bf 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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" @@ -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