showSecurityKeyDialog function
- BuildContext context
Shows a dialog to prompt user for security key. Returns true if key was successfully entered and verified.
Implementation
Future<bool> showSecurityKeyDialog(BuildContext context) async {
final keyController = TextEditingController();
bool result = false;
await showDialog(
context: context,
barrierDismissible: false,
builder: (ctx) {
bool isLoading = false;
bool obscureText = true;
String? errorText;
return StatefulBuilder(
builder: (context, setState) => AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
title: const Row(
children: [
Icon(Icons.key, color: Colors.blue),
SizedBox(width: 8),
Expanded(
child: Text(
'Enter Security Key',
style: TextStyle(fontSize: 18),
),
),
],
),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Please enter your security key to access encrypted data.',
style: TextStyle(fontSize: 13),
),
const SizedBox(height: 16),
TextField(
controller: keyController,
obscureText: obscureText,
decoration: InputDecoration(
labelText: 'Security Key',
border: const OutlineInputBorder(),
errorText: errorText,
suffixIcon: IconButton(
icon: Icon(
obscureText ? Icons.visibility_off : Icons.visibility,
),
onPressed: () => setState(() => obscureText = !obscureText),
),
),
enabled: !isLoading,
onSubmitted: (_) async {
if (keyController.text.isNotEmpty && !isLoading) {
await _verifyAndSetKey(
keyController.text,
setState,
ctx,
() => result = true,
(msg, loading) {
errorText = msg;
isLoading = loading;
},
);
}
},
),
],
),
actions: [
TextButton(
onPressed: isLoading ? null : () => Navigator.pop(ctx),
child: const Text('Cancel'),
),
ElevatedButton(
onPressed: isLoading
? null
: () async {
if (keyController.text.isEmpty) {
setState(() => errorText = 'Please enter a key');
return;
}
await _verifyAndSetKey(
keyController.text,
setState,
ctx,
() => result = true,
(msg, loading) {
errorText = msg;
isLoading = loading;
},
);
},
child: isLoading
? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('Confirm'),
),
],
),
);
},
);
keyController.dispose();
return result;
}