Skip to content
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

Currency input formatter empty value error #87

Closed
Sancene opened this issue Aug 19, 2022 · 12 comments
Closed

Currency input formatter empty value error #87

Sancene opened this issue Aug 19, 2022 · 12 comments

Comments

@Sancene
Copy link

Sancene commented Aug 19, 2022

when i erase my currency value it becomes 0 and then if i type new numbers to that input field it doesn't erase the zero and leaves it at the start of the text

@caseyryan
Copy link
Owner

can you provide some code snippet to reproduce this?

@caseyryan
Copy link
Owner

Screen.Recording.2022-08-20.at.18.55.18.mov

I've just checked it, and it works just fine

@caseyryan
Copy link
Owner

Oh, I found one way to reproduce it. Is when you specify a 0 mantissa length. Ok, thanks for the report. I'll fix this

@caseyryan
Copy link
Owner

Fixed in 2.6.1. I also found 2 related bugs and fixed them as well

@Sancene
Copy link
Author

Sancene commented Aug 22, 2022

Thanks for the fix but there is another bug related to mantissa length being 0: if your cursor is set on the trailing symbol you are not able to delete but are able to type.

@caseyryan
Copy link
Owner

It's not a bug, it's planned this way

@Sancene
Copy link
Author

Sancene commented Aug 22, 2022

but if i set mantissa to 1 then i am able to delete input value. It displaces cursor to value first, bypassing the trailing symbol but i am still able to delete after that.

@Sancene
Copy link
Author

Sancene commented Aug 22, 2022

video_2022-08-22_11-10-21.mp4

@asadamatic
Copy link

asadamatic commented Nov 11, 2024

@caseyryan I'm facing the issue that's mentioned in very first comment. I understand that you had fixed it, but I'm encountering it. When I set the mantis to 0, then pressing the clear button erases the first number as well.

Also, when setting the mantis greater than 0, the cursor is at the end of the decimal zeros, and the user has to clear up to the decimal point to start entering a value.

@asadamatic
Copy link

second.mp4
first.mp4

@caseyryan
Copy link
Owner

caseyryan commented Nov 11, 2024

When I set the mantis to 0, then pressing the clear button erases the first number as well.

Sorry, I don't understand what you mean here. Shouldn't it clear the first digit? Why?
If you mean the caret index that goes in front of the only zero character then yea, it's a bug. But the problem is that Flutter team always change the behavior of the input in different versions of Flutter and I just can't physically keep pace with them

Also, when setting the mantis greater than 0, the cursor is at the end of the decimal zeros, and the user has to clear up to the decimal point to start entering a value.

This is the correct behavior of the textfield. Of course it can be improved but the question is where should the caret go in this case? Before the decimal separator or after it? I believe the better solution is to give every developer an option to keep track of the focus themselves and select the behavior they want. In your case it might be reasonable to develop some kind of a wrapper around the text input and when it gathers focus check what text is entered there, for example, if the text ends with 0.00 put the caret right before the period (or right after, whatever you need).

Something like this (I wrote the code right here so there may be some typos or sth, but you can get the idea):

TextInputField(
   controller: _textEditingController,
   focusNode: _focusNode,
    onFocusChange: () {
         if (_focusNode.hasFocus) {
            _textEditingController.setSelectionToComfortNumber(
              _focusNode,
            );
         }
    }
),

and here how you can extend TextEditingController

extension TextEditinControllerExtension on TextEditingController {
   Future setSelectionToComfortNumber([
    FocusNode? focusNode,
    int delayMillis = 100,
  ]) async {
    await Future.delayed(Duration(milliseconds: delayMillis));
    final indexOfPeriod = text.lastIndexOf('.');
    if (indexOfPeriod > -1) {
      selection = TextSelection.collapsed(
        offset: indexOfPeriod,
      );
    }

    focusNode?.requestFocus();
  }
}

@asadamatic
Copy link

Thanks, I'll try it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants