Fixed space between two TextFormField on Flutter - flutter

I'm trying to create an app for my work with login and signup form.
The page is ok, but there is a litte problem: on each TextFormField there is a validator for check the field, but when appears the error message below the field, the other fields goes down.
The question is: how can I fix the textfields?
This is a gif demostration: https://ibb.co/VwNn2f0
I try to use padding or other method finded here or on google, but I didn't find a solution.

you could wrap your TextField with a SizedBox and set a height for it :
SizedBox(
height: 80,
child: TextFormField(
validator: myValidator,
),
),
or you can also set a style for the error text, with a 0 height :
TextField(
decoration: InputDecoration(
errorStyle: TextStyle(height: 0),
),
);

Related

Flutter 2: maxLength inside the textfield without using onChanged

I am trying to find a way to use maxLength and put it inside of the textfield to have a nice rendering. However, I don't want to use the onChanged parameter because I would have to use a setState, but this not the good way to use it.
The parameter maxLength from the flutter's textfield is designed to do not update the state, so that every statefull widgets don't have to rebuild.
So I would like to put the 0/50 inside, at the end of the textfield, on the same line as Title.
From this :
To this :
So somebody have an idea of how to do it without using the parameter onChanged ?
Does flutter has implemented this functionality ?
Try with this, hope you get a solution.
TextField(
maxLength: 250,
buildCounter: (_, {currentLength, maxLength, isFocused}) => Padding(
padding: const EdgeInsets.only(left: 16.0),
child: Container(
alignment: Alignment.centerLeft,
child: Text(currentLength.toString() + "/" + maxLength.toString())),
),
)
Also, Try with this,
TextField(
controller: nameController,
maxLength: 60,
prefixIcon: Icon(
Icons.account_circle,
color: Colors.black45,
),
),

How to create a custom height TextFormField in flutter?

I have a form where there are two TextFormField and one bottom button to validate it but the problem is that the screen is looking awkward. I want the **TextFormField ** the bottom one two cove the area between them completely even if there is no content and become scrollable once it reaches the bottom.
the left one is what I have, but I want the right one, I tried to wrap it with Expandable or Container but it didn't work because I was using border property.
You can achieve this by wrapping the TexFormField widget with an Expanded widget and setting the expands and maxLines property of the TextFormField to true and null respectively.
The TextFormField with take all the available space irrespective of the screen size, check the code provided below for an example:
// wrap it with an Expanded widget
Expanded(
child: TextFormField(
// set the keyboard type to multiline f
keyboardType: TextInputType.multiline,
// set maxlines to null
maxLines: null,
// set expands to true
expands: true,
decoration: InputDecoration(
isDense: true,
border: OutlineInputBorder(
borderSide: BorderSide(color: Colors.black)
)
),
),
),
You just have to increase the maxLines inside the TextFormField to increase it's size like this:
Container(
height: MediaQuery.of(context).size.height*0.5,//set the size according to your choice
decoration: BoxDecoration(
//Customize your container
color: Colors.red,
borderRadius: BorderRadius.all(Radius.circular(10.0)),
),
child: TextFormField(
maxLines: 30,//add any number of lines
//Customize your text field
)
),

Flutter TextField input to not disappear after save/done

how can i make the user input to stay and not disappear after users done so next time the user want to update the text they get to clear the text fileds manually!
Padding(
padding: EdgeInsets.symmetric(horizontal: 30.0),
child: TextField(
style: TextStyle(fontSize: 18.0),
decoration: InputDecoration(
labelText: 'option',
),
onChanged: (input) => _alOne2 = input,
),
)
best regards
Solved the issue by changing TextField to TextFormField and using its initial value .i am using several fields in the same page and in that case had to create a Controller for each field.

How to show last line of one TextFormField with fixed number of lines in Flutter

I have a TextFormField with a fixed number of lines and when a screen load, this TextFormField already have more lines then a fixed number of lines that i defined,
and if i want to see the last line,
I need manually scroll it to the last line.
Is there a way to show automatically the last line of this TextFormField?
Container(
child: SingleChildScrollView(
child: TextField(
readOnly: true,
keyboardType: TextInputType.multiline,
maxLines: 9,
controller: chatController,
decoration: InputDecoration(
hintText: "Chat...",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(15.0),
),
),
),
),
),
This appears to be a missing feature in the Flutter Framework, Someone has filed a bug to get it resolved: https://github.com/flutter/flutter/issues/9365
However site includes a work around. Please have a look at it.
make this public variable:
ScrollController scrollController = new ScrollController();
attach it to the TextField
TextField(
//...
scrollController: scrollController,
//...
),
put this into your initState function:
WidgetsBinding.instance.addPostFrameCallback((_) async {
scrollController.jumpTo(double.parse((9 * 17 * 1.9 /*number of lines * fontSize * 1.9 (you can of course experiment with this values)*/).toStringAsFixed(1)));
});
i hope it helped

TextField with animated hint / label

I want to implement a form containing TextFields. Each field has a label / hint. I want the hint to animate and become a label when the user starts typing. This is a standard Material design pattern, so I expected it to be implemented by the standard Widgets.
Something like this:
It turns out to be very simple.
InputDecoration has a labelText parameter, which does what I wanted.
E.g.
TextField(decoration: InputDecoration(labelText: 'Full name')),
In Flutter, both hint and label are behaving in two different way that hintText will be shown as fixed but the labelText will be(double acting) shown as hint which is animating to the top when the cursor is getting focused.
TextField(decoration: InputDecoration
(
labelText: "Animatable hint",
hintText: "Inanimate hint"
)
)
Difference between labelText and HintText.
labelText : Shows label top of the input field, if it's empty or unfocused. When it get focus, labelText moves to above the input field.
hintText: just shows hint to the user.
TextField(decoration: InputDecoration(labelText: 'labelText'),),
TextField(decoration: InputDecoration(hintText: 'hintText'),),
TextField(decoration:InputDecoration(hintText: 'both', labelText: 'both'),),
more information - TextFormField placeholder
Also it's a good way to make your own Method or widget.(So you can reuse code later)
Example:
//your generator method or you can make your own widget class if you want that.
Widget _entryField(String title, {bool isPassword = false}) {
return Container(
margin: EdgeInsets.symmetric(vertical: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
title,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15),
),
SizedBox(
height: 10,
),
TextField(
obscureText: isPassword,
decoration: InputDecoration(
//labelText: title , // you can change this with the top text like you want
hintText: "Please enter your $title" ,
border: InputBorder.none,
fillColor: Color(0xfff3f3f4),
filled: true))
],
),
);
}
==============
Edit:
As mentioned by #Evin1_ below.
After reading this article Splitting widgets to methods is a performance antipattern/Iiro Krankka
I found it's better to use StatelessWidget to split your code and functions only for doing Operations.
the reason:
This way, you won’t be rebuilding your static widget trees multiple times for nothing but wasted CPU cycles.
If you really prefer building your widget trees with methods, you might want to take a look at a package called functional_widget by Remi Rousselet.
Also others comments for more information about this topic here difference between functions and classes to create reusable widgets