fluffychat/lib/widgets/lock_screen.dart

122 lines
3.4 KiB
Dart
Raw Permalink Normal View History

2023-09-23 05:49:34 +00:00
import 'dart:async';
2021-05-01 09:43:54 +00:00
import 'package:flutter/material.dart';
2024-06-07 10:23:09 +00:00
import 'package:flutter/services.dart';
2021-10-26 16:50:34 +00:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-05-01 09:43:54 +00:00
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/config/themes.dart';
import 'package:fluffychat/widgets/app_lock.dart';
2021-05-01 09:43:54 +00:00
class LockScreen extends StatefulWidget {
2023-09-23 05:49:34 +00:00
const LockScreen({super.key});
2021-10-14 16:09:30 +00:00
2021-05-01 09:43:54 +00:00
@override
2023-09-23 05:49:34 +00:00
State<LockScreen> createState() => _LockScreenState();
2021-05-01 09:43:54 +00:00
}
2023-09-23 05:49:34 +00:00
class _LockScreenState extends State<LockScreen> {
String? _errorText;
int _coolDownSeconds = 5;
bool _inputBlocked = false;
2021-05-01 09:43:54 +00:00
final TextEditingController _textEditingController = TextEditingController();
2023-09-23 05:49:34 +00:00
2024-06-07 10:23:09 +00:00
void tryUnlock(String text) async {
2023-09-23 05:49:34 +00:00
setState(() {
_errorText = null;
});
2024-06-07 10:23:09 +00:00
if (text.length < 4) return;
2023-09-23 05:49:34 +00:00
2024-06-07 10:23:09 +00:00
final enteredPin = int.tryParse(text);
if (enteredPin == null || text.length != 4) {
2023-09-23 05:49:34 +00:00
setState(() {
_errorText = L10n.of(context)!.invalidInput;
});
_textEditingController.clear();
return;
}
if (AppLock.of(context).unlock(enteredPin.toString())) {
setState(() {
_inputBlocked = false;
_errorText = null;
});
_textEditingController.clear();
2023-09-23 05:49:34 +00:00
return;
}
setState(() {
_errorText = L10n.of(context)!.wrongPinEntered(_coolDownSeconds);
_inputBlocked = true;
});
Future.delayed(Duration(seconds: _coolDownSeconds)).then((_) {
setState(() {
_inputBlocked = false;
_coolDownSeconds *= 2;
_errorText = null;
});
});
_textEditingController.clear();
}
2021-05-01 09:43:54 +00:00
@override
Widget build(BuildContext context) {
2024-01-24 08:01:11 +00:00
return Scaffold(
appBar: AppBar(
title: Text(L10n.of(context)!.pleaseEnterYourPin),
centerTitle: true,
),
extendBodyBehindAppBar: true,
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: ConstrainedBox(
constraints: const BoxConstraints(
maxWidth: FluffyThemes.columnWidth,
2021-05-01 09:43:54 +00:00
),
2024-01-24 08:01:11 +00:00
child: ListView(
shrinkWrap: true,
children: [
Center(
child: Image.asset(
'assets/info-logo.png',
width: 256,
2023-09-23 05:49:34 +00:00
),
2024-01-24 08:01:11 +00:00
),
TextField(
controller: _textEditingController,
textInputAction: TextInputAction.done,
keyboardType: TextInputType.number,
obscureText: true,
autofocus: true,
textAlign: TextAlign.center,
readOnly: _inputBlocked,
2024-06-07 10:23:09 +00:00
onChanged: tryUnlock,
onSubmitted: tryUnlock,
2024-01-24 08:01:11 +00:00
style: const TextStyle(fontSize: 40),
2024-06-07 10:23:09 +00:00
inputFormatters: [
LengthLimitingTextInputFormatter(4),
],
2024-01-24 08:01:11 +00:00
decoration: InputDecoration(
errorText: _errorText,
hintText: '****',
suffix: IconButton(
icon: const Icon(Icons.lock_open_outlined),
onPressed: () => tryUnlock(_textEditingController.text),
),
2023-09-23 05:49:34 +00:00
),
),
2024-01-24 08:01:11 +00:00
if (_inputBlocked)
const Padding(
padding: EdgeInsets.all(8.0),
child: LinearProgressIndicator(),
),
],
2022-04-15 09:42:59 +00:00
),
2021-05-01 09:43:54 +00:00
),
),
),
);
}
}