can I set the position of text inside textfield in Flutter? - flutter

I have code like this
Stack(
children: [
TextFormField(
autofocus: true,
autocorrect: false,
maxLines: 1,
textAlign: TextAlign.center,
),
Positioned(
bottom: 14,
left: 0,
child: Text("Rp"),
),
],
),
I know I can set the text in the center using TextAlign.center, but now I want to make the text inside textfield at the start + around 24 pt from start
I can't use TextAlign.start since it will overlap the 'Rp' text.
how to do that?

Use Prefix or Suffix for the "Rp" text:
TextFormField(
decoration: InputDecoration(
prefix: ...,
suffix: ...,)
Then you can use textAlign

Try below code hope its helpful to you you can use prefix propery inside Inputdecoration of TextFormField here and InputDecoration here
TextFormField(
autofocus: true,
autocorrect: false,
maxLines: 1,
textAlign: TextAlign.center,
decoration: InputDecoration(
prefix: Text('Rp'),
),
),
Your result screen->

TextFormField(
decoration: InputDecoration(
prefixText: 'Rp',
),
),

Related

How to reduce Space between textfield and the input text in flutter

I am developing this textfield and for some reason the text is having some space below it. Please let me know the solution to this.
This is my code for textfield.
Expanded(
child: Container(
height: 40,
child: TextField(
keyboardType: TextInputType.number,
controller: textEditingController,
style: TextStyle(
fontSize: 14, height: 0, color: Colors.white),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
decoration: InputDecoration(
// isDense: true,
// errorText: validateQtyTxt.toString(),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10)),
labelText: 'QTY > '
// hintText: 'Enter a search term',
),
onChanged: (String newValue) {
(text) => setState(() => _text);
print(newValue);
int qty;
derivativeScanController.buttonClick(newValue, qty);
},
),
),
),
This is the image for reference
enter image description here
There is a property called contentPadding which takes EdgeInsetsGeometry type value with that you can reduce the spacing between textfield and actual text
sample code .
contentPadding: EdgeInsets.zero,
if you want to remove all the spacing change the isDense property to true also
isDense: true,
all the space will be removed
You can use the contentPadding in the decoration property of the TextField to edit its padding.
https://api.flutter.dev/flutter/material/InputDecoration-class.html
TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.only(...),
),
)
Which you can fill with whatever values you want. You can use things like EdgeInsets.zero or EdgeInsets.symmetric too:
https://api.flutter.dev/flutter/painting/EdgeInsets-class.html
Change the textStyle from 0 to 1 and adjust the text using padding.

How to start text in the left top corner in TextFormField

I have TextFormField and I increased height of this field. I would like to start text from the left top corner and make that text will break at the end of the line. So if someone will put a lot of text, field will have X lines not only one.
How it looks now:
How it should looks like
Code:
TextFormField(
controller: contentController,
decoration: InputDecoration(
contentPadding: new EdgeInsets.symmetric(
vertical: MediaQuery.of(context).size.height * .10,
horizontal: 10.0),
border: OutlineInputBorder(),
hintText: 'Treść',
fillColor: Color(0xffffffff),
filled: true,
prefixIcon: Icon(Icons.topic_rounded),
),
validator: (value) {
if (value == null || value.isEmpty) {
return ErrorMessages.NO_CONTENT_MESSAGE;
}
},
),
There are a few things you can do:
You can add maxLines: desiredNumberOfLines and textAlignVertical: TextAlignVertical.top, to your TextFormField and wrap your prefix icon with Padding like this:
TextFormField(
textAlignVertical: TextAlignVertical.top,
decoration: InputDecoration(
prefixIcon: Padding(
padding: const EdgeInsets.only(bottom: 70.0),
child: Icon(
Icons.topic_rounded,
),
),
...
),
maxLines: 5,
...
),
You can wrap your TextFormField with a SizedBox widget and add expands: true to the text field, also you have to set minLines and minLines to null, because otherwise you get an error. The icon alignment can be the same as in the previous example with wrapping the icon with a Padding widget or you can do it the other way, which is shown in this example. It is done by using a prefix parameter instead of prefixIcon like this:
SizedBox(
height: 200,
child: TextFormField(
textAlignVertical: TextAlignVertical.top,
expands: true,
maxLines: null,
minLines: null,
decoration: InputDecoration(
prefix: Icon(
Icons.topic_rounded,
),
...
),
...
),
)
Choose a combination of text and icon alignment methods that works best for you.
You need to specify the height of your prefix icon by default it will be in the center. But to make it go on top you need to add Container() in which you will add your icon like this.
TextFormField(
minLines: 4,
keyboardType: TextInputType.multiline,
maxLines: 4,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Treść',
fillColor: Color(0xffffffff),
filled: true,
prefixIcon: Container(
width: 20,
height: 100,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(Icons.topic_rounded),
),
Container(),
],
),
),
prefixIconConstraints: BoxConstraints(
minWidth: 35,
minHeight: 0,
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return "error";
}
return null;
},
)
It will look like this

