flutter_screen_lock 2.0.1-nullsafety flutter_screen_lock: ^2.0.1-nullsafety copied to clipboard
Provides the ability to lock the screen on ios and android. Biometric authentication can be used in addition to passcode.
import 'package:example/second_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screen_lock/circle_input_button.dart';
import 'package:flutter_screen_lock/lock_screen.dart';
import 'package:local_auth/local_auth.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Example',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({required this.title});
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
child: Text('Open Lock Screen'),
onPressed: () => showLockScreen(
context: context,
correctString: '1234',
onCompleted: (context, result) {
// if you specify this callback,
// you must close the screen yourself
Navigator.of(context).maybePop();
},
onUnlocked: () => print('Unlocked.'),
),
),
ElevatedButton(
child: Text('6 Digits'),
onPressed: () => showLockScreen(
context: context,
digits: 6,
correctString: '123456',
),
),
ElevatedButton(
child: Text('Use local_auth'),
onPressed: () => showLockScreen(
context: context,
correctString: '1234',
canBiometric: true,
// biometricButton is default Icon(Icons.fingerprint)
// When you want to change the icon with `BiometricType.face`, etc.
biometricButton: Icon(Icons.face),
biometricAuthenticate: (context) async {
final localAuth = LocalAuthentication();
final didAuthenticate = await localAuth.authenticate(
localizedReason: 'Please authenticate',
biometricOnly: true,
);
if (didAuthenticate) {
return true;
}
return false;
},
onUnlocked: () {
print('Unlocked.');
},
),
),
ElevatedButton(
child: Text('Open biometric first'),
onPressed: () => showLockScreen(
context: context,
correctString: '1234',
canBiometric: true,
showBiometricFirst: true,
biometricAuthenticate: (_) async {
final localAuth = LocalAuthentication();
final didAuthenticate = await localAuth.authenticate(
localizedReason: 'Please authenticate',
biometricOnly: true,
);
if (didAuthenticate) {
return true;
}
return false;
},
onUnlocked: () => print('Unlocked.'),
),
),
ElevatedButton(
child: Text('Go to another page after unlocked biometrics'),
onPressed: () => showLockScreen(
context: context,
correctString: '1234',
canBiometric: true,
showBiometricFirst: true,
biometricAuthenticate: (_) async {
final localAuth = LocalAuthentication();
final didAuthenticate = await localAuth.authenticate(
localizedReason: 'Please authenticate',
biometricOnly: true,
);
if (didAuthenticate) {
return true;
}
return false;
},
onUnlocked: () async => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondPage(),
),
),
),
),
ElevatedButton(
child: Text('Can\'t cancel'),
onPressed: () => showLockScreen(
context: context,
correctString: '1234',
canCancel: false,
),
),
ElevatedButton(
child: Text('Customize text'),
onPressed: () => showLockScreen(
context: context,
correctString: '1234',
cancelText: 'Close',
deleteText: 'Remove',
),
),
ElevatedButton(
child: Text('Confirm mode.'),
onPressed: () => showConfirmPasscode(
context: context,
onCompleted: (context, verifyCode) {
print(verifyCode);
Navigator.of(context).maybePop();
},
),
),
ElevatedButton(
child: Text('Change styles.'),
onPressed: () => showLockScreen(
context: context,
correctString: '1234',
backgroundColor: Colors.grey.shade50,
backgroundColorOpacity: 1,
circleInputButtonConfig: CircleInputButtonConfig(
textStyle: TextStyle(
fontSize: MediaQuery.of(context).size.width * 0.1,
color: Colors.white,
),
backgroundColor: Colors.blue,
backgroundOpacity: 0.5,
shape: RoundedRectangleBorder(
side: BorderSide(
width: 1,
color: Colors.blue,
style: BorderStyle.solid,
),
),
),
),
),
ElevatedButton(
child: Text('Max retries'),
onPressed: () => showLockScreen(
context: context,
correctString: '1234',
maxRetries: 0,
didMaxRetries: () {
Navigator.pop(context);
showAboutDialog(
context: context,
children: [
Text('The maximum number of retries has been reached.'),
],
);
},
biometricAuthenticate: (_) async {
final localAuth = LocalAuthentication();
final didAuthenticate = await localAuth.authenticate(
localizedReason: 'Please authenticate',
biometricOnly: true,
);
if (didAuthenticate) {
return true;
}
return false;
},
canBiometric: true,
),
),
],
),
),
),
);
}
}