I'm using the intl_phone_field package to verify phone numbers based on country code.
The IntlPhoneField checks automatically for invalid phone numbers, and prompt a message respectively.
It support onChange, onSubmit and onSaved functions, but does not support an onValid function.
I want to enable/disable a Submit Button, based on package's validate function, Since it already supports many country codes and different phone number lengths.
Widget build(BuildContext context) {
return IntlPhoneField(
autofocus: true,
decoration: const InputDecoration(
labelText: 'Phone Number',
border: OutlineInputBorder(),
counterText: '',
),
initialCountryCode: 'US',
// countries: const ['US'],
);
}
How do I achieve that?
Solved!
I detected validation using the intl_phone_field package by exporting the Country object, which contains the minLength and maxLength of the country code phone number.
Using those parameters I verified the phone number in the onChange function and a function in case of verified phone number.
class PhonePicker extends StatelessWidget {
const PhonePicker({super.key});
#override
Widget build(BuildContext context) {
const _initialCountryCode = 'US';
var _country =
countries.firstWhere((element) => element.code == _initialCountryCode);
return IntlPhoneField(
autofocus: true,
decoration: const InputDecoration(
labelText: 'Phone Number',
border: OutlineInputBorder(),
counterText: '',
),
initialCountryCode: _initialCountryCode,
onChanged: (value) {
if (value.number.length >= _country.minLength &&
value.number.length <= _country.maxLength) {
// Run anything here
}
},
onCountryChanged: (country) => _country = country,
);
}
}
I have opened a pull request with a solution in the GitHub project.
You can a state variable and use validator data to check whether number is valid or not
bool isValid = false;
#override
Widget build(BuildContext context) {
return Column(
children: [
IntlPhoneField(
autofocus: true,
validator: (p0) {
/// logic to validate number
/// isValid =true
},
decoration: const InputDecoration(
labelText: 'Phone Number',
border: OutlineInputBorder(),
counterText: '',
),
initialCountryCode: 'US',
// countries: const ['US'],
),
ElevatedButton(onPressed: isValid ? () {} : null, child: child)
],
);
Also you can use TextEditingController with listener and check data and enable button state. make sure to use setState.
Related
I want to write something like a function that generates TextFormFields with different names, but I don't want the labelText attribute to be const. That way I can easily rubber stamp out a bunch of similar fields that have different names.
For example
TextFormField myFormField(myMapType myMap, String fieldName) {
return TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: fieldName,
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your $fieldName';
}
return null;
},
initialValue: myMap.attribute[fieldName],
onSaved: (val) {
setState(
() {
myMap.setAttribute(fieldName, val!);
},
);
});
}
But that gives an error "Invalid constant value" at the line "labelText: fieldName". What's the trick needed to accomplish what I'm trying to do? Or what dumb mistake did I make?
fieldName will get on runtime rather than compile time. Therefore you cant use const
You can do
return TextFormField(
decoration: InputDecoration(
border: const OutlineInputBorder(), //this can be const
labelText: fieldName,
),
validator: (value) {
You can check What is the difference between the "const" and "final" keywords in Dart?
I have a flutter app which accepts an amount of money as input using a textformfield. I would like to format the input in textformfield so that whatever is being input can be formatted as currency copmlete wiht thousand separator commas. I have tried using the intl package number formatter but all I can do is print it to the command line.
Here is how it looks currently
This is how I would like it to look like
This is the textfield code
TextEditingController currencyControler = TextEditingController();
String? amount;
TextFormField(
controller: currencyControler,
keyboardType: TextInputType.number,
validator: (value) {
if (value!.isEmpty) {
return 'Please enter an amount';
}
return null;
},
onSaved: (String? value) {
amount = value;
},
decoration: InputDecoration(
icon: const Icon(Icons.money_outlined),
labelText: "Amount",
hintText: 'Enter an amount',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
),
),
)
How can I format the input so that the comma separators appear as any number is being entered
Try below code hope its helpful to you.Used intl package here for number formation
Your functions:
TextEditingController currencyControler = TextEditingController();
String formNum(String s) {
return NumberFormat.decimalPattern().format(
int.parse(s),
);
}
Your Widget:
TextFormField(
controller: currencyControler,
decoration: InputDecoration(
border: OutlineInputBorder(),
prefixIcon: Icon(
Icons.money,
)),
keyboardType: TextInputType.number,
onChanged: (string) {
string = '${formNum(
string.replaceAll(',', ''),
)}';
currencyControler.value = TextEditingValue(
text: string,
selection: TextSelection.collapsed(
offset: string.length,
),
);
},
),
Your result screen->
I need separate logic with UI.
I used the following example:
1.- Use a class validation item to show a string value and error.
class ValidationItem {
final String value;
final String error;
ValidationItem(this.value, this.error);
}
2.- Use the next code for provider class.
class SignupValidation with ChangeNotifier {
ValidationItem _firstName = ValidationItem(null,null);
//Getters
ValidationItem get firstName => _firstName;
bool get isValid {
if (_firstName.value != null){
return true;
} else {
return false;
}
}
//Setters
void changeFirstName(String value){
if (value.length >= 3){
_firstName=ValidationItem(value,null);
} else {
_firstName=ValidationItem(null, "Must be at least 3 characters");
}
notifyListeners();
}
void submitData(){
print("FirstName: ${firstName.value}");
}
}
3.- Use the next widget to show text field and validate
class Signup extends StatelessWidget {
#override
Widget build(BuildContext context) {
final validationService = Provider.of<SignupValidation>(context);
return Scaffold(
appBar: AppBar(
title: Text('Signup'),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
children: <Widget>[
TextField(
decoration: InputDecoration(
labelText: "First Name",
errorText: validationService.firstName.error,
),
onChanged: (String value) {
validationService.changeFirstName(value);
},
),
RaisedButton(
child: Text('Submit'),
onPressed: (!validationService.isValid) ? null : validationService.submitData,
)
],
),
),
);
}
}
The problem is the performance for example every time the text is changed the notifyListener() Is calles.
My question: Is there a cost to performance?
you can use TextFormField instead of TextField.
Best way to validate fields is that you can use validator property TextFormField property as bellow
TextFormField(
decoration: InputDecoration(
labelText: "First Name",
errorText: validationService.firstName.error,
),
validator: (e){
if(e!.trim().isEmpty) return "String is empty";
return null;
},
onChanged: (String value) {
validationService.changeFirstName(value);
},
),
The TextField itself gives you the ability to validate the form, then why to make it complex by implementing notifier, instead you want to make it common you can make the validater global function for it. Nad user it in the validate function.
Void validateEmail(String value){ // your logic}
Use this function as follow
TextFormField(
decoration: InputDecoration(
labelText: "First Name",
errorText: validationService.firstName.error,
),
validator: validateEmail(),
onChanged: (String value) {
validationService.changeFirstName(value);
},
Secondly to get the value of inputted string you have a TextEditingController which directly give you the string you inputted.
Declare TextEditingController as follow
TextEditingController emailCont = TextEditingController();
Use this controller in TextField as follow
TextFormField(
controller: emailCont,
decoration: InputDecoration(
labelText: "First Name",
errorText: validationService.firstName.error,
),
validator: validateEmail(),
onChanged: (String value) {
validationService.changeFirstName(value);
},
Now to get the value from this controller you can get it this way.
emailCont.text
This way it will be easy to manage and less complexity.
I am developing e com application, when user click on quantity label want to show dialog box for select how many quantity he want to buy.
I tried to wrap number picker inside Alert dialog. It show alert dialog, but the problem is not updating value when scroll
I can I use Number Picker for that?
Use a drop button combo widget. The DropdownButtonFormField holds a collection of YourClassView type objects held in the list listStatusMenuItems. The _currentStatusComboView variable holds the selected value. It is assigned on the onChange event. I use a restful call to load the listStatusMenuItems.
List<DropdownMenuItem> listStatusMenuItems = <DropdownMenuItem>[];
StatusComboView _currentStatusComboView;
_loadStatusCombo() {
Provider.of<Api>(context, listen: false)
.getYourClassViews()
.then((listView) {
setState(() {
listStatusMenuItems =
listView?.map<DropdownMenuItem<YourClassView>>((item) {
return DropdownMenuItem<StatusComboView>(
value: item, child: Text(item.displayValue));
}).toList();
});
});
#override
void initState() {
super.initState();
_loadStatusCombo();
}
DropdownButtonFormField<YourClassView>(
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(5.0),
),
),
filled: true,
hintStyle:
TextStyle(color: Colors.grey[800]),
hintText: "Select a Value",
fillColor: Colors.orange),
items: listStatusMenuItems,
isDense: true,
isExpanded: true,
value: this._currentStatusComboView,
validator: (value) =>
value == null ? 'field required' : null,
onChanged: (StatusComboView value) {
setState(() {
this._currentStatusComboView = value;
});
}),
I am trying to set a value to the TextFormField in flutter.
But I couldn't find a way to do that.
this is how my widget looks like:
Widget _showHeadlineField() {
return TextFormField(
textInputAction: TextInputAction.next,
onEditingComplete: () {
FocusScope.of(context).requestFocus(_descriptionNode);
},
controller: _headlineController,
validator: (headline) {
if (headline == null || headline.isEmpty) {
return "Headline cannot be empty";
}
},
decoration: InputDecoration(
labelText: "Headline",
hintText: "Covid-19 new stats",
border: OutlineInputBorder(),
icon: Icon(Icons.add_box),
),
);
}
I even tried initialValue but doesn't work. Can someone help me?
You can use
_headlineController.text = 'Your text';
Or when you create controller :
_headlineController = TextEditingController(text: 'Your text')