Align labelText of TextField when focused - flutter

The labelText property
https://api.flutter.dev/flutter/material/InputDecoration/labelText.html
is moved upwards when the textfield is focused and you have an Inputdecoration like this:
InputDecoration(
labelText: labelText,
filled: true,
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: ImpexColors.grey,
width: 1.0,
),
),
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: ImpexColors.secondaryColor,
width: 2.0,
),
),
labelStyle: TextStyle(
color: ImpexColors.blue,
),
)
It is then aligned within the middle of the upper border.
Is there a possibility to change the position of the labelText, when the TextField is focused?

It's possible, kida?
FocusNode myFocusNode = FocusNode();
...
TextFormField(
focusNode: myFocusNode,
decoration: InputDecoration(
labelStyle: TextStyle(
height: myFocusNode.hasFocus ? 0.1 : 1, // 0,1 - label will sit on top of border
),
labelText: labelText,
...
));
would also have to add:
onTap: () => setState(() {FocusScope.of(context).requestFocus(myFocusNode);})

Related

Flutter "Hint Text" Animation

i would like to animtate "Hint text" in "InputDecoration". I found "Animated_text_kit" which alove to animate text, but i don't know how to implement this in inputDecoration - Hint text
Here is my InputDecoration Code:
decoration: InputDecoration(
enabledBorder: inputBorderDecoration,
alignLabelWithHint: true,
focusedBorder: focusedBorder,
suffixIcon: isWriting
? null
: IconButton(
icon: Image.asset('assets/images/microphone.png'),
color: Colors.black,
onPressed: () {
print('microphone taped');
}),
hintText: "Message...",
hintStyle: TextStyle(color: AppColor.darkGrey, fontSize: 18),
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.red.withOpacity(0.2),
),
borderRadius: BorderRadius.circular(15),
),
contentPadding:
EdgeInsets.symmetric(horizontal: 10, vertical: 0),
filled: true,
fillColor: Colors.white.withOpacity(0.3),
),

How to position the labelText inside a textfield?

I would like to move the label text to the topLeft position.
From this, to this.
Code:
Container buildPendingPageTextField() {
return Container(
alignment: Alignment.topLeft,
width: MediaQuery.of(context).size.width,
child: TextField(
textAlign: TextAlign.start,
controller: attendanceBloc.refusedReasonController,
textInputAction: TextInputAction.done,
maxLines: 3,
minLines: 3,
scrollPhysics: ScrollPhysics(),
style: TextStyle(
height: 1,
fontSize: 15,
letterSpacing: -0.5,
),
decoration: InputDecoration(
errorText: attendanceBloc.error.value,
floatingLabelBehavior: FloatingLabelBehavior.never,
labelText: "Descreva aqui o motivo",
labelStyle: TextStyle(),
border: const OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey,
width: 0.3,
),
),
),
),
);
}
I have tried aligment on the container. on the inputDecoration, and even on textStyle, no success.
alignLabelWithHint property
Typically set to true when the InputDecorator contains a multiline TextField (TextField.maxLines is null or > 1) to override the default behavior of aligning the label with the center of the TextField.
You should set alignLabelWithHint to true to override the default behavior of aligning the label with the center:
decoration: InputDecoration(
errorText: attendanceBloc.error.value,
floatingLabelBehavior: FloatingLabelBehavior.never,
labelText: "Descreva aqui o motivo",
labelStyle: TextStyle(),
alignLabelWithHint: true, // Insert this line
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey,
width: 0.3,
),
),
),
lableText shouldn't be used for such a situation,
instead, use hintText to do it with less code
decoration: InputDecoration(
hintText: 'Descreva aqui o motivo',
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey,
width: 0.3,
),
),
),

How to hide the bottom edge on TextField?

How to hide the bottom edge on TextField?
TextField(
decoration: InputDecoration(
hintText: '(201) 555-0123',
),
You can use the *border parameters of TextField:
TextField(
decoration: InputDecoration(
hintText: '(201) 555-0123',
// Hides the border when the TextField is enabled
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.transparent),
),
// Hides the border when you click the TextField
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.transparent),
),
// Hides the border when the TextField is disabled
disabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.transparent),
),
),
)
How are you?
You can set the border of InputDecoration to InputBorder.none.
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: S.of(context).textHere,
hintStyle: TextStyle(fontSize: 12),
),
minLines: 5,
maxLines: null,
controller: _content,
keyboardType: TextInputType.multiline,
textCapitalization: TextCapitalization.sentences,
),
https://i.stack.imgur.com/trK69.png

How to change the inside color of a Textfield in flutter?

