71 lines
1.9 KiB
Dart
71 lines
1.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
class MyPassField extends StatefulWidget {
|
|
final controller;
|
|
final String hintText;
|
|
|
|
const MyPassField({
|
|
super.key,
|
|
required this.controller,
|
|
required this.hintText,
|
|
});
|
|
|
|
@override
|
|
State<MyPassField> createState() => _MyPassFieldState();
|
|
}
|
|
|
|
class _MyPassFieldState extends State<MyPassField> {
|
|
final textFieldFocusNode = FocusNode();
|
|
bool _obscured = true;
|
|
|
|
void _toggleObscured() {
|
|
setState(() {
|
|
_obscured = !_obscured;
|
|
if (textFieldFocusNode.hasPrimaryFocus) {
|
|
return; // If focus is on text field, dont unfocus
|
|
}
|
|
textFieldFocusNode.canRequestFocus =
|
|
false; // Prevents focus if tap on eye
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 25.0),
|
|
child: TextField(
|
|
controller: widget.controller,
|
|
obscureText: _obscured,
|
|
decoration: InputDecoration(
|
|
fillColor: Colors.white,
|
|
filled: true,
|
|
hintText: widget.hintText,
|
|
hintStyle: TextStyle(color: Colors.blueGrey[400]),
|
|
enabledBorder: const OutlineInputBorder(
|
|
borderSide: BorderSide(
|
|
color: Colors.blueAccent,
|
|
width: 2.0,
|
|
),
|
|
),
|
|
focusedBorder: const OutlineInputBorder(
|
|
borderSide: BorderSide(color: Colors.blue),
|
|
),
|
|
suffixIcon: Padding(
|
|
padding: const EdgeInsets.fromLTRB(0, 0, 4, 0),
|
|
child: GestureDetector(
|
|
onTap: _toggleObscured,
|
|
child: Icon(
|
|
_obscured
|
|
? Icons.visibility_rounded
|
|
: Icons.visibility_off_rounded,
|
|
size: 24,
|
|
color: Colors.blue,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|