There is a problem in Android Web Chrome. When Text inputType text or name does not work onsubmit and it is not possible to send a request. But for some reason, if you set the TextInputType.emailAddress parameter, then the submit key on the keyboard becomes a "right arrow" and everything works as it should work. What could be the reason for this behavior?
TextFormField(
enabled: isEnabled ?? true,
obscureText: isObscured ?? false,
controller: controller,
keyboardType: widget.keyboardType,
textInputAction: widget.textInputAction,
minLines: widget.minLines ?? 1,
maxLines: widget.maxLines ?? 1,
decoration: InputDecoration(
fillColor: AppColors.fieldBackground,
filled: true,
hintText: widget.hintText,
contentPadding: const EdgeInsets.only(
top: (Dimens.buttonHeight - 14) / 2,
bottom: (Dimens.buttonHeight - 14) / 2,
left: Dimens.horizontalPadding,
right: Dimens.horizontalPadding,
),
onChanged: (value) {
setState(() {
textLength = value.length;
});
},
focusNode: focusNode,
onFieldSubmitted: (query) {
if (query.length > 2 || query.isEmpty) {
serviceLocator<SearchBloc>().add(
FiltersChanged(query: query),
);
FocusScope.of(context).unfocus();
} else {
Messages.showInfoMessage(
context,
AppLocalizations.of(context)!.searchFieldShortQueryTitle,
AppLocalizations.of(context)!.searchFieldShortQueryMessage);
}
},
);
You can set the input action manually and it should work as expected
TextField(
textInputAction: TextInputAction.go,
)
Related
I've made a TextField that is suppose to update the user's info.
The user has to type either 11 or 14 numbers, so the mask for the textfield has to change if someone types more than 11 numbers. How do I do that?
Masks:
var mascaraCpf = MaskTextInputFormatter(
mask: '###.###.###-##',
filter: {"#": RegExp(r'[0-9]')},
type: MaskAutoCompletionType.lazy);
var mascaraCnpj = MaskTextInputFormatter(
mask: '##.###.###/####-##',
filter: {"#": RegExp(r'[0-9]')},
type: MaskAutoCompletionType.lazy);
TextField:
TextField(
keyboardType: TextInputType.number,
inputFormatters: [
mascaraCpf,
FilteringTextInputFormatter.digitsOnly
],
controller: cpfController,
decoration: InputDecoration(
filled: true,
fillColor: Color(0xffFCF9F4),
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(5))),
hintText: appModel.usuario!.cpf,
),
),
Do you have an StatefulWidget?
If yes, try something like the following:
onChanged: (value) {
if (value.length >= 11) {
setState(() {
//change something
});
} else {
setState(() {
//change back
});
}
}
First, make sure your screen is a stateful widget.
Then replace the textfield with the following code:
TextField(
onChanged: (value) => {
if (value.length >= 11 && value.length <= 14) //changing value if input length is more than 11 and less than 14
{
setState(() {
currentMask = mascaraCnpj;
})
}
else
{
if(currentMask != mascaraCpf) { //rebuilding screen if currentMask != mascaraCpf , otherwise it'll lead to unnecessary rebuilds
setState(() {
currentMask = mascaraCpf;
})
}
}
},
keyboardType: TextInputType.number,
inputFormatters: [currentMask, FilteringTextInputFormatter.digitsOnly],
controller: TextEditingController(),
decoration: InputDecoration(
filled: true,
fillColor: Color(0xffFCF9F4),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(5))),
hintText: appModel.usuario!.cpf,
),
),
I have name field in profile page. I want to restrict user from adding leading spaces. But in the process what I did is the user can't put any space now. I want him to simple enter "Arjun Malhotra" and not " space space space Arjun Malhotra"......
This is the code
child: TextFormField(
inputFormatters: [
FilteringTextInputFormatter
.deny(
RegExp('[ ]')),
],
maxLength: 20,
readOnly: !edit,
controller: name,
autovalidateMode:
AutovalidateMode
.onUserInteraction,
autofocus: true,
validator: (value) {
RegExp regex =
RegExp(r'^.{3,}$');
if (value!.isEmpty) {
return ("Name can't be empty");
}
if (!regex
.hasMatch(value)) {
return ("Name can't be empty");
}
if (value.trim() ==
"") {
return ("Name can't be empty");
}
if (value
.trim()
.length <=
2) {
return ("Minimum of 3 characters Required");
}
return null;
},
onSaved: (value) {
name.text = value!;
},
onChanged: (value) {
context
.read<
ProfileDetails>()
.name = value;
},
decoration:
InputDecoration(
counterText: "",
filled: true,
fillColor: const Color(
0xFFC2E3FE),
enabledBorder:
InputBorders
.enabled,
errorBorder:
InputBorders.error,
focusedErrorBorder:
InputBorders.error,
border:
InputBorder.none,
focusedBorder:
InputBorders
.focused,
contentPadding:
const EdgeInsets
.only(
left: 30,
right: 10),
hintText: "Name",
),
),
Above code only allows this format "ArjunMalhotra". How can I achieve what I want and also I dont want user to add emojis. Is there a special keyboard type I need to specify or what?
^\s*: This will match any (*) whitespace (\s) at the beginning (^) of the text
inputFormatters: [
FilteringTextInputFormatter.deny(RegExp(r'^\s*')),
],
Everything works fine except the space between the elements/user input and bottom bar. I tried different methods to get rid of that space: content padding, box constraints, prefix icon constraints etc. None worked. Eventually I limited the height of this widget, but then my error message was placed on top of user input and I was getting this message Another exception was thrown: BoxConstraints has a negative minimum height.
Here is the code:
Widget _emailField(FormBloc formBloc) {
return StreamBuilder(
stream: formBloc.email,
builder: (context, AsyncSnapshot<String> snapshot) {
return TextFormField(
autofocus: false,
keyboardType: TextInputType.emailAddress,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null) {
return "Email cannot be empty";
} else {
if (value.isEmpty) {
return "Email cannot be empty";
}
}
},
onChanged: formBloc.changeEmail,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
prefixIcon: const Icon(
Icons.email,
size: 15,
),
contentPadding: const EdgeInsets.all(0),
labelText: "Email",
labelStyle: const TextStyle(fontSize: 14),
errorText:
snapshot.error != null ? snapshot.error as String : null,
),
);
}
);
}
I need to make everything work as before, except this time I do need to have no space between bottom border and input elements, how can I do so?
First inside InputDecoration(), you can give zero padding to your prefix icon.
Then set prefixIconConstraints to BoxConstraints(maxHeight:0)
Still isn't enough you can give negative value to contentPadding: EdgeInsets.only(bottom:-5),.
InputDecoration(
prefixIcon: Padding(
padding: EdgeInsets.only(right:10),
child:const Icon(
Icons.email,
size: 15,
),
),
prefixIconConstraints: BoxConstraints(maxHeight:0),
contentPadding: EdgeInsets.only(bottom:-5),
labelText: "Email",
labelStyle: const TextStyle(fontSize: 14),
errorText:"error",
)
Here is the code that I want to delete the field from -
setPincode() {
return TextFormField(
keyboardType: TextInputType.number,
controller: pincodeC,
readOnly: true,
style: Theme.of(context)
.textTheme
.subtitle2
.copyWith(color: colors.fontColor),
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
onSaved: (String value) {
pincode = value;
},
validator: (val) =>
validatePincode(val, getTranslated(context, 'PIN_REQUIRED')),
decoration: InputDecoration(
hintText: getTranslated(context, 'PINCODEHINT_LBL'),
isDense: true,
),
);
}
Or you can see in this image what I want to remove
You are calling a function called setPincode that returns this TextFormField. Don't call setPincode if you no longer need it.
TextField (
decoration: InputDecoration(
hintText: 'Need extra ice or have another request?',
contentPadding: EdgeInsets.all(10),
),
//else {
// decoration: InputDecoration(
// hintText: Provider.of<OrderProvider>(context, listen: false).specialNotes,
// contentPadding: EdgeInsets.all(10),
// ),}
onChanged: (newText) {
Provider.of<OrderProvider>(context, listen: false)
.updateSpecialRequests(newText);
},
keyboardType: TextInputType.multiline,
maxLines: null,
),
I want to make a conditional statement that determines whether the code above the commented code is read or the commented code below it gets read. Textfield is an array of children. However I try to do this, it breaks my code. The TextField invariably is no longer lit up in gold as it is when the code is working. The online suggestions have not been working.
You can use tenary operators
TextField (
decoration: condition ? InputDecoration(
hintText: 'Need extra ice or have another request?',
contentPadding: EdgeInsets.all(10),
): InputDecoration(
hintText: Provider.of<OrderProvider>.specialNotes,
contentPadding: EdgeInsets.all(10),
),
onChanged: (newText) {
Provider.of<OrderProvider>(context, listen: false)
.updateSpecialRequests(newText);
},
keyboardType: TextInputType.multiline,
maxLines: null,
),