Title should say everything. Nevertheless, here are some pictures for better understanding.
How it should look like (Google Material Components):
How it actually looks like:
But please ignore the bottom border of the pic "how it should look like". The problem is just that the label is outside of the background.
And also the Text and LabelText is not vertically centered. Another picture:
I also tried playing around with the paddings (top and bottom) but either it didn't change anything or I got an error.
And here is the source code:
return TextField(
onChanged: (String? value) {
print(value);
onChanged(value);
},
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
prefixIcon: prefixIcon,
labelText: labelText,
labelStyle: TextStyle(
fontWeight: FontWeight.w700,
color: kInputColor,
fontSize: 14.0,
),
filled: true,
fillColor: kInputFillColor,
contentPadding: EdgeInsets.only(
top: 14.0,
bottom: 14.0,
left: 14.0,
right: 14.0,
),
border: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(6.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(6.0),
),
),
cursorWidth: 1.5,
style: TextStyle(
fontWeight: FontWeight.w600,
color: Colors.black,
fontSize: 14.0,
),
);
The problem was the type of border I used. Through the answer of #Diwyansh, I found out that the default border a TextField uses is the UnderlineInputBorder. I found out that this type of border also has the properties to set borderRadius and borderSide. So when I use UnderlineInputBorder instead of OutlineInputBorder (which for me sounded more meaningful than UnderlineInputBorder), the label is drawn within the background of the TextField. This is how my source code looks now:
return TextField(
onChanged: (String? value) {
print(value);
onChanged(value);
},
decoration: InputDecoration(
prefixIcon: prefixIcon,
labelText: labelText,
labelStyle: TextStyle(
fontWeight: FontWeight.w700,
color: kInputColor,
fontSize: 14.0,
),
filled: true,
fillColor: kInputFillColor,
contentPadding: EdgeInsets.only(
top: 14.0,
bottom: 12.0,
left: 14.0,
right: 14.0,
),
border: UnderlineInputBorder(
borderRadius: BorderRadius.circular(6.0),
borderSide: BorderSide.none,
),
),
cursorWidth: 1.5,
style: TextStyle(
fontWeight: FontWeight.w600,
color: Colors.black,
fontSize: 14.0,
),
);
You can get it InputDecoration Property
TextFormField(
decoration: InputDecoration(
labelText: "Label",
filled: true),
)
According to The Official Flutter Documentation of OutlineInputBorder https://api.flutter.dev/flutter/material/OutlineInputBorder/OutlineInputBorder.html
InputDecoration.floatingLabelBehavior, which should be set to FloatingLabelBehavior.never when the borderSide is BorderSide.none. If let as FloatingLabelBehavior.auto, the label will extend beyond the container as if the border were still being drawn.
Which means if BorderSide is set to BorderSide.none, just like you did, the label would look outside the background container when focused.
so you just need to set floatingLabelBehavior to FloatingLabelBehavior.never.
which would make it work like hintText
Related
It is necessary to move the cursor to the end of the text, but in flutter this is strangely implemented through text selection, I do it like this
textController.selection =
TextSelection.collapsed(offset: textController.text.length);
it works, but when typing, it appears underlined
It is possible to somehow remove the underlining of the text, I read the documentation, but did not find it.
My TextFormField
TextFormField(
cursorColor: Colors.white,
style: const TextStyle(
color: Colors.white,
fontSize: 20,
),
controller: textController,
autofocus: false,
decoration: const InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white)),
filled: true,
isDense: true,
hintText:
'search',
hintStyle: TextStyle(
//Style of hintText
color: Colors.white60,
fontSize: 20,
),
),
),
This might help you:
TextField(
style: const TextStyle(
decoration: TextDecoration.none),
),
I build the widget with TextField but error text is shows inside the field.
The widget is:
child: TextField(
maxLines: 1,
controller: textEditingController,
style: TextStyle(
fontFamily: ConstantData.fontFamily,
color: ConstantData.textColor,
fontWeight: FontWeight.w400,
fontSize: fontSize),
decoration: InputDecoration(
contentPadding: EdgeInsets.only(
left: ConstantWidget.getWidthPercentSize(context, 2)),
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
hintText: 'name',
errorText: 'errorText',
hintStyle: TextStyle(
fontFamily: ConstantData.fontFamily,
color: ConstantData.textColor,
fontWeight: FontWeight.w400,
fontSize: fontSize)),
),
Where is my mistake to show the text inside of field, not below them?
Thanks a lot.
I think you have wrapped the TextField with a Container of grey color.
Please remove it and add fillColor and filled properties to the InputDecoration() to color the TextField.
For example:
decoration: InputDecoration(
...
filled: true,
fillColor: Colors.grey, // Any color you want
...
)
I can align the hint text to the center with no issues, but when the user starts typing into the textbox, I can't seem to get it to align to the center.
When maxLines are set to 1, there isn't an issue, but when it is set to more than 1 (or to null in my case), then it seems to align to the top by default.
Screenshot1
Screenshot2
Is there any way to correct this?
Container(
width: screenWidth / 1.2,
height: 120,
padding:
EdgeInsets.symmetric(horizontal: 0),
child: TextField(
// autofocus: false,
controller: postText,
keyboardType: TextInputType.text,
maxLines: 10,
textAlign: TextAlign.center,
textAlignVertical:
TextAlignVertical.center,
style: TextStyle(
fontFamily: 'Michroma',
color: selectedFont,
fontSize: 12, // 12
),
decoration: InputDecoration(
hintText: '\n\nType away ! :\n\n\n',
hintStyle: TextStyle(
color: fontColour,
fontFamily: 'Michroma',
fontWeight: FontWeight.bold,
fontSize: 12,
),
filled: true,
fillColor: screenColour,
contentPadding: EdgeInsets.symmetric(
vertical: 8.0,
horizontal: 10.0,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(20.0),
), // 32
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: borderColour,
width: 1.0,
),
borderRadius: BorderRadius.all(
Radius.circular(20.0),
), // 32
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: borderColour,
width: 2.0,
),
borderRadius: BorderRadius.all(
Radius.circular(20.0),
), // 32
),
),
),
),
You need to delete \n\n expression in front of the hint text value because \n provides us to switch to a new line.
In your case, you switch the line you in two times. That is why the hint text is not appearing where you type.
decoration: InputDecoration(
hintText: 'Type away ! :', <--- here
hintStyle: TextStyle(
color: fontColour,
fontFamily: 'Michroma',
fontWeight: FontWeight.bold,
fontSize: 12),
When you remove that expression, probably it will not be in the center. so you can center it by doing following;
child: TextField(
controller: postTextController,
keyboardType: TextInputType.multiline,
textAlign: TextAlign.center,
maxLines: null,
expands: true,
textAlignVertical: TextAlignVertical.center,
style: TextStyle(
fontFamily: 'Michroma',
fontSize: 12),
decoration: InputDecoration(
hintText: 'Type away ! :',
hintStyle: TextStyle(
fontFamily: 'Michroma',
fontWeight: FontWeight.bold,
fontSize: 12),
Tested on Both physical device and simulator.
As we all know textSelectionHandleColor is deprecated. So, I decided to make some changes in my app code, unfortunately, selectionHandleColor is still default blue color we get, when we create new app.
TextSelectionTheme(
data: TextSelectionThemeData(
cursorColor: kLightGreen,
selectionHandleColor: kLightGreen,
selectionColor: kGrey),
child: TextField(
keyboardType: TextInputType.number,
style: TextStyle(
color: kOffWhite,
letterSpacing: 1.5,
),
decoration: InputDecoration(
labelText: 'Label',
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: kLightGreen),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: kLightGreen),
),
hintText: 'Hint',
hintStyle: TextStyle(
color: kOffWhite,
letterSpacing: 1.5,
),
labelStyle: TextStyle(
color: kLightGreen,
letterSpacing: 1.5,
),
),
),
),
Cursor color, selection color got changed except handle color. I have tried every possible way to change text selection handle color.
As said in the title; my text "disappears" when I added text too much against the length of the textfield, why does this happens??
Here's the code
Container(
height: mediaSize.height * .075,
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(12.5)),
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.black54.withOpacity(0.45),
spreadRadius: 1,
blurRadius: 4,
offset: Offset(3.5, 4))
]),
child: TextFormField(
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: myLightOrangeColor),
borderRadius: BorderRadius.all(
Radius.circular(12.5))),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: myLightOrangeColor, width: 6),
borderRadius: BorderRadius.all(
Radius.circular(12.5))),
labelStyle: TextStyle(color: Colors.black, fontSize: 15, fontWeight: FontWeight.bold),
filled: true,
fillColor: Colors.white),
keyboardType: TextInputType.text,
style: TextStyle(color: Colors.black, fontSize: 15, fontWeight: FontWeight.bold),
),
),
When I add to much of text this happens: [first one ok] [next one ???]
For the text of the TextField to appear normally it needs his normal height, in the image below an image without giving height to the Container:
But if you give it less height than it need to show the text this happen (in the example the height of the device multiplied by 0.075):
To reduce the height of the TextField you can change the property contentPadding or set the isDense to true:
TextFormField(
decoration: InputDecoration(
isDense: true,
//contentPadding: EdgeInsets.all(0), //or any padding you want
),
keyboardType: TextInputType.text,
style: TextStyle(
color: Colors.black,
fontSize: 15,
fontWeight: FontWeight.bold,
),
),