generatePasswordField static method
String
generatePasswordField(
)
Implementation
static String generatePasswordField() {
return '''
import 'package:flutter/material.dart';
class PasswordField extends StatefulWidget {
final String label;
final String? hint;
final TextEditingController? controller;
final String? Function(String?)? validator;
final bool showStrengthIndicator;
final Function(String)? onChanged;
const PasswordField({
Key? key,
this.label = 'Password',
this.hint,
this.controller,
this.validator,
this.showStrengthIndicator = false,
this.onChanged,
}) : super(key: key);
@override
State<PasswordField> createState() => _PasswordFieldState();
}
class _PasswordFieldState extends State<PasswordField> {
bool _obscureText = true;
double _strength = 0.0;
String _strengthText = '';
Color _strengthColor = Colors.red;
void _calculateStrength(String password) {
if (!widget.showStrengthIndicator) return;
double strength = 0.0;
if (password.isEmpty) {
setState(() {
_strength = 0.0;
_strengthText = '';
});
return;
}
// Length check
if (password.length >= 8) strength += 0.25;
if (password.length >= 12) strength += 0.25;
// Contains lowercase
if (password.contains(RegExp(r'[a-z]'))) strength += 0.15;
// Contains uppercase
if (password.contains(RegExp(r'[A-Z]'))) strength += 0.15;
// Contains numbers
if (password.contains(RegExp(r'[0-9]'))) strength += 0.1;
// Contains special characters
if (password.contains(RegExp(r'[!@#\$%^&*(),.?":{}|<>]'))) strength += 0.1;
String text;
Color color;
if (strength < 0.3) {
text = 'Weak';
color = Colors.red;
} else if (strength < 0.6) {
text = 'Fair';
color = Colors.orange;
} else if (strength < 0.8) {
text = 'Good';
color = Colors.blue;
} else {
text = 'Strong';
color = Colors.green;
}
setState(() {
_strength = strength;
_strengthText = text;
_strengthColor = color;
});
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextFormField(
controller: widget.controller,
validator: widget.validator,
obscureText: _obscureText,
onChanged: (value) {
_calculateStrength(value);
widget.onChanged?.call(value);
},
style: const TextStyle(fontSize: 16),
decoration: InputDecoration(
labelText: widget.label,
hintText: widget.hint ?? 'Enter your password',
prefixIcon: const Icon(Icons.lock_outline, size: 22),
suffixIcon: IconButton(
icon: Icon(
_obscureText ? Icons.visibility_outlined : Icons.visibility_off_outlined,
size: 22,
),
onPressed: () {
setState(() {
_obscureText = !_obscureText;
});
},
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide(color: theme.colorScheme.outline),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide(
color: theme.colorScheme.outline.withOpacity(0.5),
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide(
color: theme.colorScheme.primary,
width: 2,
),
),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide(color: theme.colorScheme.error),
),
focusedErrorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide(
color: theme.colorScheme.error,
width: 2,
),
),
filled: true,
fillColor: theme.colorScheme.surfaceVariant.withOpacity(0.3),
contentPadding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 18,
),
),
),
if (widget.showStrengthIndicator && _strength > 0) ...[
const SizedBox(height: 8),
Row(
children: [
Expanded(
child: ClipRRect(
borderRadius: BorderRadius.circular(4),
child: LinearProgressIndicator(
value: _strength,
backgroundColor: theme.colorScheme.surfaceVariant,
valueColor: AlwaysStoppedAnimation<Color>(_strengthColor),
minHeight: 6,
),
),
),
const SizedBox(width: 12),
Text(
_strengthText,
style: TextStyle(
color: _strengthColor,
fontSize: 12,
fontWeight: FontWeight.w600,
),
),
],
),
],
],
);
}
}
''';
}