class _UpdateViewState extends State<UpdateView> {
var dropDownValue = "Vaccine";
#override
Widget build(BuildContext context) {
void getDropDownItem(){
print(val); //Need help here
;}
return Scaffold(
body: new Column(
children: <Widget>[
SizedBox(height: 100),
new ListTile(
leading: const Icon(Icons.person),
title: TextField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(12.0)),
borderSide: BorderSide(color: Colors.blue, width: 2),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.blue),
),
border: OutlineInputBorder(),
labelText: 'Enter Provider Name Here',
hintText: 'Provider Name',
),
),
),
}
In this ListTile, I am trying to collect the text value and print. I'm not even sure how to collect the value from the TextField. In the tutorials, this part was skipped. Please help
You can set the controller in TextField
take controller variable
TextEditingController controller = new TextEditingController();
in TextField set controller
TextField(
controller: controller,
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(12.0)),
borderSide: BorderSide(color: Colors.blue, width: 2),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.blue),
),
border: OutlineInputBorder(),
labelText: 'Enter Provider Name Here',
hintText: 'Provider Name',
)
and you can get text using controller like
controller.text
You need to use a TextEditingController , as for example:
TextEditingController _myController = TextEditingController();
final textfield = TextField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(12.0)),
borderSide: BorderSide(color: Colors.blue, width: 2),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.blue),
),
border: OutlineInputBorder(),
labelText: 'Enter Provider Name Here',
hintText: 'Provider Name',
),
Then you can access this value using _myController.text property
There are multiple ways of doing it:
declaring a variable and assigning value to it-
String textvalue;
TextField(
onChanged: (value) {
email = value;
},
)
by assigning TextEditingController()-
TextEditingController textController;
TextField(
controller: textController,
),
now use it as
var value = textColtroller.text;
Related
I am trying resize DropdownButtonFormField with SizedBox. The Hinttext is shown correctly, but when I choose on of the options I get a half of text.
Also I have the Column with TextFields and SizedBox (for separating them) and I want have
What I have
What I want but with correct Text not only hint
return SizedBox(height: 42.5, child: DropdownButtonFormField(
value: _selectedVal,
items: _items
.map((x) => DropdownMenuItem(
value: x,
child: Text(x),
))
.toList(),
onChanged: (val) {
setState(() {
_selectedVal = val as String;
});
},
decoration: const InputDecoration(
filled: true,
fillColor: Colors.white,
hintText: 'hintText',
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
borderRadius: BorderRadius.all(Radius.circular(10))),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
borderRadius: BorderRadius.all(Radius.circular(10)),
),
),
));
try add contentPadding EdgeInsets.zero inside InputDecoration
return SizedBox(height: 42.5, child: DropdownButtonFormField(
value: _selectedVal,
items: _items
.map((x) => DropdownMenuItem(
value: x,
child: Text(x),
))
.toList(),
onChanged: (val) {
setState(() {
_selectedVal = val as String;
});
},
decoration: const InputDecoration(
contentPadding: EdgeInsets.zero <====== Here
filled: true,
fillColor: Colors.white,
hintText: 'hintText',
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
borderRadius: BorderRadius.all(Radius.circular(10))),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
borderRadius: BorderRadius.all(Radius.circular(10)),
),
),
));
can you please let me know how we can remove a small margin from the text field label widget? also, can we add a different design for the label?
1.can we set different design label widgets to display vertically in the center of the text field? I want to show normal text in the center and when I tap on the text field then shows the label with a border.
2.Is there any method available to remove the extra margin from the label widget?
Here is below textField widget.
TextFormField(
validator: (value) {
return (value == null || value.isEmpty)
? 'Please Enter Password'
: null;
},
controller: tfPasswordController,
decoration: inputDecoration('Password', Icons.lock),
),
Here is the Input decoration
InputDecoration inputDecoration(String labelText, IconData iconData,
{String? prefix, String? helperText}) {
return InputDecoration(
contentPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 12),
// helperText: helperText,
// labelText: labelText,
labelStyle: const TextStyle(color: Colors.black),
fillColor: Colors.blue,
filled: true,
prefixText: prefix,
prefixIcon: Icon(
iconData,
size: 20,
),
alignLabelWithHint: false,
label: Container(
padding: EdgeInsets.zero,
decoration: BoxDecoration(color: Colors.green,borderRadius: BorderRadius.circular(20),
border: Border.all(width: 1)),
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 6.0,horizontal: 12),
child: Text(labelText),
)),
prefixIconConstraints: const BoxConstraints(minWidth: 60),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30),
borderSide: const BorderSide(color: Colors.black)),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30),
borderSide: const BorderSide(color: Colors.black)),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30),
borderSide: const BorderSide(color: Colors.black)),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30),
borderSide: const BorderSide(color: Colors.black)),
);
Try below code and set isDense: true, hope its help to you.
TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.all(0.0),
isDense: true,
labelText: "Email",
border: InputBorder.none,
),
),
Result->
As the image is shown below, the first image displays my text field before I click on it. the second image displays how it displays when after start typing and clicking on it. I want to remove that label text display while typing. how can I do that?
TextFormField(
controller: usernameEditingController,
decoration: InputDecoration(
fillColor: textWhite,
filled: true,
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
borderSide: const BorderSide(
color: textdarkBlue,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
borderSide: const BorderSide(
color: textdarkBlue,
),
),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
borderSide: const BorderSide(color: Colors.red),
),
focusedErrorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
borderSide: const BorderSide(color: Colors.red),
),
isDense: true,
contentPadding: EdgeInsets.fromLTRB(10, 30, 10, 0),
labelText: "User Name",
labelStyle: TextStyle(
fontSize: 15,
color: textdarkBlue,
),
hintText: "ex: James",
hintStyle: TextStyle(
color: textdarkBlue,
fontFamily: "Paralucent",
fontSize: 14)),
style: TextStyle(
color: textdarkBlue,
),
validator: (text) {
if (text!.isEmpty) {
return "Username can't be empty";
} else {
return null;
}
},
onChanged: (String? text) {
userName = text!;
//print(userName);
},
),
It is displaying as OutlineInputBorder's behavior.
There are few different ways, One is using FocusNode, goal is to provide labelText:null on focus change.
String userName = "";
late FocusNode focusNode = FocusNode()
..addListener(() {
setState(() {});
});
.....
TextFormField(
// controller: usernameEditingController,
focusNode: focusNode,
decoration: InputDecoration(
labelText: focusNode.hasFocus ? null : "User Name",
That is the behaviour of labelText property if you don't want it , simply remove it and use hintText instead
TextFormField(
controller: usernameEditingController,
decoration: InputDecoration(
fillColor: textWhite,
filled: true,
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
borderSide: const BorderSide(
color: textdarkBlue,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
borderSide: const BorderSide(
color: textdarkBlue,
),
),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
borderSide: const BorderSide(color: Colors.red),
),
focusedErrorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
borderSide: const BorderSide(color: Colors.red),
),
isDense: true,
contentPadding: EdgeInsets.fromLTRB(10, 30, 10, 0),
hintText: "ex: James",
hintStyle: TextStyle(
color: textdarkBlue,
fontFamily: "Paralucent",
fontSize: 14)),
style: TextStyle(
color: textdarkBlue,
),
validator: (text) {
if (text!.isEmpty) {
return "Username can't be empty";
} else {
return null;
}
},
onChanged: (String? text) {
userName = text!;
//print(userName);
},
),
How to pass a data from first screen to second screen but should receive on textfield widget in second screen. I have tried but there is no option in textfield widget, how to do this?
TextField(
keyboardType: TextInputType.text,
decoration: InputDecoration(
fillColor: Colors.white,
hintText: "",
filled: true,
labelText: "First name",
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white),
borderRadius: BorderRadius.circular(6),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white),
borderRadius: BorderRadius.circular(6),
)),
),
To set default text in your TextField you need to use TextEditingController, you can use it by initializing in initState or directly to the TextField, here are the examples
To initialize with initState
late TextEditingController _controller;
#override
void initState() {
_controller.text = widget.text;
super.initState();
}
To pass value directly
TextField(
controller: TextEditingController(text: widget.text),
keyboardType: TextInputType.text,
decoration: InputDecoration(
fillColor: Colors.white,
hintText: "",
filled: true,
labelText: "First name",
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(6),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(6),
)),
)
I am making a Login Form and on error i want to change the color of my TextFormField.
Currently I am using Bloc Architecture and State management.
Here is a picture of how I want the text field on error.
Simply use text validation and test on it.
Here's a generic example :
class MyHomePageState extends State<MyHomePage> {
final _text = TextEditingController();
bool _validate = false;
#override
void dispose() {
_text.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Textfield validation'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('form cant be empty'),
TextField(
controller: _text,
decoration: InputDecoration(
filled: _validate,
fillColor: Colors.red,
labelText: 'Enter the Value',
errorText: _validate ? 'Value Can\'t Be Empty' : null,
),
),
RaisedButton(
onPressed: () {
setState(() {
_text.text.isEmpty ? _validate = true : _validate = false;
});
},
child: Text('Submit'),
textColor: Colors.white,
color: Colors.blueAccent,
)
],
),
),
);
}
}
Here's a codepen demo for it
create a method in util class and call that method as mention below
textFormField(
color: Colors.white,
hintText: "User Name",
radius: 50,
onSave: (String value) {
_loginData.email = value;
},
validator: FormValidator().validateEmail,
prefixIcon: Padding(
padding: EdgeInsets.all(0.0),
child: Icon(
Icons.person,
color: Colors.grey,
), // icon is 48px widget.
),suffixIcon: null,obscureText: false),
textFormField({
Color color,
String hintText,
double radius,
dynamic validator,
Function onSave,
dynamic prefixIcon,
dynamic suffixIcon,
bool obscureText,
}) {
return new TextFormField(
keyboardType: TextInputType.emailAddress,
autofocus: false,
obscureText: obscureText,
decoration: InputDecoration(
hintText: hintText,
errorText: "",
fillColor: color,
filled: true,
contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
focusedErrorBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(radius)),
borderSide: BorderSide(color: color, width: 0)),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(radius)),
borderSide: BorderSide(color: color, width: 0)),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(radius)),
borderSide: BorderSide(color: color, width: 0)),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(radius)),
borderSide: BorderSide(color: color, width: 0)),
disabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(radius)),
borderSide: BorderSide(color: color, width: 0)),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(radius)),
borderSide: BorderSide(color: color, width: 0)),
prefixIcon: prefixIcon,
suffixIcon: suffixIcon,
),
validator: validator,
// validator: FormValidator().validateEmail,
onSaved:
onSave, /*onSaved: (String value) {
_loginData.email = value;
},*/
);
}
This example might be helpful:
TextFormField(
decoration: InputDecoration(
errorStyle: TextStyle(
color: Colors.white,
)
),
),