import 'package:flutter/material.dart'; /// Password field with show/hide toggle class PasswordField extends StatefulWidget { const PasswordField({ super.key, required this.controller, required this.label, this.hint, this.validator, this.textInputAction, this.onFieldSubmitted, this.enabled = true, this.autofocus = false, }); final TextEditingController controller; final String label; final String? hint; final String? Function(String?)? validator; final TextInputAction? textInputAction; final void Function(String)? onFieldSubmitted; final bool enabled; final bool autofocus; @override State createState() => _PasswordFieldState(); } class _PasswordFieldState extends State { bool _obscureText = true; void _toggleVisibility() { setState(() { _obscureText = !_obscureText; }); } @override Widget build(BuildContext context) { final theme = Theme.of(context); return TextFormField( controller: widget.controller, validator: widget.validator, obscureText: _obscureText, textInputAction: widget.textInputAction, onFieldSubmitted: widget.onFieldSubmitted, enabled: widget.enabled, autofocus: widget.autofocus, style: theme.textTheme.bodyLarge, decoration: InputDecoration( labelText: widget.label, hintText: widget.hint, prefixIcon: Icon( Icons.lock_outline, color: theme.colorScheme.primary, ), suffixIcon: IconButton( icon: Icon( _obscureText ? Icons.visibility : Icons.visibility_off, color: theme.colorScheme.onSurface.withOpacity(0.6), ), onPressed: _toggleVisibility, ), labelStyle: theme.textTheme.bodyMedium?.copyWith( color: theme.colorScheme.onSurface.withOpacity(0.7), ), hintStyle: theme.textTheme.bodyMedium?.copyWith( color: theme.colorScheme.onSurface.withOpacity(0.4), ), errorStyle: theme.textTheme.bodySmall?.copyWith( color: theme.colorScheme.error, ), ), ); } }