I want to change the Color of an textfield in flutter.
Means the area within the InputBorder.
I tried to wrap the textfield in container, and change the color of the container, but this seems a fool's step to solve the problem.
I tried the fillColor option, but nothing changed.
Here is my Code -->
Container(
padding: EdgeInsets.fromLTRB(
10,
screenHeight * 0.016415869,
10,
0,
),
height: screenHeight * 0.082079343,
child: TextField(
cursorColor: Color(0xFF7675E0),
textAlign: TextAlign.left,
decoration: InputDecoration(
fillColor: Colors.black, //Nothing Worked
contentPadding: EdgeInsets.fromLTRB(
15,
screenHeight * 0.002735978,
2,
screenHeight * 0.002735978,
),
hintText: "Search",
hintStyle: GoogleFonts.sniglet(
fontSize: screenHeight * 0.020519836,
color: Color(0xFFC6C8CA),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
borderSide: BorderSide(
color: Colors.grey[200].withOpacity(0.2),
),
),
prefixIcon: Icon(Icons.search),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey[200].withOpacity(1),
),
),
),
),
),
Thanks in Advance :)
Try out this:-
decoration: InputDecoration(
filled: true,
fillColor: Colors.grey,
If you want to set the color to your text field then there is one boolean variable which you need to set true so your color will be added to your text field
Container(
child: TextField(
cursorColor: Color(0xFF7675E0),
textAlign: TextAlign.left,
decoration: InputDecoration(
fillColor: Colors.black, // you can change color of textfield
filled: true, // this should be true if you want to set color to textfield
hintText: "Search",
prefixIcon: Icon(Icons.search),
),
),
),
Basically you can leave your code as you provided us. The important missing value is
filled: true
With this value applied you can style the background color easy.
Reference to the doc: https://api.flutter.dev/flutter/material/TextField-class.html
don't use Container for TextField decoration, there are more property into TextField
TextField use
decoration: InputDecoration(
filled: true,
fillColor: Colors.white10,
border: new OutlineInputBorder(
borderRadius: new BorderRadius.all(
new Radius.circular(14.0),
),
),
TextField(
controller: usernameController,
keyboardType: TextInputType.emailAddress,
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
filled: true,
fillColor: Colors.white10,
border: new OutlineInputBorder(
borderRadius: new BorderRadius.all(
new Radius.circular(14.0),
),
),
hintText: 'Username',
hintStyle: TextStyle(color: Colors.white),
contentPadding: const EdgeInsets.all(24),
prefixIcon: Icon(Icons.person, color: Colors.white),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white10),
borderRadius: BorderRadius.circular(14),
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white10),
borderRadius: BorderRadius.circular(14),
),
),
),

Align hint text in multiline TextField in Flutter

I have a multiline TextField with a prefixIcon, so now the Icon is in the center vertically and hint text is at top left. I want both of them to be aligned, either at the top or in the center vertically.
The code used is
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: TextField(
maxLines: 3,
decoration: InputDecoration(
hintText: 'Bio',
prefixIcon: Icon(Icons.chrome_reader_mode),
fillColor: Colors.grey[200],
filled: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: BorderSide(),
),
),
),
),
Although in your particular case problem can be solved by adding a newline character (hintText: '\nBio'), a better solution is to use labelText property instead of a hintText. By default label is aligned with the center of the TextField.
TextField(
maxLines: 3,
decoration: InputDecoration(
labelText: 'Bio',
prefixIcon: Icon(Icons.chrome_reader_mode),
fillColor: Colors.grey[200],
filled: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: BorderSide(),
),
),
),
This is an ugly solution, but it works. Other solutions (e.g., setting the textAlignVertical to TextAlignVertical.center) don't work since the maxLines is not null.
Add a TextStyle to the hintText and set the height of the TextStyle to 2.8. You need to reduce this when the fontSize is bigger because the height will be multiplied by the fontSize to make the line height. I added a contentPadding to make sure the text doesn't overflow(because now the text begins from the center of the TextField).
TextField(
maxLines: 3,
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 30),
hintText: 'Bio',
hintStyle: TextStyle(
height: 2.8,
),
prefixIcon: Icon(Icons.chrome_reader_mode),
fillColor: Colors.grey[200],
filled: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: BorderSide(),
),
),
),
Result:
Widget customTextView(String hint, Icon iconName) {
return Container(
decoration: BoxDecoration(
color: _DarkWhiteColor.withOpacity(0.5),
borderRadius: BorderRadius.circular(5),
),
child: Padding(
padding: const EdgeInsets.only(left: 0, right: 0),
child: TextField(
style: TextStyle(color: Colors.grey),
cursorColor: Colors.grey,
decoration: InputDecoration(
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
fillColor: _DarkWhiteColor.withOpacity(0),
filled: true,
hintText: "Enter Your Message",
labelStyle: TextStyle(color: Colors.white),
// suffixIcon: iconName,
prefixIcon: Container(
transform: Matrix4.translationValues(0, -45, 0),
child: iconName,
),
),
maxLines: 6,
),
),
);
}