Skip to content

Commit 2c74590

Browse files
authored
Add password field with toggle (from FatumaA)
1 parent 5abeab0 commit 2c74590

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import 'package:flutter/material.dart';
2+
3+
/// Internal password field component with visibility toggle
4+
class SupaPasswordField extends StatefulWidget {
5+
/// Controller for the password field
6+
final TextEditingController controller;
7+
8+
/// Validator function for the password field
9+
final String? Function(String?)? validator;
10+
11+
/// Label text for the password field
12+
final String labelText;
13+
14+
/// Prefix icon for the password field
15+
final Widget? prefixIcon;
16+
17+
/// Autofill hints for the password field
18+
final Iterable<String>? autofillHints;
19+
20+
/// Text input action for the password field
21+
final TextInputAction? textInputAction;
22+
23+
/// Callback when field is submitted
24+
final void Function(String)? onFieldSubmitted;
25+
26+
/// Whether the field should auto-validate
27+
final AutovalidateMode? autovalidateMode;
28+
29+
const SupaPasswordField({
30+
super.key,
31+
required this.controller,
32+
this.validator,
33+
required this.labelText,
34+
this.prefixIcon,
35+
this.autofillHints,
36+
this.textInputAction,
37+
this.onFieldSubmitted,
38+
this.autovalidateMode,
39+
});
40+
41+
@override
42+
State<SupaPasswordField> createState() => _SupaPasswordFieldState();
43+
}
44+
45+
class _SupaPasswordFieldState extends State<SupaPasswordField> {
46+
bool _obscureText = true;
47+
48+
@override
49+
Widget build(BuildContext context) {
50+
return TextFormField(
51+
controller: widget.controller,
52+
validator: widget.validator,
53+
obscureText: _obscureText,
54+
autofillHints: widget.autofillHints,
55+
textInputAction: widget.textInputAction,
56+
onFieldSubmitted: widget.onFieldSubmitted,
57+
autovalidateMode: widget.autovalidateMode,
58+
decoration: InputDecoration(
59+
prefixIcon: widget.prefixIcon,
60+
label: Text(widget.labelText),
61+
suffixIcon: Tooltip(
62+
message: _obscureText ? 'Show password' : 'Hide password',
63+
child: IconButton(
64+
icon: Icon(
65+
_obscureText ? Icons.visibility_off : Icons.visibility,
66+
),
67+
onPressed: () {
68+
setState(() {
69+
_obscureText = !_obscureText;
70+
});
71+
},
72+
),
73+
),
74+
),
75+
);
76+
}
77+
}

0 commit comments

Comments
 (0)