From cdf2b14f30d4eb65f510aa11947a1ae4fe98e37c Mon Sep 17 00:00:00 2001 From: krille-chan Date: Fri, 7 Jun 2024 12:23:09 +0200 Subject: [PATCH] fix: Auto unlock lock screen --- lib/widgets/lock_screen.dart | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/widgets/lock_screen.dart b/lib/widgets/lock_screen.dart index 93c0b54fb..17f4bef52 100644 --- a/lib/widgets/lock_screen.dart +++ b/lib/widgets/lock_screen.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; @@ -20,14 +21,14 @@ class _LockScreenState extends State { bool _inputBlocked = false; final TextEditingController _textEditingController = TextEditingController(); - void tryUnlock(BuildContext context) async { + void tryUnlock(String text) async { setState(() { _errorText = null; }); - if (_textEditingController.text.length < 4) return; + if (text.length < 4) return; - final enteredPin = int.tryParse(_textEditingController.text); - if (enteredPin == null || _textEditingController.text.length != 4) { + final enteredPin = int.tryParse(text); + if (enteredPin == null || text.length != 4) { setState(() { _errorText = L10n.of(context)!.invalidInput; }); @@ -90,9 +91,12 @@ class _LockScreenState extends State { autofocus: true, textAlign: TextAlign.center, readOnly: _inputBlocked, - onChanged: (_) => tryUnlock(context), - onSubmitted: (_) => tryUnlock(context), + onChanged: tryUnlock, + onSubmitted: tryUnlock, style: const TextStyle(fontSize: 40), + inputFormatters: [ + LengthLimitingTextInputFormatter(4), + ], decoration: InputDecoration( errorText: _errorText, hintText: '****',