Executing a method in a dart TextField on every key press - dart

I have a TextField in my project, which will be used to act as an autocomplete text field. I need it to respond to key presses.
After researching on Flutter's documentation, https://flutter.io/docs/cookbook/forms/text-field-changes, I found two ways of allowing a TextField to execute a method every time the TextField's text changed, however it only executes that method when you hit and enter and leave the focus of the TextField
I have used both the controller method, and the onChanged method and neither work.
Is there a way of having a TextField respond to every key press, as you type in a word?

OnChanged should work fine. If you are trying to change something on the screen you will have to make a call to setState() and your functionality with in that call.

Related

Is there a pre-defined function in iOS that is activated upon the user typing a symbol?

There are classes with pre-defined functions such as viewDidLoad or touchesBegan:. Is there one I could use for when keys on the software keyboard are pressed?
This question is not specific to either Swift or Xcode. It's an iOS question.
The answer is "sort of".
There is not a special function that gets called for a key press.
Instead, if the keyboard is attached to a UITextField, you can set up an object to be the text field's delegate, and then the delegate's textField:shouldChangeCharactersInRange:replacementString: method will be called as the user enters text. (At each key press, or on a paste). There is a similar method textView:shouldChangeTextInRange:replacementText: if the input field is a UITextView.

Distinguishing between EditingDidBegin because of user action vs programatic setting

When I set the text of a UITextField programmatically, the EditingChanged action is not sent to my view controller, the action is only sent when the user changes the text through the keyboard.
However, when I call becomeFirstResponder() on a text field, it does send the EditingDidBegin action. So I can't tell if editing began because the user tapped on the UITextField, or because I did it programmatically. Do you know how I can easily distinguish between the two?
I'm looking for a way to know when the user taps in a UITextField and only when the user taps on it, not when becomeFirstResponder is called on it. Do you know of one?
when I call becomeFirstResponder()
The secret is that when you call becomeFirstResponder(), you call it. Your code is running. You are in control. You can do whatever you want.
So if you need your EditingChanged handler to know that you called becomeFirstResponder(), you simply have to make a notation of this fact beforehand. For example, you could have a Bool property justCalledBecomeFirstResponder which you set to true just before calling becomeFirstResponder(). Now your EditingChanged handler has only to examine (and possibly reset to false) this Bool property before proceeding.

ios/xcode/objective c: Capture last keystroke in textfield

I have a method to enable the save button that I want to fire when the user meets several conditions, for example, username is at least 4 characters. To test these conditions I need to check with every keystroke. What is best touch event (through storyboard) to capture this?
Have tried "Editing did end" however, that doesn't fire until you click out of textfield. I'd rather see button enabled as soon as textfield has acceptable entry. Also tried on touchup inside and value changed but neither seem to work.
What is best touch event so that as soon as user types fourth character, button is enabled?
Here is action method that textfield is wired to. Note it does not say anything about touch event which is present in storyboard.
- (IBAction)changedText:(id)sender {
[self updateSaveButton];
-(void) updateSaveButton
{
NSString *name = self.username.text;
NSLog(#"update save button called");
self.submitButton.enabled = (name.length > 3);
}
What I do is to assign the text field a delegate. In this case, the delegate would whatever is the instance whose class's code you have shown in your question.
The delegate is sent a bunch of delegate messages, documented in UITextFieldDelegate. In this case, the one I would use is textField:shouldChangeCharactersInRange:replacementString:, which fires every time the user does anything that proposed to change the text in the field.
Simply respond as you see fit, and return YES to allow the change to take place.
Alternatively, you can treat the text field as a control (UIControl) and use the text field's Editing Changed control event to set up a target-action pair. But in actual practice I always end up using the delegate method instead.

resignFirstResponder vs. endEditing for Keyboard Dismissal

In Swift, both [someTextField].resignFirstResponder() and self.view.endEditing(true) accomplish the same task - hiding the keyboard from the user's view and de-focusing whatever text field was using it. I understand that the former is specific to a particular field, while the latter encompasses the entire view, but other than wanting to target a specific text field, when is one preferred/recommended over the other?
someTextField.resignFirstResponder()
resignFirstResponder() is good to use any time you know exactly which text field is the first responder and you want to resign its first responder status. This can be slightly more efficient then the alternative, but if you're doing something such as creating a custom control, this can make plenty of sense. Perhaps you have a text field, and when the "Next" button is pressed, you want to get rid of the keyboard and present a date picker, for example. Here, I would definitely use resignFirstResponder()
self.view.endEditing(true)
I typically reserve this for scenarios when I just absolutely need to clear the keyboard no matter what is currently going on, for whatever reason. Perhaps, I've got a slide-over menu? Just before this slides out, no matter what is going on, the keyboard should go away, so I'll make sure everything resigns its first responder status. It's important to note that endEditing() will look through the entire hierarchy of subviews and make sure whatever is the first responder resigns its status. This makes it less efficient then calling resignFirstResponder() if you already have a concrete reference to the first responder, but if not, it's easier than finding that view and having it resign.
There is no such strict rule.
You use resignFirstResponder when you have the reference of the text field currently holding first responder status. When you don't have the reference or you are unsure about that, endEditing would do the trick.
One thing however should be noted that endEditing has a boolean parameter that we occasionally set to true. By setting this parameter to true the view, on which endEditing has been called, will force every child text field to resign first responder status irrespective of it has returned a false value from textFieldShouldEndEditing delegate method. On the contrary calling endEditing with false would only ask (not force) the text field to resign, respecting the return value from textFieldShouldEndEditing protocol method.

iOS Text field delegate if the value of a text field is programatically

I have tried many an attempts to make this work. After much googling, trial & error, I post this here.
I have a text field whose value is changed when a slider near by is moved.
So in the IBAction sliderChanged, I have this
self.amountTextField.text = self.amountSlider.value
Now, I would like to be notified of this change in the text field value. I have tried with quite a few delegates. It so happens that when the text field's value is changed by tapping on the text field and changing it using the keyboard, these delegates are fired. But when the value is changed using the above piece of code, the delegates are not invoked.
I do like to know if theres a possible solution for this, i.e, i would like a delegate to handle the change in the text field value even if its done from code. Would love to have a working example. Thanks in advance.

Resources