Skip to content

Add showErrorWhenFocused and fix wrong renderbox size #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/src/pinput.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class Pinput extends StatefulWidget {
this.onAppPrivateCommand,
this.mouseCursor,
this.forceErrorState = false,
this.showErrorWhenFocused = false,
this.errorText,
this.validator,
this.errorBuilder,
Expand Down Expand Up @@ -159,6 +160,7 @@ class Pinput extends StatefulWidget {
this.onAppPrivateCommand,
this.mouseCursor,
this.forceErrorState = false,
this.showErrorWhenFocused = false,
this.validator,
this.pinputAutovalidateMode = PinputAutovalidateMode.onSubmit,
this.scrollPadding = const EdgeInsets.all(20),
Expand Down Expand Up @@ -390,6 +392,9 @@ class Pinput extends StatefulWidget {
/// If true [errorPinTheme] will be applied and [errorText] will be displayed under the Pinput
final bool forceErrorState;

/// If true, the error will also be displayed in the focused state. Otherwise the error is not displayed in the focused state.
final bool showErrorWhenFocused;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you use the forceErrorState instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default behavior of a normal text field is that the error is displayed even when the user focuses on the field.
In theory, I could also use forceErrorState, but then I would need to handle focus checks manually.
From my perspective, this approach is much easier.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


/// Text displayed under the Pinput if Pinput is invalid
final String? errorText;

Expand Down
12 changes: 9 additions & 3 deletions lib/src/pinput_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class _PinputState extends State<Pinput>
enabled: isEnabled,
validator: _validator,
initialValue: _effectiveController.text,
builder: (FormFieldState<String> field) {
builder: (field) {
return MouseRegion(
cursor: _effectiveMouseCursor,
onEnter: (PointerEnterEvent event) => _handleHover(true),
Expand All @@ -354,7 +354,10 @@ class _PinputState extends State<Pinput>
child: Stack(
alignment: Alignment.topCenter,
children: [
_buildEditable(textSelectionControls, field),
// the editable need to be the full size, otherwise the focus is not correct when getting the renderbox from the focus
Positioned.fill(
child: _buildEditable(textSelectionControls, field),
),
_buildFields(),
],
),
Expand Down Expand Up @@ -543,7 +546,10 @@ class _PinputState extends State<Pinput>
}

@protected
bool get showErrorState => hasError && (!hasFocus || widget.forceErrorState);
bool get showErrorState {
return hasError &&
((!hasFocus || (hasFocus && widget.showErrorWhenFocused)) || widget.forceErrorState);
}

Widget _buildError() {
if (showErrorState) {
Expand Down