diff --git a/lib/passcode_screen.dart b/lib/passcode_screen.dart index 41f51a9..270a23a 100644 --- a/lib/passcode_screen.dart +++ b/lib/passcode_screen.dart @@ -15,6 +15,8 @@ typedef CancelCallback = void Function(); class PasscodeScreen extends StatefulWidget { final Widget title; final int passwordDigits; + final bool variablePinLength; + final Widget? confirmButton; final PasswordEnteredCallback passwordEnteredCallback; // Cancel button and delete button will be switched based on the screen state final Widget cancelButton; @@ -35,6 +37,8 @@ class PasscodeScreen extends StatefulWidget { Key? key, required this.title, this.passwordDigits = 6, + this.variablePinLength = false, + this.confirmButton, required this.passwordEnteredCallback, required this.cancelButton, required this.deleteButton, @@ -124,9 +128,18 @@ class _PasscodeScreenState extends State ), ), ), + if (widget.variablePinLength) + Positioned( + child: Align( + alignment: Alignment.bottomRight, + child: _buildConfirmButton(), + ), + ), Positioned( child: Align( - alignment: Alignment.bottomRight, + alignment: (widget.variablePinLength) + ? Alignment.bottomLeft + : Alignment.bottomRight, child: _buildDeleteButton(), ), ), @@ -179,9 +192,18 @@ class _PasscodeScreenState extends State ), ), ), + if (widget.variablePinLength) + Positioned( + child: Align( + alignment: Alignment.bottomRight, + child: _buildConfirmButton(), + ), + ), Positioned( child: Align( - alignment: Alignment.bottomRight, + alignment: (widget.variablePinLength) + ? Alignment.bottomLeft + : Alignment.bottomRight, child: _buildDeleteButton(), ), ) @@ -200,7 +222,12 @@ class _PasscodeScreenState extends State var list = []; var config = widget.circleUIConfig; var extraSize = animation.value; - for (int i = 0; i < widget.passwordDigits; i++) { + for (int i = 0; + i < + ((widget.variablePinLength) + ? enteredPasscode.length + : widget.passwordDigits); + i++) { list.add( Container( margin: EdgeInsets.all(8), @@ -215,6 +242,12 @@ class _PasscodeScreenState extends State return list; } + _onConfirmButtonPressed() { + if (enteredPasscode.length > 0) { + widget.passwordEnteredCallback(enteredPasscode); + } + } + _onDeleteCancelButtonPressed() { if (enteredPasscode.length > 0) { setState(() { @@ -234,9 +267,11 @@ class _PasscodeScreenState extends State return; } setState(() { - if (enteredPasscode.length < widget.passwordDigits) { + if ((widget.variablePinLength && enteredPasscode.length < 10) || + enteredPasscode.length < widget.passwordDigits) { enteredPasscode += text; - if (enteredPasscode.length == widget.passwordDigits) { + if (!widget.variablePinLength && + enteredPasscode.length == widget.passwordDigits) { widget.passwordEnteredCallback(enteredPasscode); } } @@ -291,4 +326,21 @@ class _PasscodeScreenState extends State ), ); } + + Widget _buildConfirmButton() { + return Container( + child: CupertinoButton( + onPressed: _onConfirmButtonPressed, + child: Container( + margin: widget.keyboardUIConfig.digitInnerMargin, + child: widget.confirmButton ?? + Text( + 'OK', + style: const TextStyle(fontSize: 16, color: Colors.white), + semanticsLabel: 'OK', + ), + ), + ), + ); + } }