Skip to content

Commit

Permalink
Merge pull request #86 from naoki0719:naoki0719/issue85
Browse files Browse the repository at this point in the history
Added option for onValidate callback.
  • Loading branch information
naoki0719 authored Jul 21, 2022
2 parents 3377edd + 245633b commit cba503f
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [7.0.3] - 2022-07-21

- Added option for onValidate callback.
- ⚠️There is a known bug where Secrets are not satisfied if the callback delays processing.

## [7.0.3] - 2022-07-19

- Added useLandscape property.
Expand Down
14 changes: 14 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screen_lock/flutter_screen_lock.dart';
Expand Down Expand Up @@ -295,6 +297,18 @@ class _MyHomePageState extends State<MyHomePage> {
),
child: const Text('Disable landscape mode'),
),
ElevatedButton(
onPressed: () => screenLock(
context: context,
digits: 4,
correctString: '',
onValidate: (x) async {
sleep(Duration(milliseconds: 500));
return '1234' == x;
},
),
child: const Text('Callback validation'),
),
],
),
),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ packages:
path: ".."
relative: true
source: path
version: "7.0.3"
version: "7.0.4"
intl:
dependency: transitive
description:
Expand Down
3 changes: 3 additions & 0 deletions lib/src/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import 'package:flutter_screen_lock/flutter_screen_lock.dart';
/// - `withBlur`: Blur the background
/// - `secretsBuilder`: Custom secrets animation widget builder
/// - `useLandscape`: Use a landscape orientation. Default `true`
/// - `onValidate`: Callback to validate input values filled in [digits].
Future<void> screenLock({
required BuildContext context,
required String correctString,
Expand Down Expand Up @@ -60,6 +61,7 @@ Future<void> screenLock({
bool withBlur = true,
SecretsBuilderCallback? secretsBuilder,
bool useLandscape = true,
ValidationCallback? onValidate,
}) async {
return Navigator.push<void>(
context,
Expand Down Expand Up @@ -96,6 +98,7 @@ Future<void> screenLock({
withBlur: withBlur,
secretsBuilder: secretsBuilder,
useLandscape: useLandscape,
onValidate: onValidate,
),
),
transitionsBuilder: (context, animation, secondaryAnimation, child) =>
Expand Down
22 changes: 17 additions & 5 deletions lib/src/input_controller.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screen_lock/flutter_screen_lock.dart';

class InputController {
InputController();

late int _digits;
late String _correctString;
late bool _isConfirmed;
late ValidationCallback? _validationCallback;

final List<String> _currentInputs = [];

Expand Down Expand Up @@ -91,6 +93,15 @@ class InputController {
correctString = _correctString;
}

if (_validationCallback == null) {
_localValidation(inputText, correctString);
} else {
_validationCallback!(inputText)
.then((success) => _verifyController.add(success));
}
}

void _localValidation(String inputText, String correctString) {
if (inputText == correctString) {
_verifyController.add(true);
} else {
Expand All @@ -99,18 +110,19 @@ class InputController {
}

/// Create each stream.
void initialize({
required int digits,
required String correctString,
bool isConfirmed = false,
}) {
void initialize(
{required int digits,
required String correctString,
bool isConfirmed = false,
ValidationCallback? onValidate}) {
_inputValueNotifier = ValueNotifier<String>('');
_verifyController = StreamController.broadcast();
_confirmedController = StreamController.broadcast();

_digits = digits;
_correctString = correctString;
_isConfirmed = isConfirmed;
_validationCallback = onValidate;
}

/// Close all streams.
Expand Down
22 changes: 20 additions & 2 deletions lib/src/screen_lock.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ typedef SecretsBuilderCallback = Widget Function(
Stream<bool> verifyStream,
);

typedef ValidationCallback = Future<bool> Function(
String input,
);

class ScreenLock extends StatefulWidget {
const ScreenLock({
Key? key,
Expand Down Expand Up @@ -46,6 +50,7 @@ class ScreenLock extends StatefulWidget {
this.withBlur = true,
this.secretsBuilder,
this.useLandscape = true,
this.onValidate,
}) : title = title ?? const Text('Please enter passcode.'),
confirmTitle =
confirmTitle ?? const Text('Please enter confirm passcode.'),
Expand Down Expand Up @@ -139,6 +144,11 @@ class ScreenLock extends StatefulWidget {
/// Use a landscape orientation.
final bool useLandscape;

/// Callback to validate input values filled in [digits].
///
/// If `true` is returned, the lock is unlocked.
final ValidationCallback? onValidate;

@override
State<ScreenLock> createState() => _ScreenLockState();
}
Expand Down Expand Up @@ -272,6 +282,7 @@ class _ScreenLockState extends State<ScreenLock> {
correctString: widget.correctString,
digits: widget.digits,
isConfirmed: widget.confirmation,
onValidate: widget.onValidate,
);

inputController.verifyInput.listen((success) {
Expand Down Expand Up @@ -303,8 +314,15 @@ class _ScreenLockState extends State<ScreenLock> {

@override
Widget build(BuildContext context) {
final secretLength =
widget.confirmation ? widget.digits : widget.correctString.length;
late final int secretLength;

if (widget.confirmation) {
secretLength = widget.digits;
} else {
secretLength = widget.correctString.isNotEmpty
? widget.correctString.length
: widget.digits;
}

final orientations = <Orientation, Axis>{
Orientation.portrait: Axis.vertical,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: flutter_screen_lock
description: Provides the ability to lock the screen on ios and android. Biometric authentication can be used in addition to passcode.
homepage: https://github.com/naoki0719/flutter_screen_lock
version: 7.0.3
version: 7.0.4

environment:
sdk: ">=2.17.0-0 <3.0.0"
Expand Down

0 comments on commit cba503f

Please sign in to comment.