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,
),
Related
how do i center this? You can see the Code next to the Emulator
Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(
width: 120,
child: TextFormField(
inputFormatters: [
LengthLimitingTextInputFormatter(3),
],
textAlign: TextAlign.center,
keyboardType: TextInputType.number,
style: const TextStyle(
fontSize: 16,
),
autovalidateMode: AutovalidateMode.disabled,
controller: controller,
cursorColor: Colors.white,
decoration: InputDecoration(
alignLabelWithHint: true,
floatingLabelAlignment: FloatingLabelAlignment.center,
labelText: lableText,
labelStyle: const TextStyle(fontSize: 16)),
),
),
),
I added code Snippet so you can see it better :)
you can do like this
SizedBox(
width: 120,
child: TextField(
textAlign: TextAlign.center,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'In jahren',
),
),)
delete labelText and Use label instead.
give Text to label as child.
wrap label with Center.
TextFormField(
.
.
.
label: Center(
child: Text('In jahren'),
),//center
),//TextFormField
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),
],
),
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
The cursor and text in this picture are what I want
But my demo looks like this.The text and cursor are small and not filled
code
TextEditingController searchController = new TextEditingController();
FocusNode focusNode = new FocusNode();
// other code
TextField(
key: Key("buy_subject_input"),
autofocus: true,
focusNode: focusNode,
cursorColor: Colours.default_color,
decoration: InputDecoration(
hintText: 'search',
hintStyle: TextStyle(
color: Colours.hint_text_color,
),
prefixIcon: Icon(
Icons.search,
color: Colours.hint_text_color,
),
fillColor: Colors.white,
filled: true,
border: UnderlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(Radius.circular(5)),
),
),
controller: searchController,
),
//other code
After searching everywhere I finally found it you have to give style: TextStyle(
height: 2.0,
), to increase the cursor height.
TextField(
style: TextStyle(
height: 2.0,
),
decoration: InputDecoration(
isDense: true,
contentPadding: EdgeInsets.only(
bottom: 15, top: 15, left: 10, right: 10)),
),
TextField(
cursorHeight: 20, // you can put your ideal height here
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'How to Change Cursor Height'
You can use the CupertinoTextField to achieve this:
CupertinoTextField(
prefix: Icon(Icons.search),
placeholder: 'search',
),
you can play around with input decoration parameter content-padding to reduce space between The limit area of text field and the TextForm style parameter to make text and cursor height bigger.
here is a code sample
TextEditingController searchController =
new TextEditingController(text: 'search');
FocusNode focusNode = new FocusNode();
#override
Widget build(BuildContext context) {
return TextField(
key: Key("buy_subject_input"),
autofocus: true,
focusNode: focusNode,
cursorColor: Colors.black,
style: TextStyle(fontSize: 22, height: 2.0),
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 2.0),
hintStyle: TextStyle(
color: Colors.black,
),
prefixIcon: Icon(
Icons.search,
color: Colors.black,
),
fillColor: Colors.white,
filled: true,
border: UnderlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(Radius.circular(5)),
),
),
controller: searchController,
);
}
}
you can also use the cupertino text field to have same layout as iphone
code sample
CupertinoTextField(
prefix: Icon(Icons.search),
placeholder: 'search',
);
I am trying to build a login page using Flutter.
I want to have both hintText and labelText shown for the TextFormField that I'm using, however it only shows me the labelText when I click on the field, that is, when the cursor is in that field.
TextFormField(
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(
hintText: 'example#email.com',
labelText: 'Username',
labelStyle: TextStyle(
color: Colors.blue,
),
),
);
How it looks initially:
How it looks after I click on it:
You set autofocus: true to to make it focused immediately after the page has been rendered.
TextFormField(
autofocus: true,
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(
hintText: 'example#email.com',
labelText: 'Username',
labelStyle: TextStyle(
color: Colors.blue,
),
),
);
There is no default properties for it but you can try with this.
return Scaffold(
body: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CustomTextFormField(
labelText: 'EMAIL',
hintText: 'example#email.com',
keyboardType: TextInputType.emailAddress,
validator: (t) {},
),
SizedBox(height: 8),
CustomTextFormField(
labelText: 'PASSWORD',
hintText: 'Enter password',
keyboardType: TextInputType.emailAddress,
validator: (t) {},
),
SizedBox(height: 8),
CustomTextFormField(
labelText: 'NAME',
hintText: 'Enter name',
keyboardType: TextInputType.emailAddress,
validator: (t) {},
)
],
),
)));
Created on CustomTextFormField class
import 'package:flutter/material.dart';
class CustomTextFormField extends StatelessWidget {
final TextEditingController controller;
final String hintText;
final String labelText;
final TextInputType keyboardType;
final Function(String) validator;
final bool obscureText;
CustomTextFormField({
this.controller,
this.hintText,
this.labelText,
this.keyboardType,
this.validator,
this.obscureText: false
});
#override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Theme(
data: theme.copyWith(
primaryColor: theme.accentColor,
hintColor: Colors.grey[500],
errorColor: Colors.redAccent,
),
child: Container(
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
labelText,
textAlign: TextAlign.start,
style: TextStyle(
color: theme.primaryColor,
fontWeight: FontWeight.bold,
fontSize: 15
)
),
TextFormField(
cursorColor: Colors.grey[850],
style: TextStyle(color: Colors.grey[850]),
controller: controller,
decoration: InputDecoration(
hintText: hintText,
enabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: theme.primaryColor)),
focusedBorder: UnderlineInputBorder(borderSide: BorderSide(color: theme.primaryColor)),
disabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: theme.primaryColor))
),
keyboardType: keyboardType,
obscureText: obscureText,
validator: validator,
),
],
),
)
);
}
}
Add floatingLabelBehavior: FloatingLabelBehavior.always to InputDecoration
enter image description here