Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit cdec410

Browse files
committed
Apply flutter format
1 parent 9351fd7 commit cdec410

18 files changed

+399
-192
lines changed

example/lib/main.dart

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class _MyAppState extends State<MyApp> {
6363
'Your data is locked for privacy reasons. You need to unlock the app to access your data.',
6464

6565
/// Provide a widget that represents a single pin input field
66-
inputNodeBuilder: (index, state) => _InputField(state: state, index: index),
66+
inputNodeBuilder: (index, state) =>
67+
_InputField(state: state, index: index),
6768

6869
/// Provide a widget that describes what you want your lock screen to look like,
6970
/// given the state of the lock screen ([LockScreenConfiguration])
@@ -94,9 +95,12 @@ class _InputField extends StatelessWidget {
9495
}) : super(key: key);
9596
@override
9697
Widget build(BuildContext context) {
97-
final borderColor = state == InputFieldState.error ? Theme.of(context).errorColor : Theme.of(context).primaryColor;
98+
final borderColor = state == InputFieldState.error
99+
? Theme.of(context).errorColor
100+
: Theme.of(context).primaryColor;
98101
double borderWidth = 1;
99-
if (state == InputFieldState.focused || state == InputFieldState.filledAndFocused) {
102+
if (state == InputFieldState.focused ||
103+
state == InputFieldState.filledAndFocused) {
100104
borderWidth = 4;
101105
}
102106
return Container(
@@ -110,7 +114,8 @@ class _InputField extends StatelessWidget {
110114
width: borderWidth,
111115
),
112116
),
113-
child: state == InputFieldState.filled || state == InputFieldState.filledAndFocused
117+
child: state == InputFieldState.filled ||
118+
state == InputFieldState.filledAndFocused
114119
? Center(
115120
child: Container(
116121
width: 6,
@@ -142,7 +147,8 @@ class _Home extends StatelessWidget {
142147
),
143148
TextButton(
144149
onPressed: () {
145-
Navigator.of(context).push(MaterialPageRoute(builder: (_) => _SetupAuthWidget()));
150+
Navigator.of(context)
151+
.push(MaterialPageRoute(builder: (_) => _SetupAuthWidget()));
146152
},
147153
child: Text('Configure local authentication'),
148154
),
@@ -214,7 +220,8 @@ class _SetupAuthWidget extends StatelessWidget {
214220

215221
/// Pin input widget can be the same as on the lock screen, or you can provide a custom UI
216222
/// that you want to use when setting it up
217-
pinInputBuilder: (index, state) => _InputField(state: state, index: index),
223+
pinInputBuilder: (index, state) =>
224+
_InputField(state: state, index: index),
218225

219226
/// Overview refers to the first thing your user sees when getting to settings, before they have made
220227
/// any action, as well as after they made an action (such as changing pincode)
@@ -243,14 +250,17 @@ class _SetupAuthWidget extends StatelessWidget {
243250
),
244251

245252
/// In case of something going wrong, [OverviewConfiguration] provides an [error] property
246-
if (config.error != null) Text(config.error!.toString(), style: TextStyle(color: Colors.red)),
253+
if (config.error != null)
254+
Text(config.error!.toString(),
255+
style: TextStyle(color: Colors.red)),
247256

248257
/// If biometric authentication is available, provide an option to toggle it on or off
249258
if (config.isBiometricAuthAvailable == true)
250259
Row(
251260
mainAxisAlignment: MainAxisAlignment.spaceBetween,
252261
children: [
253-
const Text('Use fingerprint or face id to unlock the app'),
262+
const Text(
263+
'Use fingerprint or face id to unlock the app'),
254264
Switch(
255265
value: config.isBiometricAuthEnabled!,
256266
onChanged: (_) => config.onToggleBiometric(),
@@ -291,7 +301,8 @@ class _SetupAuthWidget extends StatelessWidget {
291301

292302
/// [configuration.error] provides details if something goes wrong (e.g., pins don't match)
293303
if (configuration.error != null)
294-
Text(configuration.error.toString(), style: TextStyle(color: Colors.red)),
304+
Text(configuration.error.toString(),
305+
style: TextStyle(color: Colors.red)),
295306

296307
/// [configuration.canSubmitChange] can optionaly be used to hide or disable submit button
297308
/// It is also possible to listen for this property and programatically trigger [config.onSubmitChange],
@@ -324,7 +335,9 @@ class _SetupAuthWidget extends StatelessWidget {
324335
style: TextStyle(color: Colors.red),
325336
),
326337
if (configuration.canSubmitChange)
327-
OutlinedButton(onPressed: configuration.onChangeSubmitted, child: Text('Save'))
338+
OutlinedButton(
339+
onPressed: configuration.onChangeSubmitted,
340+
child: Text('Save'))
328341
],
329342
),
330343
),
@@ -342,7 +355,8 @@ class _SetupAuthWidget extends StatelessWidget {
342355
configuration.newPinInputWidget,
343356
const Text('Confirm new pin'),
344357
configuration.confirmNewPinInputWidget,
345-
if (configuration.error != null && !_isCurrentPinIssue(configuration.error))
358+
if (configuration.error != null &&
359+
!_isCurrentPinIssue(configuration.error))
346360
Text(
347361
configuration.error!.toString(),
348362
style: const TextStyle(color: Colors.red),
@@ -359,6 +373,7 @@ class _SetupAuthWidget extends StatelessWidget {
359373
}
360374

361375
bool _isCurrentPinIssue(LocalAuthFailure? error) {
362-
return error == LocalAuthFailure.wrongPin || error == LocalAuthFailure.tooManyAttempts;
376+
return error == LocalAuthFailure.wrongPin ||
377+
error == LocalAuthFailure.tooManyAttempts;
363378
}
364379
}

lib/pin_lock.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ class PinLock {
5757
userId,
5858
);
5959

60-
static Future<Authenticator> baseAuthenticator(String userId) async => PinLock.authenticatorInstance(
61-
repository: LocalAuthenticationRepositoryImpl(await SharedPreferences.getInstance()),
60+
static Future<Authenticator> baseAuthenticator(String userId) async =>
61+
PinLock.authenticatorInstance(
62+
repository: LocalAuthenticationRepositoryImpl(
63+
await SharedPreferences.getInstance()),
6264
biometricAuthenticator: LocalAuthentication(),
6365
lockController: LockController(),
6466
userId: UserId(userId),

lib/src/blocs/cubit/lock_cubit.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import 'package:pin_lock/src/entities/value_objects.dart';
66

77
class LockCubit extends Cubit<LockScreenState> {
88
final Authenticator _authenticator;
9-
LockCubit(this._authenticator) : super(const LockScreenState(isLoading: true));
9+
LockCubit(this._authenticator)
10+
: super(const LockScreenState(isLoading: true));
1011

1112
Future<void> enterPin(String pin) async {
1213
emit(LockScreenState(pin: pin));
@@ -20,8 +21,10 @@ class LockCubit extends Cubit<LockScreenState> {
2021
}
2122

2223
Future<void> unlockWithBiometrics(String userFacingExplanation) async {
23-
final result = await _authenticator.unlockWithBiometrics(userFacingExplanation: userFacingExplanation);
24-
result.fold((l) => emit(LockScreenState(pin: state.pin, error: l)), (r) => null);
24+
final result = await _authenticator.unlockWithBiometrics(
25+
userFacingExplanation: userFacingExplanation);
26+
result.fold(
27+
(l) => emit(LockScreenState(pin: state.pin, error: l)), (r) => null);
2528
}
2629
}
2730

lib/src/blocs/cubit/setup_local_auth_cubit.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class SetuplocalauthCubit extends Cubit<SetupStage> {
1313
final lastState = state;
1414
if (lastState is Base) {
1515
final isPinAuthEnabled = await authenticator.isPinAuthenticationEnabled();
16-
emit(lastState.copyWith(isPinAuthEnabled: isPinAuthEnabled, isLoading: false));
16+
emit(lastState.copyWith(
17+
isPinAuthEnabled: isPinAuthEnabled, isLoading: false));
1718

1819
if (!isPinAuthEnabled) {
1920
emit(lastState.copyWith(
@@ -25,7 +26,8 @@ class SetuplocalauthCubit extends Cubit<SetupStage> {
2526
return;
2627
}
2728

28-
final biometrics = await authenticator.getBiometricAuthenticationAvailability();
29+
final biometrics =
30+
await authenticator.getBiometricAuthenticationAvailability();
2931
if (biometrics is Available) {
3032
emit(lastState.copyWith(
3133
isPinAuthEnabled: isPinAuthEnabled,
@@ -51,12 +53,14 @@ class SetuplocalauthCubit extends Cubit<SetupStage> {
5153
Future<void> toggleBiometricAuthentication() async {
5254
final lastState = state;
5355
if (lastState is Base) {
54-
final biometricAvailability = await authenticator.getBiometricAuthenticationAvailability();
56+
final biometricAvailability =
57+
await authenticator.getBiometricAuthenticationAvailability();
5558
if (biometricAvailability is Available) {
5659
final result = biometricAvailability.isEnabled
5760
? await authenticator.disableBiometricAuthentication()
5861
: await authenticator.enableBiometricAuthentication();
59-
result.fold((l) => emit(lastState.copyWith(error: l)), (r) => checkInitialState());
62+
result.fold((l) => emit(lastState.copyWith(error: l)),
63+
(r) => checkInitialState());
6064
}
6165
}
6266
}
@@ -117,7 +121,8 @@ class SetuplocalauthCubit extends Cubit<SetupStage> {
117121
Future<void> disablePinAuthentication() async {
118122
final lastState = state;
119123
if (lastState is Disabling) {
120-
final result = await authenticator.disableAuthenticationWithPin(pin: Pin(lastState.pin));
124+
final result = await authenticator.disableAuthenticationWithPin(
125+
pin: Pin(lastState.pin));
121126
result.fold(
122127
(l) => emit(lastState.copyWith(pin: '', error: l)),
123128
(r) => checkInitialState(),

lib/src/blocs/cubit/setup_stage.dart

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ class Base extends SetupStage {
3131
bool canGoFurther(int pinLength) => true;
3232

3333
@override
34-
List<Object?> get props => [isLoading, isPinAuthEnabled, isBiometricAuthAvailable, isBiometricAuthEnabled, error];
34+
List<Object?> get props => [
35+
isLoading,
36+
isPinAuthEnabled,
37+
isBiometricAuthAvailable,
38+
isBiometricAuthEnabled,
39+
error
40+
];
3541

3642
Base copyWith({
3743
bool? isLoading,
@@ -43,8 +49,10 @@ class Base extends SetupStage {
4349
Base(
4450
isLoading: isLoading ?? this.isLoading,
4551
isPinAuthEnabled: isPinAuthEnabled ?? this.isPinAuthEnabled,
46-
isBiometricAuthEnabled: isBiometricAuthEnabled ?? this.isBiometricAuthEnabled,
47-
isBiometricAuthAvailable: isBiometricAuthAvailable ?? this.isBiometricAuthAvailable,
52+
isBiometricAuthEnabled:
53+
isBiometricAuthEnabled ?? this.isBiometricAuthEnabled,
54+
isBiometricAuthAvailable:
55+
isBiometricAuthAvailable ?? this.isBiometricAuthAvailable,
4856
error: error,
4957
);
5058
}
@@ -62,7 +70,8 @@ class Enabling extends SetupStage {
6270
});
6371

6472
@override
65-
bool canGoFurther(int pinLength) => pin?.length == pinLength && confirmationPin?.length == pinLength;
73+
bool canGoFurther(int pinLength) =>
74+
pin?.length == pinLength && confirmationPin?.length == pinLength;
6675

6776
@override
6877
List<Object?> get props => [pin, confirmationPin, error];
@@ -93,7 +102,8 @@ class Disabling extends SetupStage {
93102
@override
94103
List<Object?> get props => [pin, error];
95104

96-
Disabling copyWith({int? pinLength, String? pin, LocalAuthFailure? error}) => Disabling(
105+
Disabling copyWith({int? pinLength, String? pin, LocalAuthFailure? error}) =>
106+
Disabling(
97107
pin: pin ?? this.pin,
98108
error: error,
99109
);
@@ -115,7 +125,9 @@ class ChangingPasscode extends SetupStage {
115125

116126
@override
117127
bool canGoFurther(int pinLength) =>
118-
currentPin.length == pinLength && newPin.length == pinLength && confirmationPin.length == pinLength;
128+
currentPin.length == pinLength &&
129+
newPin.length == pinLength &&
130+
confirmationPin.length == pinLength;
119131

120132
@override
121133
List<Object?> get props => [currentPin, confirmationPin, newPin, error];

lib/src/entities/authenticator.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ abstract class Authenticator with WidgetsBindingObserver {
4343

4444
/// Disables locking the app completely, including biometric authentication
4545
/// Only happens if provided [pin] is correct, or if [force] is true (which should be avoided)
46-
Future<Either<LocalAuthFailure, Unit>> disableAuthenticationWithPin({required Pin pin, bool force = false});
46+
Future<Either<LocalAuthFailure, Unit>> disableAuthenticationWithPin(
47+
{required Pin pin, bool force = false});
4748

4849
/// Disables biometric authentication. If [requirePin] is true, it is necessary to provide the correct [pin]
4950
/// before biometric authentication is disabled
@@ -73,7 +74,8 @@ abstract class Authenticator with WidgetsBindingObserver {
7374
Future<bool> isPinAuthenticationEnabled();
7475

7576
/// Triggers the OS's biometric authentication and changes the [lockState] if successful
76-
Future<Either<LocalAuthFailure, Unit>> unlockWithBiometrics({required String userFacingExplanation});
77+
Future<Either<LocalAuthFailure, Unit>> unlockWithBiometrics(
78+
{required String userFacingExplanation});
7779

7880
/// Make an attempt to unlock the app using provided [pin]. If the [pin] is correct,
7981
/// [lockState] will be changed and lock screen dismissed

lib/src/entities/authenticator_impl.dart

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ class AuthenticatorImpl with WidgetsBindingObserver implements Authenticator {
2727
final UserId userId;
2828

2929
AuthenticatorImpl(
30-
this._repository,
31-
this._biometricAuth,
32-
this._lockController,
33-
this.maxRetries,
34-
this.lockedOutDuration,
35-
this.lockAfterDuration,
36-
this.pinLength,
37-
this.userId,
30+
this._repository,
31+
this._biometricAuth,
32+
this._lockController,
33+
this.maxRetries,
34+
this.lockedOutDuration,
35+
this.lockAfterDuration,
36+
this.pinLength,
37+
this.userId,
3838
);
3939

4040
@override
@@ -70,8 +70,10 @@ class AuthenticatorImpl with WidgetsBindingObserver implements Authenticator {
7070
final lastActive = await _repository.getPausedTimestamp();
7171
if (lastActive != null) {
7272
final now = DateTime.now();
73-
if (now.millisecondsSinceEpoch - lastActive.millisecondsSinceEpoch > lockAfterDuration.inMilliseconds) {
74-
_lockController.lock(availableMethods: await getAvailableBiometricMethods());
73+
if (now.millisecondsSinceEpoch - lastActive.millisecondsSinceEpoch >
74+
lockAfterDuration.inMilliseconds) {
75+
_lockController.lock(
76+
availableMethods: await getAvailableBiometricMethods());
7577
}
7678
}
7779
break;
@@ -125,7 +127,8 @@ class AuthenticatorImpl with WidgetsBindingObserver implements Authenticator {
125127
}
126128

127129
@override
128-
Future<Either<LocalAuthFailure, Unit>> disableAuthenticationWithPin({required Pin pin, bool force = false}) async {
130+
Future<Either<LocalAuthFailure, Unit>> disableAuthenticationWithPin(
131+
{required Pin pin, bool force = false}) async {
129132
if (!force) {
130133
final isAuthenticationEnabled = await isPinAuthenticationEnabled();
131134
if (!isAuthenticationEnabled) {
@@ -215,20 +218,23 @@ class AuthenticatorImpl with WidgetsBindingObserver implements Authenticator {
215218
if (!isSupported) {
216219
return const Unavailable(reason: LocalAuthFailure.notAvailable);
217220
}
218-
final storedValue = await _repository.isBiometricAuthenticationEnabled(userId: userId);
221+
final storedValue =
222+
await _repository.isBiometricAuthenticationEnabled(userId: userId);
219223
return Available(isEnabled: storedValue ?? false);
220224
}
221225

222226
@override
223227
Future<bool> isPinAuthenticationEnabled() async {
224-
final storedValue = await _repository.isPinAuthenticationEnabled(userId: userId);
228+
final storedValue =
229+
await _repository.isPinAuthenticationEnabled(userId: userId);
225230
return storedValue ?? false;
226231
}
227232

228233
/// -- Usage --
229234
230235
@override
231-
Future<Either<LocalAuthFailure, Unit>> unlockWithPin({required Pin pin}) async {
236+
Future<Either<LocalAuthFailure, Unit>> unlockWithPin(
237+
{required Pin pin}) async {
232238
final isEnabled = await isPinAuthenticationEnabled();
233239
if (!isEnabled) {
234240
_lockController.unlock();
@@ -250,8 +256,10 @@ class AuthenticatorImpl with WidgetsBindingObserver implements Authenticator {
250256
}
251257

252258
@override
253-
Future<Either<LocalAuthFailure, Unit>> unlockWithBiometrics({required String userFacingExplanation}) async {
254-
final biometricAvailability = await getBiometricAuthenticationAvailability();
259+
Future<Either<LocalAuthFailure, Unit>> unlockWithBiometrics(
260+
{required String userFacingExplanation}) async {
261+
final biometricAvailability =
262+
await getBiometricAuthenticationAvailability();
255263
if (biometricAvailability is Available) {
256264
if (!biometricAvailability.isEnabled) {
257265
_lockController.lock(availableMethods: const []);
@@ -263,8 +271,8 @@ class AuthenticatorImpl with WidgetsBindingObserver implements Authenticator {
263271

264272
try {
265273
final isSuccessful = await _biometricAuth.authenticate(
266-
localizedReason: userFacingExplanation,
267-
biometricOnly: true,
274+
localizedReason: userFacingExplanation,
275+
biometricOnly: true,
268276
);
269277
if (isSuccessful) {
270278
_lockController.unlock();
@@ -291,9 +299,11 @@ class AuthenticatorImpl with WidgetsBindingObserver implements Authenticator {
291299
}
292300

293301
Future<bool> _isLockedDueToTooManyAttempts() async {
294-
final failedAttemptsList = await _repository.getListOfFailedAttempts(userId: userId);
302+
final failedAttemptsList =
303+
await _repository.getListOfFailedAttempts(userId: userId);
295304
if (failedAttemptsList.length > maxRetries) {
296-
if (DateTime.now().difference(failedAttemptsList.last) < lockedOutDuration) {
305+
if (DateTime.now().difference(failedAttemptsList.last) <
306+
lockedOutDuration) {
297307
return true;
298308
}
299309
}
@@ -308,7 +318,9 @@ class AuthenticatorImpl with WidgetsBindingObserver implements Authenticator {
308318
final biometric = await getBiometricAuthenticationAvailability();
309319
if (biometric is Available) {
310320
_lockController.lock(
311-
availableMethods: biometric.isEnabled ? await getAvailableBiometricMethods() : const [],
321+
availableMethods: biometric.isEnabled
322+
? await getAvailableBiometricMethods()
323+
: const [],
312324
);
313325
}
314326
if (biometric is Unavailable) {

0 commit comments

Comments
 (0)