I've used SimpleAutoCompleteTextField in my flutter project but am facing a problem of not being suggestions the right suggestions unless I started to type the beginning of the word not from the middle of it .. example:
When I am looking for the word "Astalavista", if I type "asta" it will be suggested but if I typed "lavis" it won't be suggested, I need to fix this out.
Here is my code :
child: SimpleAutoCompleteTextField(
key: endKey,
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
hintText: S.of(context).end_location_hint),
controller: endLocationTextEditingController,
suggestions: suggestions,
textChanged: (text) => currentText = text,
clearOnSubmit: true,
textSubmitted: (text) async {
await movingCameraToLocation(
double.parse(
allStations[suggestions.indexOf(text)]
.stationLatitude),
double.parse(
allStations[suggestions.indexOf(text)]
.stationLongitude));
toLocationName = text;
setState(() {});
},
),
Try adding the filter parameter, im not sure if it can be used with SimpleAutoCompleteTextField, but the official documentation states that
"itemFilter" parameter can be used with AutoCompleteTextField <String>(),
Your filter would be :
itemFilter: (suggestion, input) =>
suggestion.toLowerCase().contains(input.toLowerCase()),```
Related
What is the difference between TextField and TextFormField in flutter? Both look the same. Which one should i use? Which one do you recommend? I use TextField like this:
const TextField(
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Password',
),
)
A TextFormField mainly has the validator argument, while TextField doesn't: it must return null if the input is valid, and it must return a String if there's some error (usually the String itself contains information about the error). This argument allows you to validate the user input in a very easy way, provided that you include the form fields (there are others, in addition to TextFormField) in a Form widget and that you apply a Key to the Form.
If you do everything correctly, you could just invoke formKey.currentState!.validate() and automatically Flutter will invoke the validator argument for each form field that you added to the Form. If everything checks, the validate will return true, and you can proceed with your program logic. Otherwise, it will return false and it will show the String returned by the validator near the form field that contained incorrect data.
This example is taken from the Flutter cookbook on forms:
[...]
final _formKey = GlobalKey<FormState>();
#override
Widget build(BuildContext context) {
// Build a Form widget using the _formKey created above.
return Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextFormField(
// The validator receives the text that the user has entered.
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed: () {
// Validate returns true if the form is valid, or false otherwise.
if (_formKey.currentState!.validate()) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Processing Data')),
);
}
},
child: const Text('Submit'),
),
),
],
),
);
}
}
You use TextFormField when using Form widget. Combined with Form widget you can make more complex forms + validation for whole form.
TextField is basically a TextFormField but you don't have to include it into the Form widget and validation would work a bit differently.
I just want to make my TextField as required field, in which I am using Email and password to login for the user. Please let me know how can I make it required and if user don't fill it, how can I give him warning.
TextField (
onChanged: (value) {
email=value;
},
style: const TextStyle(color: Colors.black),
decoration: InputDecoration(
fillColor: Colors.grey.shade100,
filled: true,
hintText: "Email",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
)
),
),
const SizedBox(
height: 30,
),
TextField(
onChanged: (value) {
password=value;
},
style: const TextStyle(),
obscureText: true,
decoration: InputDecoration(
fillColor: Colors.grey.shade100,
filled: true,
hintText: "Password",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
)
),
),
The esiest way to set a validation logic for the TextField in Flutter is to use TextFormField instead of TextField in combination with Form widget.
It provides you with a callback called validator which is called whenever you call .validate() method in the Form Key.
To learn more about using Form widget in Flutter along with TextFormFiled and validation, check out this video.
Example for a condition in the validator to make the field required:
validator: (String? value) {
if (value == null)
{
return 'This field is required';
}
return null;
},
NOTE:
If the validator callback returned a message, this means the message would be displayed in the errorText for the TextFormField and the .validate() method would return false.
If the validator callback returned null, this means that no errors and the .validate() method would return true.
if user click on submit button then you can check for is email or password field is empty or not empty.
I havea bug where my code cant be build because this errorr
can someone look my code
here the code
Text(
"Login",
style: Theme.of(context).textTheme.headline2,
),
SizedBox(height: 20,),
new TextFormField(
keyboardType: TextInputType.emailAddress,
validator: (input) => !input.contains("#")
? "Email id Should be Valid"
: null,
)
],
),
),
),
it show error message "The method 'contains' can't be unconditionally invoked because the receiver can be 'null' try making the call conditional (using '?.' or adding a null check to target
can someone please fix my bug
thanks before
You need to use:
TextFormField(
keyboardType: TextInputType.emailAddress,
validator: (input) => !(input?.contains("#") ?? false)
? "Email id Should be Valid"
: null,
)
As per the official documentation FormFieldValidator, the validator parameter is an optional String. So you need to convert that to non-nullable String first.
How to can I implement a callback that fires after every few seconds or when user stop typing in TextField ?
Or is it performant to just implement in onChanged callback directly ?
input Field onChanged gives the input value when ever user types in, So you may use onChnaged callback function to save the input, like below,
TextFormField(
controller: _nameController,
onChanged: (value) {
saveData();
},
initialValue: widget.user.userName,
onSaved: (val) {
widget.user.userName = val;
},
validator: (val) =>
val.length > 3 ? null : 'Full name is invalid',
decoration: InputDecoration(
labelText: 'Full Name',
hintText: 'Enter your full name',
icon: Icon(Icons.person),
isDense: true,
),
),
I am working with Flutter and am struggling to remove a Dismissible object from the tree. Below is my code. I have created a custom class that is stored in the list 'newlist.' I seemingly remove the Dismissible object from the List and setState(), but it does not seem to work. Any help is greatly appreciated.
return new Dismissible(key: new Key("newlist"),
direction: DismissDirection.horizontal,
onDismissed: (DismissDirection direction) {
setState(() {
newlist.remove(newlist[index]);
print(newlist.length);
});
},
child: new ListTile(
leading: const
Icon(Icons.album),
title: new Text(newlist[index].amount),
subtitle: new Text(
newlist[index].name)));
})),
I have solved it using items name + lists length as a key. Because there could be some items with the same value
return Dismissible(
key: Key(item.name + _paths.length.toString()),
onDismissed: (direction) {
setState(() {
_paths.removeAt(index);
});
// Show a red background as the item is swiped away
background: Container(color: Colors.red),
child: Container(child: new Texts().tallText(item.name)),
);
I solved it. Essentially, I was using the same Key for every Dismissable. This makes Flutter think that the object I dismissed is still there. Hope this helps someone.
Yes, It because of Key only.
key: new Key("newlist") - wrong
it should be:
key: Key(newlist[index])