I want to provide a limit to the textfield in flutter. For example, I want that the user can only type numbers between 0-5000. How can I achieve it?
child: TextField(
controller: sliderController,
onChanged: (value) {
setState(() {
amount = value;
sliderAmount = double.parse(value);
});
},
decoration: InputDecoration(
border: OutlineInputBorder(),
contentPadding: EdgeInsets.fromLTRB(15, 65, 15, 0),
prefixText: '₹',
prefixStyle: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 30,
),
),
keyboardType: TextInputType.number,
inputFormatters: [
LengthLimitingTextInputFormatter(6),
],
),
Related
When a user double taps the text to bring up "Cut Copy Paste", there is a sizing error with the pop-up and the text is cut off. I can't find any questions around this that can help.
See Screenshot and code below
Padding(
padding: const EdgeInsets.only(left: 12.0),
child: CupertinoTextField(
textCapitalization: TextCapitalization.words,
focusNode: titleFocus,
autofocus: true,
controller: titleController,
keyboardType: TextInputType.multiline,
maxLines: null,
cursorColor: Theme.of(context).cursorColor,
onSubmitted: (text) {
titleFocus.unfocus();
FocusScope.of(context)
.requestFocus(contentFocus);
},
textInputAction: TextInputAction.next,
style: GoogleFonts.roboto(
textStyle: TextStyle(
//color: Color(0xff3a4759),
fontSize: 20,
color: Theme.of(context).accentColor,
fontWeight: FontWeight.w700),
),
placeholder: "Sermon Title",
placeholderStyle: TextStyle(
color: Colors.grey.shade400,
fontSize: 20,
fontFamily: 'Helvetica Neue',
fontWeight: FontWeight.w500),
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey.withOpacity(0.0),
),
),
),
),
Try below code hope its help to you
Declare one Controller
final TextEditingController _controller = new TextEditingController();
Your Widget :
Column(
children:[
SizedBox(
height:30,
),
Padding(
padding: const EdgeInsets.all( 16.0),
child: CupertinoTextField(
textCapitalization: TextCapitalization.words,
autofocus: true,
controller: _controller,
keyboardType: TextInputType.multiline,
maxLines: null,
cursorColor: Theme.of(context).cursorColor,
textInputAction: TextInputAction.next,
placeholder: "Sermon Title",
placeholderStyle: TextStyle(
color: Colors.grey.shade400,
fontSize: 20,
fontFamily: 'Helvetica Neue',
fontWeight: FontWeight.w500),
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey.withOpacity(0.0),
),
),
),
),
],
),
Your result screen ->
I Have couple of TextEditingControllers in an ExpansionTile.
When i click on one of them, I get multi cursor on all of the TextEditingController with it.
When i put a text in one the TextEditingController (or any action) : it affect all of the other Text in the same ExpansionTile widget.
widget build(){
...
...
ExpansionTile(
title: Container(
width: double.infinity,
child: Text("Parametre vitaux",
style: TextStyle(fontSize: 18),)),
trailing: Icon(Icons.arrow_drop_down, size: 32,
color: Colors.black,),
onExpansionChanged: (value) {
setState(() {
// isExpand=value;
});
},
children: [
listmesures()
]
)
}
Widget listmesures() {
return Container(child:
ListView(
shrinkWrap: true,
scrollDirection: Axis.vertical,
children: [
TextFormField(
keyboardType: TextInputType.phone,
focusNode: f2,
controller: _poidsController,
style: GoogleFonts.lato(
fontSize: 18, fontWeight: FontWeight.bold),
decoration: InputDecoration(
contentPadding:
EdgeInsets.only(left: 20, top: 10, bottom: 10),
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(90.0)),
borderSide: BorderSide.none,
),
filled: true,
fillColor: Colors.grey[350],
hintText: 'Poids Kg*',
hintStyle: GoogleFonts.lato(
color: Colors.black26,
fontSize: 18,
fontWeight: FontWeight.w800,
),
),
validator: (value) {
if (value.isEmpty) {
return 'entrez poids';
}
return null;
},
onFieldSubmitted: (String value) {
f2.unfocus();
FocusScope.of(context).requestFocus(f3);
},
textInputAction: TextInputAction.next,
),
SizedBox(
height: 10,
),
TextFormField(
keyboardType: TextInputType.phone,
focusNode: f2,
controller: _tempController,
style: GoogleFonts.lato(
fontSize: 18, fontWeight: FontWeight.bold),
decoration: InputDecoration(
contentPadding:
EdgeInsets.only(left: 20, top: 10, bottom: 10),
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(90.0)),
borderSide: BorderSide.none,
),
filled: true,
fillColor: Colors.grey[350],
hintText: 'Temperature*',
hintStyle: GoogleFonts.lato(
color: Colors.black26,
fontSize: 18,
fontWeight: FontWeight.w800,
),
),
validator: (value) {
if (value.isEmpty) {
return 'entrez temperature';
}
return null;
},
onFieldSubmitted: (String value) {
f2.unfocus();
FocusScope.of(context).requestFocus(f3);
},
textInputAction: TextInputAction.next,
),
.....
],
));
}
ExpansionTile: It is a simple and useful widget. This widget lets you create a collapse or expansion view with features similar to ListTile. It is like a ListTile which will expand on tapping the title.
TextEditingController : Connect the TextEditingController to a text field.
I've solved using different focusNode.
$ flutter channel
Flutter channels:
stable
beta
master
first:
flutter channel stable
and
flutter upgrade
running in 2022
channel documentation
https://github.com/flutter/flutter/wiki/Flutter-build-release-channels
I have a simple TextField to get a number and checking if it contains a number less than 50.
Note : keyboardType: TextInputType.number
Here is my how im doing it:
Column(
children: [
TextField(
controller: textEditingController,
keyboardType: TextInputType.number,
style: TextStyle(color: Colors.white, fontSize: 20),
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Number of Questions',
hintStyle: TextStyle(color: Colors.white, fontSize: 17),
),
),
InkWell(
onTap: () {
if (textEditingController.text == '50') {
print(textEditingController.text);
} else {
showInSnackBar();
}
},
child: Text(
'Start',
style: TextStyle(
color: Colors.black,
fontSize: 26,
fontWeight: FontWeight.bold,
letterSpacing: 2,
),
),
),
],
);
#override
void initState() {
textEditingController.addListener(_handleNumber);
}
void _handleNumber(){
if(textEditingController.text.isNotEmpty){
var num = int.parse(textEditingController.text);
if(num <= 50){....}
}
}
You can use the onchanged function callback like this:
Column(
children: [
TextField(
// controller: textEditingController,
keyboardType: TextInputType.number,
style: TextStyle(color: Colors.white, fontSize: 20),
onChanged: (value){
{
if (value == '50') {
print(value);
} else {
showInSnackBar();
}
},
},
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Number of Questions',
hintStyle: TextStyle(color: Colors.white, fontSize: 17),
),
),
],
),
Inside my TextFormField, I have a maxLength of 35, and the maxLengthEnforced (which by default is true) isn't being enforced, when the user writes something between the text, it just pushes the characters to the right and deletes the ones in the end to meet the 35 maxLength.
This is what I'm using:
TextFormField(
focusNode: _currentFocus,
textInputAction: _textInputAction ?? TextInputAction.done,
onFieldSubmitted: _textInputAction == TextInputAction.done
? (_) => FocusScope.of(context).unfocus()
: (_) => FocusScope.of(context).nextFocus(),
initialValue: _initialValue,
decoration: InputDecoration(
counterText: "",
hintText: _hintText,
hintStyle: TextStyle(color: Colors.grey),
focusedBorder: new UnderlineInputBorder(
borderSide: new BorderSide(color: Colors.blue),
),
),
maxLines: 1,
maxLength: 35,
maxLengthEnforced: true,
onChanged: _onChanged,
style: TextStyle(
color: Colors.black,
fontSize: 20,
fontWeight: FontWeight.w500),
);
I want to make the hint text in TextFormField disappear when the field focused. But the hint text won't disappear until I insert a character in the field. I did the following. How can I make the hint text disappear when the field focused? Thanks!
Container buildTextField(bool isObscure, TextEditingController textEditingController, String hintText, bool onlyNumber) {
return Container(
width: double.infinity,
height: 60,
color: MyColors.primary,
alignment: Alignment.center,
child: Padding(
padding: EdgeInsets.only(left: 15, right: 10),
child: TextFormField(
textAlign: TextAlign.center,
keyboardType: onlyNumber ? TextInputType.number : TextInputType.text,
decoration: InputDecoration(
hintText: hintText,
hintStyle: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w600,
),
border: InputBorder.none,
),
style: TextStyle(
color: Colors.white,
decoration: TextDecoration.none,
fontWeight: FontWeight.w600,
),
obscureText: isObscure,
controller: textEditingController,
),
),
);
}
You probably have to add a listener to listen to focus changes:
FocusNode myFocusNode = FocusNode();
myFocusNode.addListener( () {
setState((){});
}
And then, inside your InputDecoration you update the value:
hintText: myFocusNode.hasFocus ? hintText : ''
Use labelText instead,
TextFormField(
controller: fullNameController,
validator: (value) {
if (value.trim().isEmpty) {
return Lang.enterFullName;
}
return null;
},
decoration: InputDecoration(
labelText: Lang.fullName,
prefixIcon: Icon(Icons.person),
),
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
),