Skip to content

Commit

Permalink
Fix according to review
Browse files Browse the repository at this point in the history
  • Loading branch information
WinXaito committed Mar 29, 2023
1 parent 6a8c71d commit de98ce3
Showing 1 changed file with 39 additions and 34 deletions.
73 changes: 39 additions & 34 deletions lib/src/controls/form/password_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class PasswordBox extends StatefulWidget {
/// {@macro flutter.widgets.editableText.obscuringCharacter}
final String obscuringCharacter;

/// Creates a password box
const PasswordBox({
super.key,
this.controller,
Expand Down Expand Up @@ -150,11 +151,11 @@ class _PasswordBoxState extends State<PasswordBox> {
bool focusCanPeek = true;
bool textCanPeek = false;

late final TextEditingController controller =
widget.controller ?? TextEditingController();
TextEditingController? _internalController;
TextEditingController? get controller =>
widget.controller ?? _internalController;

FocusNode? _internalNode;

FocusNode? get focusNode => widget.focusNode ?? _internalNode;

bool get _isVisible =>
Expand All @@ -172,17 +173,24 @@ class _PasswordBoxState extends State<PasswordBox> {
@override
void initState() {
if (widget.focusNode == null) {
_internalNode ??= _createFocusNode();
_internalNode ??= FocusNode(debugLabel: '${widget.runtimeType}');
}
if (widget.controller == null) {
_internalController = TextEditingController();
}
controller.addListener(_handleTextChange);
controller!.addListener(_handleTextChange);
focusNode!.addListener(_handleFocusChange);
super.initState();
}

@override
void dispose() {
controller.dispose();
focusNode!.dispose();
if (widget.controller == null) {
controller!.dispose();
}
if (widget.focusNode != null) {
focusNode!.dispose();
}
super.dispose();
}

Expand All @@ -198,19 +206,19 @@ class _PasswordBoxState extends State<PasswordBox> {
}

void _handleTextChange() {
if (controller.text.isEmpty) {
if (controller!.text.isEmpty) {
// If the text box is empty, then we ignore if the focus has been
// lost or not previously.
focusCanPeek = true;
}

if (controller.text.isNotEmpty && !textCanPeek) {
if (controller!.text.isNotEmpty && !textCanPeek) {
// If the text box is not empty, the reveal button must be visible
// (it will be only if focusCanPeek is true !)
setState(() {
textCanPeek = true;
});
} else if (controller.text.isEmpty && textCanPeek) {
} else if (controller!.text.isEmpty && textCanPeek) {
// If the text box is empty, the reveal button must be hidden.
setState(() {
textCanPeek = false;
Expand All @@ -219,7 +227,7 @@ class _PasswordBoxState extends State<PasswordBox> {
}

void _handleFocusChange() {
if (!focusNode!.hasFocus && controller.text.isNotEmpty) {
if (!focusNode!.hasFocus && controller!.text.isNotEmpty) {
// If the focus is lost and the text box is not empty, then the reveal
// button must not be hidden.
setState(() {
Expand All @@ -228,11 +236,6 @@ class _PasswordBoxState extends State<PasswordBox> {
}
}

// Only used if needed to create _internalNode.
FocusNode _createFocusNode() {
return FocusNode(debugLabel: '${widget.runtimeType}');
}

@override
Widget build(BuildContext context) {
return TextBox(
Expand All @@ -243,24 +246,26 @@ class _PasswordBoxState extends State<PasswordBox> {
placeholderStyle: widget.placeholderStyle,
obscureText: !_isVisible,
suffix: _canPeek
? IconButton(
icon: const Icon(FluentIcons.red_eye),
// todo: half eye icon, like WinUI3 ?
onPressed: null,
onTapDown: widget.enabled
? () {
setState(() {
peek = true;
});
}
: null,
onTapUp: widget.enabled
? () {
setState(() {
peek = false;
});
}
: null,
? SmallIconButton(
child: IconButton(
icon: const Icon(FluentIcons.red_eye),
// todo: half eye icon, like WinUI3 ?
onPressed: null,
onTapDown: widget.enabled
? () {
setState(() {
peek = true;
});
}
: null,
onTapUp: widget.enabled
? () {
setState(() {
peek = false;
});
}
: null,
),
)
: null,
onEditingComplete: widget.onEditingComplete,
Expand Down

0 comments on commit de98ce3

Please sign in to comment.