Flutter TextFormField text crops when width exceeds

I am using TextFormField for my flutter application search field.
When i type a text bigger that the width of textformfield, then entered text it cropped to half an not visible completely.
Please find my below code and attached image for more understanding of problem.
TextFormField(
focusNode: focusNode,
controller: textController,
decoration: InputDecoration(
border: InputBorder.none,
icon: Icon(
Icons.search,
),
hintText: AppStrings.searchCatHint,
hintStyle: TextStyle(
fontSize: 17,),
),
autovalidate: true,
autocorrect: false,
autofocus: true,
);
Screen for problem:
Similar problem i have seen in another question also, but no solution provided.
Similar issue link
Text display proper with #CarlosSR suggestion. But alignment issue as below.
I found a solution for the issue.
Problem description: On entering new text, old text should move to left without any problem.
Solution: I have specified a height for my TextFormField with Wrapping with Container. Code looks like below.
Container(
height:40,
child: TextFormFiled(...),
);
This worked for me. Thanks a lot for all your responses.
wrapping textformfield with container:
error will not occur:
Container(
color: Colors.black12,
width: 280,
height: 45,
child: TextFormField(
decoration: InputDecoration(
border: InputBorder.none,
icon: Icon(
Icons.search,
),
hintStyle: TextStyle(
fontSize: 17,),
),
autovalidate: true,
autocorrect: false,
autofocus: true,
),
),
error is with padding I guess if you are using:
Container(
color: Colors.black12,
width: 280,
height: 45,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
decoration: InputDecoration(
border: InputBorder.none,
icon: Icon(
Icons.search,
),
hintStyle: TextStyle(
fontSize: 17,),
),
autovalidate: true,
autocorrect: false,
autofocus: true,
),
),
),
This way you can only scroll the widget in horizontal axis, plus you add a padding to get your text in the position you want. In other, I recommend you to check for Align widget.
SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
focusNode: focusNode,
controller: textController,
decoration: InputDecoration(
border: InputBorder.none,
icon: Icon(
Icons.search,
),
hintText: AppStrings.searchCatHint,
hintStyle: TextStyle(
fontSize: 17,),
),
autovalidate: true,
autocorrect: false,
autofocus: true,
),
),
),
I too happend to have the exact issue.The thing that i noticed that when i have a textfield in an container of some hardcoded height you might encounter this issue.Try removing the height of the container or just remove the textfields bottom padding.This will help.
The solution to this is using the CupertinoTextField widget. You can read about it here.

How scroll over a disabled TextField with fixed number of lines in Flutter?

After i set 'enable: false' i can't see the others lines if a text has more than 9 lines. I need set 'enable:false' because i don't want user edit text in this TextField. I tried use SigleChildScrollView, but it didn't work...
Container(
child: SingleChildScrollView(
child: TextField(
enabled: false,
keyboardType: TextInputType.multiline,
maxLines: 9,
controller: chatController,
decoration: InputDecoration(
hintText: "Chat...",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(15.0),
),
),
),
),
),
make readOnly: true
now you can't edit the field and still you can scroll the field.
TextFormField(
enabled: true,
readOnly: true,
initialValue: 'initial value',
)

Add mandatory(*) for labelText in Input Decoration in Flutter

I want to add asteric sign in InputDecoration labelText and change color(like red) of it so that user understand easily this field is required.
TextField(
autofocus: true,
controller: _nameCtrlr,
keyboardType: TextInputType.text,
decoration: InputDecoration(
labelText: "Name *",
))
Expected result like this image
Sample Image
You can use the Rich Text Widget like this
RichText getRequiredLabel(String fieldName) {
return RichText(
text: TextSpan(
style: TextStyle(color: Colors.black),
text: fieldName,
children: [
TextSpan(text: '*', style: TextStyle(color: Colors.red))
]));
}
You can use label arguments inside InputDecoration parameters. This work both widgets TextField or TextFormField
TextField(
decoration: InputDecoration(
label: Row(
children: [
const Text('*', style: TextStyle(color: Colors.red)),
Padding(
padding: EdgeInsets.all(3.0),
),
Text("Name")
],
),
),
controller: _nameCtrlr,
autofocus: true,
keyboardType: TextInputType.text,
);
Yes, man you can do that.
TextField(
autofocus: true,
controller: _nameCtrlr,
keyboardType: TextInputType.text,
decoration: InputDecoration(
labelText: "Name \*",
))
"\" sign will help compiler to distinguish between asterisk sign(*) from multiplication.