I cant get the textfield labeltext to show. anyone can help? and how do I get the border to always showing and not disappear when I click on the text field.
#override
Widget build(BuildContext context) {
return TextFormField(
controller: _controller,
decoration: InputDecoration(
border: new OutlineInputBorder(
borderSide: new BorderSide(width: 1)
),
// hintText: _hintText,
labelText: "Label Text",
contentPadding: EdgeInsets.only(left: 15),
// suffixText: _nameLength.toString() + "/ " + _maxLength.toString(),
// suffixStyle: TextStyle(fontSize: 12),
// suffixStyle: TextStyle(color: Theme.of(context).textTheme.bodyText2.color),
// counterText: "",
),
maxLines: _maxLines,
textCapitalization: TextCapitalization.sentences,
cursorRadius: Radius.circular(10),
keyboardType: TextInputType.text,
autofocus: true,
maxLength: _showTextCount ? _maxLength : null,
style: TextStyle(color: AppSetting.textColor1),
validator: (val) {
// if (val.isEmpty) {
// return 'Please enter text.';
// }
// return null;
},
onChanged: (val) {
_onChanged(val);
setState(() {
_inputFieldText = val;
_nameLength = _inputFieldText.length;
});
},
onTap: () => AppSetting.hapticFeeback(),
);
}
}
You should add
errorBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.redAccent,
width: 2.0,
),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: primaryColor,
width: 2.0,
),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.black54,
width: 2.0,
),
),
),
Just make sure the default value of labelColor of the input decoration is different from background (the default label color was white in my case). In order to show border when clicked set focusedBorder property.
Related
i have a form pages contain a widget that will return various widget form according to the api parameters, for example: the parameters is 'type': 'text_box so the widget will return TextField widget if the parameters is 'type': 'dropdown_box' it will return dropdown widget.
This is just an example for what widget i make, the real problem is in the TextField widget because everytime i input in the field then press any other form widget(not pressing the complete button on textfield) and select the value it will return the focus to the TextField before if i press complete button the focus is neutral again what i want is when i press another widget the focus from the TextField is clear.
code
TextFormField(
minLines: widget.minLines,
inputFormatters: [
if (widget.isPrice)
MoneyInputFormatter(
thousandSeparator: ThousandSeparator.Period,
mantissaLength: 0,
),
],
controller: widget.controllerName,
onTap: () {
widget.onTap();
},
enabled: widget.enabled,
obscureText: widget.isObsecure,
style: TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.w400,
color: (widget.textColor != null)
? widget.textColor
: widget.enabled
? Colors.black
: Colors.black38,
),
keyboardType: widget.keyboardType,
maxLines: widget.maxLines,
decoration: InputDecoration(
isDense: true, //remove default padding
fillColor:
widget.inputColor ?? Constants.colorAppointmentTextInput,
filled: true, // activate bg color
// hintText: widget.hintText,
hintText: widget.placeholder ?? 'Masukan ' + widget.hintText,
hintStyle: TextStyle(
fontSize: 14.0,
color: (widget.textColor != null)
? widget.textColor
: widget.enabled
? Colors.black54
: Colors.black38,
),
prefix: widget.prefix,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: BorderSide(
color: Constants.colorAppointmentBorderInput,
width: 0.5)),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: BorderSide(
color: Constants.colorAppointmentBorderInput,
width: 0.5)),
disabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: BorderSide(
color: Constants.colorAppointmentBorderInput,
width: 0.5)),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide:
BorderSide(color: Constants.redTheme, width: 0.5),
),
suffixIcon: widget.suffixIcon),
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
this is what i mean the widget will return widget according to the api
#override
Widget build(BuildContext context) {
if (widget.questionType == 'text_box') {
return TextFild();
} else if (widget.questionType == 'dropdown') {
return Dropdown();
} else if (widget.questionType == 'radio') {
return radioType();
}
}
Add this Line :
FocusScope.of(context).requestFocus(FocusNode());
if keyboard on it will make it off , if it's off it will make it on .
You could use a focus node:
myFocusNode = FocusNode()
Add it to your TextFormField:
TextFormField(
focusNode: myFocusNode,
...
);
When an event that you want to unfocus the text field occurs, like tapping some other button etc. use:
myFocusNode.unfocus();
Make sure to dispose of the FocusNode in your dispose method:
#override
void dispose() {
myFocusNode.dispose();
//...
super.dispose();
}
You can use FocusNode.
Add it to TextformField.
TextFormField(
focusNode: focusNode,
...
);
Call focusNode.unfocus(); on onFieldSubmitted, onSaved. In your case on click action when you show your dropdown.
I'm trying to add validation on textfield, i want when i leave any textfield empty it change its border color into red and display a error message, so and when i write something in it then it should hide the border error and message, which is happening but not in efficient way, here is what i'm doing.
i created the custom textfield
Widget textformfieldCustom(context,keyboardType,width,icon, controller,errortext,onchanged, hintText, labelText) {
return Container(
width: width,
child:TextFormField(
keyboardType:keyboardType,
decoration: InputDecoration(
contentPadding:EdgeInsets.symmetric(vertical: 20.0, horizontal: 10.0),
errorText:errortext,
labelText: labelText,
labelStyle: GoogleFonts.montserrat(color: HexColor("#6e6b7b")),
hintStyle: GoogleFonts.montserrat(),
hintText: hintText,
prefixIcon: icon,
focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(10.0)),borderSide: BorderSide(color: HexColor("#6610f2"))),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0))),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.red, width: 1))),
onSaved: (String? value) {
// This optional block of code can be used to run
// code when the user saves the form.
},
onChanged:onchanged,
controller: controller,
));
}
and calling it as like this
bool _validatetex = false;
textformfieldCustom(
context,
TextInputType.number,
MediaQuery.of(context).size.width * 0.9,
Icon(Icons.email,color: iconColor,),
phoneNoController,
_validatetex ? 'This is the required field' : null,
(value) {
setState(() {
phoneNoController.text.isEmpty ? _validatetex = true : _validatetex = false;
});
},
'Enter your phone number',
'Phone number'
),
i'm using a bool type variable in errortext and changing its state in onchanged, so i want to do it in efficient way, like if i have 10 textfields so i have to initialize 10 bool variables so this is not a good way to go. please help how to achieve this in efficient way.
You can use the validator property in `TextField``
validator: (value) {
if (value == null || value.isEmpty) {
return 'This is the required field';
}
return null;
},
Then your code,
Widget textformfieldCustom(context,keyboardType,width,icon, controller,errortext,onchanged, hintText, labelText) {
return Container(
width: width,
child:TextFormField(
keyboardType:keyboardType,
decoration: InputDecoration(
contentPadding:EdgeInsets.symmetric(vertical: 20.0, horizontal: 10.0),
labelText: labelText,
labelStyle: GoogleFonts.montserrat(color: HexColor("#6e6b7b")),
hintStyle: GoogleFonts.montserrat(),
hintText: hintText,
prefixIcon: icon,
focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(10.0)),borderSide: BorderSide(color: HexColor("#6610f2"))),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0))),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.red, width: 1))),
onSaved: (String? value) {
// This optional block of code can be used to run
// code when the user saves the form.
},
onChanged: onchanged,
controller: controller,
// Validator
validator: (value) {
if (value == null || value.isEmpty) {
return 'This is the required field';
}
return null;
},
));
}
I got my answer from this tutorial.
I want to change the background color of the TextFormField
when I selected the TextFormField to enter a value.
default : TextFormField background color is grey
on focus : TextFormField background color is blue.
What should I do?
const _lowColor = Colors.redAccent; // use your own colors
const _highColor = Colors.yellow;
Color _field1Color = _lowColor;
Focus(
onFocusChange: (hasFocus) {
setState(() =>
_field1Color = hasFocus ? _highColor : _lowColor)
);
},
child: TextFormField(
obscureText: (textType == "password") ? true : false,
controller: (controller != null) ? controller : null,
focusNode: _thisFocus,
validator: (value) => (value.isEmpty) ? "Enter the Text" : null,
style: _inputStyle,
onFieldSubmitted: (String v) => {
if (onFieldSubmitted != null) {
onFieldSubmitted(v)
}
},
decoration: InputDecoration(
labelText: placeHolder,
labelStyle: _labelStyle,
filled: true,
fillColor: _field1Color,
contentPadding: EdgeInsets.only(top: 4,bottom: 4,left: 6,right: 6),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Color(0xffE8E8E8), width: 1.0),
borderRadius: BorderRadius.circular(10)
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Color(0xff3FC4FF), width: 1.0),
borderRadius: BorderRadius.circular(10)
)
),
),
),
I have tried to understand your questions and provide you with a better option to design. Do try it out and revert me how do you feel, you can customise it if you like.
Click here to see the design
Container inputBox(String tabName,bool hidePassword){
return Container(
height: 50,
margin: EdgeInsets.symmetric(vertical: 15.0, horizontal: 54.0,),
child: TextField(
obscureText: hidePassword,
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Color(0xFF579FAF), width: 2.0),
borderRadius: BorderRadius.circular(7.0),
),
labelText: tabName,
labelStyle: TextStyle(color: Colors.white),
contentPadding: EdgeInsets.symmetric(horizontal: 20),
focusedBorder:OutlineInputBorder(
borderSide: BorderSide(color: Color(0xFF579FAF), width: 2.0),
borderRadius: BorderRadius.circular(7.0),
),
),
)
);
}
Resolved the problem.
I modified the background of TextField to change it using focus value.
fillColor: _focusNode.hasFocus ? _focusColor : _normalColor,
how to add two textformfields controllers in which int value is given?
when i was doing it showing me error saying that textediting controller can't be int.
child: ok != null ? Text('${okk1+ok1}', style: TextStyle(color: Colors.black, fontSize: 14),) :Text(""),
first textformfield:-
Container(
height: MediaQuery.of(context).size.height/12,
width: MediaQuery.of(context).size.width/3 ,
padding: const EdgeInsets.all(10.0),
child: TextFormField(
controller: _hire,
inputFormatters: [
new LengthLimitingTextInputFormatter(7),
],
keyboardType: TextInputType.text,
onChanged: (str) {
setState(() {
okk = _advance.text as int;
ok1 =int.parse(_hire.text);
});
print(ok1);
},
decoration: const InputDecoration(
labelText: '',
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.grey),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.grey),
),
),
onSaved: (String value) {
// This optional block of code can be used to run
// code when the user saves the form.
},
validator: (String value) {
return value.contains('#')
? 'Do not use the # char.'
: null;
},
),
),
second textformfield:-
Container(
height: MediaQuery.of(context).size.height/12,
width: MediaQuery.of(context).size.width/3 ,
padding: const EdgeInsets.all(10.0),
child: TextFormField(
controller: _advance,
inputFormatters: [
new LengthLimitingTextInputFormatter(7),
],
keyboardType: TextInputType.numberWithOptions(),
onChanged: (str) {
setState(() {
ok = _advance.text;
okk1 =int.parse(_advance.text);
});
print(okk1);
},
decoration: const InputDecoration(
labelText: '',
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.grey),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.grey),
),
),
onSaved: (String value) {
// This optional block of code can be used to run
// code when the user saves the form
},
validator: (String value) {
return value.contains('#')
? 'Do not use the # char.'
: null;
},
),
),
these are the textformfield controllers for two textformfield in which data is 45 and 5 but in string because controllers text is in string format and i want to add these two controllers and the result would be 50.
You can add them like this.
If x1 and x2 are two textcontrollers.
var result = int.parse(x1.text) + int.parse(x2.text)
I am new with flutter.
I am making a user registration form, I want to achieve the following visual effect:
When a TextFormField is normal on the form, it looks like this:
But I want the following, when the textformfield is in "focus". When the user is typing it looks like this:
This is my average textFormField
TextFormField(
initialValue: name,
onChanged: (val) {
setState(() {
name = val;
print(name);
});
},
decoration: InputDecoration(
hintText: "Nombres",
hintStyle: TextStyle(
fontSize: scalador.setSp(22) * 2.63,
color: Color(0xFF949494)),
),
style: TextStyle(
color: Color(0xFF242427),
fontSize: scalador.setSp(22) * 2.63,
),
validator: (value) {
if (value.isEmpty) {
return 'Por favor ingrese su(s) Nombre(s)';
} else {
if (value.length < 4)
return 'El nombre debe tener mas de 4 caracteres';
}
return null;
}),
any ideas?
add labelText: 'Nombres', Property into InputDecoration :
decoration: InputDecoration(
hintText: "Nombres",
hintStyle: TextStyle(
fontSize: scalador.setSp(22) * 2.63,
color: Color(0xFF949494)),
),
labelText: 'Nombres',
)
source :https://api.flutter.dev/flutter/material/TextFormField-class.html
To add the desired textformfield in "focus" detail, you will need to include several things. To begin with, you will need a labelText which will be set to the string of your choice. In your case, it would probably be: labelText: "Nombres". Next, you will need an enabledBorder: which you can assign to OutlineInputBorder(in which you can specify border radius, border Side(color)) to your liking. Once you have the enabledBorder for when the user does not have it in "focus" then you will need focusedBorder: in which again you will assign to OutlineInputBorder() similar to enabledBorder. Lastly, you will need border: in which you can give it OutlineInputBorder(and your desired borderRadius inside).
This is an example for your reference
Padding(
padding: EdgeInsets.all(10),
child: new TextFormField(
decoration: new InputDecoration(
labelText: "Name",
labelStyle: TextStyle(
color: Color(0xFF264653),
),
fillColor: Colors.white,
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(
color: Color(0xFF264653),
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(color: Color(0xFF264653))),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
),
),
),
),
Depending on what you will be doing, I recommend checking out this article: https://medium.com/swlh/working-with-forms-in-flutter-a176cca9449a and/or
https://medium.com/#mahmudahsan/how-to-create-validate-and-save-form-in-flutter-e80b4d2a70a4