Flutter how to add padding in TextFormField? - flutter

I hope the title is enough to understand what i want, How to add padding and margin in TextFormField? please see picture below for more information. thanks
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset('assets/images/logo.png'),
TextFormField(
decoration: InputDecoration(
fillColor: Colors.white,
filled: true,
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
labelText: 'Enter your username'),
),
TextFormField(
decoration: InputDecoration(
fillColor: Colors.white,
filled: true,
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
labelText: 'Enter your password'),
),
],
)

You can use several methods to increase the distance from them:
SizedBox()
Padding()
Container(margin , child: ...)
1- SizedBox():
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset('assets/images/logo.png'),
TextFormField(
decoration: InputDecoration(
fillColor: Colors.white,
filled: true,
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
labelText: 'Enter your username'),
),
SizedBox(height:10), // will add 10 pixels
TextFormField(
decoration: InputDecoration(
fillColor: Colors.white,
filled: true,
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
labelText: 'Enter your password'),
),
],
)
2- Padding()
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
decoration: InputDecoration(
fillColor: Colors.white,
filled: true,
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
labelText: 'Enter your username'),
),
),
3- Container()
Container(
margin: EdgeInsets.only(top: 15),
child: TextFormField(
decoration: InputDecoration(
fillColor: Colors.white,
filled: true,
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
labelText: 'Enter your password'),
),
),

Please wrap TextFormField in the Container and set margin like as top, left, right, bottom according your ui. Please check this and If this it's correct so please tell me.
Container(
margin: EdgeInsets.only(top: 10,left: 0,right: 0,bottom: 0),
child: TextFormField(
decoration: InputDecoration(
fillColor: Colors.white,
filled: true,
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
labelText: 'Enter your password'),
),
),

To apply horizontal padding just add Padding widget above your Column:
Padding(
padding: EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('assets/images/logo.png'),
// your text fields
],
...
To add vertical padding use SizedBox inside Column:
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset('assets/images/logo.png'),
TextFormField1(...),
SizedBox(height: 24.0),
TextFormField2(...),
SizedBox(height: 24.0),
],
)

InputDecoration takes a contentPadding value. Just use that directly.

you can add SizedBox.
TextFormField(
decoration: InputDecoration(
fillColor: Colors.white,
filled: true,
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
labelText: 'Enter your username'),
),
SizedBox(height: 10),
TextFormField(
decoration: InputDecoration(
fillColor: Colors.white,
filled: true,
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
labelText: 'Enter your password'),
),

You can insert sizedbox in between the textformfield like:-
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset('assets/images/logo.png'),
TextFormField(
decoration: InputDecoration(
fillColor: Colors.white,
filled: true,
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
labelText: 'Enter your username'),
),
SizedBox(height:30), // will add 30 pixels gap in between the fields
TextFormField(
decoration: InputDecoration(
fillColor: Colors.white,
filled: true,
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
labelText: 'Enter your password'),
),
],
)

Related

Keyboard overlapping textfields

I am creating a class to edit some user fields:
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: true,
appBar: AppBar(
backgroundColor: AppColors.rojoMovMap,
title: Text("miperfil".tr()),
),
body: SingleChildScrollView(
child:Column(
children: [
Row(
children: [
Text(
"tuemail".tr(),
style: TextStyle(
color: AppColors.grisMovMap,
fontWeight: FontWeight.bold,
fontSize: 18),
),
],
),
Row(
children: [
Text(
widget.usuario.email,
style: TextStyle(color: AppColors.grisMovMap, fontSize: 16),
)
],
),
SizedBox(
height: 10,
),
SizedBox(height: 5,),
TextField(
controller: controladorUsername,
keyboardType: TextInputType.text,
maxLength: 18,
autocorrect: false,
decoration: new InputDecoration(
icon: GestureDetector(
onTap: (){
//verificar que no existe ya el username
//asignar nuevo username
},
child: new Icon(Icons.edit,color: AppColors.rojoMovMap,)),
labelText: "tuusername".tr(),
enabledBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
borderSide: const BorderSide(
color: Colors.grey,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.blue),
),
),
),
SizedBox(
height: 15,
),
TextField(
controller: controladorUbicacion,
keyboardType: TextInputType.text,
autocorrect: false,
decoration: new InputDecoration(
icon: GestureDetector(
onTap: (){
//asignar nueva ciudad,provincia
},
child: new Icon(Icons.edit,color: AppColors.rojoMovMap,)),
labelText: "tuubicacion".tr(),
enabledBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
borderSide: const BorderSide(
color: Colors.grey,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.blue),
),
),
),
SizedBox(
height: 15,
),
TextField(
controller: controladorPais,
keyboardType: TextInputType.text,
autocorrect: false,
decoration: new InputDecoration(
icon: GestureDetector(
onTap: (){
//asignar nuevo pais
},
child: new Icon(Icons.edit,color: AppColors.rojoMovMap,)),
labelText: "tupais".tr(),
enabledBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
borderSide: const BorderSide(
color: Colors.grey,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.blue),
),
),
),
SizedBox(
height: 15,
),
TextField(
controller: controladorNombre,
keyboardType: TextInputType.text,
autocorrect: false,
decoration: new InputDecoration(
icon: GestureDetector(
onTap: (){
//asignar nuevo nombre
},
child: new Icon(Icons.edit,color: AppColors.rojoMovMap,)),
labelText: "tunombre".tr(),
enabledBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
borderSide: const BorderSide(
color: Colors.grey,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.blue),
),
),
),
SizedBox(
height: 15,
),
TextField(
controller: controladorApellidos,
keyboardType: TextInputType.text,
autocorrect: false,
decoration: new InputDecoration(
icon: GestureDetector(
onTap: (){
//asignar nuevo apellidos
},
child: new Icon(Icons.edit,color: AppColors.rojoMovMap,)),
labelText: "tusapellidos".tr(),
enabledBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
borderSide: const BorderSide(
color: Colors.grey,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.blue),
),
),
),
SizedBox(
height: 15,
),
TextField(
controller: controladorApellidos,
keyboardType: TextInputType.text,
autocorrect: false,
decoration: new InputDecoration(
icon: GestureDetector(
onTap: (){
//asignar nuevo apellidos
},
child: new Icon(Icons.edit,color: AppColors.rojoMovMap,)),
labelText: "tusapellidos".tr(),
enabledBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
borderSide: const BorderSide(
color: Colors.grey,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.blue),
),
),
),
SizedBox(
height: 15,
),
TextField(
controller: controladorApellidos,
keyboardType: TextInputType.text,
autocorrect: false,
decoration: new InputDecoration(
icon: GestureDetector(
onTap: (){
//asignar nuevo apellidos
},
child: new Icon(Icons.edit,color: AppColors.rojoMovMap,)),
labelText: "tusapellidos".tr(),
enabledBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
borderSide: const BorderSide(
color: Colors.grey,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide(color: Colors.blue),
),
),
),
],
),
),
);
}
The issue is that the keyboard is overlapping some textfields:
As you may see in the code,I have wrapped the column that contains the textfields with a SingleChildScrollView and I have included resizeToAvoidBottomPadding: true, inside the scaffold.
What am Im I missing?
test this example
return Scaffold(
body: SingleChildScrollView(
physics: NeverScrollableScrollPhysics(),
child: ConstrainedBox(
constraints: BoxConstraints(
minWidth: MediaQuery.of(context).size.width,
minHeight: MediaQuery.of(context).size.height,
),
child: IntrinsicHeight(
child: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
//your code here
],
),
),
),
),
);

Custom TextField Designs Flutter

I want to achieve the design below but I don't know how to customize this, am still a newbie in Flutter.
I know how to use TextFields but don't know how to go around this :
Padding(
padding: EdgeInsets.symmetric(horizontal: 30),
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
TextField(
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
filled: true,
hintStyle: new TextStyle(color: Colors.grey[700]),
hintText: "Email",
fillColor: Colors.white70),
),
SizedBox(height: 50),
TextField(
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
filled: true,
hintStyle: new TextStyle(color: Colors.grey[700]),
hintText: "Password",
fillColor: Colors.white70),
)
]));
Lutaaya Huzaifah Idris Refer to the documentation as specified by Afridi Kayal in comments for more customization.
Container(
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(30),
),
padding: const EdgeInsets.only(left: 25, right: 20),
margin: const EdgeInsets.fromLTRB(20,10,20,10),
child: TextField(
decoration: InputDecoration(
hintText: "Email",
border: InputBorder.none,
),
),
),
Container(
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(30),
),
padding: const EdgeInsets.only(left: 25, right: 20),
margin: const EdgeInsets.fromLTRB(20,10,20,10),
child: TextField(
decoration: InputDecoration(
hintText: "Password",
border: InputBorder.none,
),
obscureText: true,
),
),
Output:

TextField placeholder and text vertical alignment is not right

I am developing a mobile application using Flutter. I am having an issue with aligning the text field's placeholder text and its value vertically centered.
This is my code for TextField.
return Container(
color: Colors.black,
child: Padding(
padding: EdgeInsets.all(10),
child: TextField(
onChanged: (text) {
this.filter = text.toLowerCase();
this._refreshListItems();
},
style: TextStyle(
height: 0.5
),
cursorColor: Colors.black12,
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
prefixIcon: Icon(Icons.search, color: Colors.black26,),
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
hintText: "Search",
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white),
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
),
),
)
);
However, when it is rendered, the placeholder and its value are getting a little bit to the top vertically as in the screenshots below.
What's wrong with my code and how can I fix it?
Using text style height to 0.5 is causing the text span to be half of the size of the font size. Remove it as don't think that will help you.
style: TextStyle(
height: 0.5
),
In order to handle content size you can play with contentPadding
decoration: InputDecoration(
contentPadding: EdgeInsets.all(1),
....
),
I used the following code and it is working
contentPadding: EdgeInsets.fromLTRB(0, 8, 0, 0),
if you set a custom height, try this.
Container(
height: 36,
child: TextField(
maxLines: 1,
style: TextStyle(fontSize: 17),
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
filled: true,
prefixIcon:
Icon(Icons.search, color: Theme.of(context).iconTheme.color),
border: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(Radius.circular(30))),
fillColor: Theme.of(context).inputDecorationTheme.fillColor,
contentPadding: EdgeInsets.zero,
hintText: 'Search',
),
),
)

Flutter wrong position of text in textfield after changing height

According to my design I have a TextField with 30pt of height.
Here's how I am trying to do that:
Container(
height: 30,
child: new TextField(
textAlign: TextAlign.start,
decoration: new InputDecoration(
prefixIcon: Icon(Icons.search),
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(30.0),
),
),
filled: true,
hintStyle: new TextStyle(color: Colors.grey[800]),
hintText: "Search",
fillColor: Colors.lightGreen),
)),
And here's the result:
How can I align hint and text vertically?
Set contentPadding to EdgeInsets.symmetric(horizontal: 0) in InputDecoration. This will remove the default horizontal padding for the Text:
Container(
height: 35,
child: TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(horizontal: 0),
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(5.0),
),
),
filled: true,
hintStyle: TextStyle(color: Colors.grey[800]),
hintText: "Search",
fillColor: Colors.lightGreen),
),
),

How can I make rounded TextField in flutter?

Material Design for iOS doesn't look good, especially the TextField. So is there any way to create your own ? Or Is ther any way to add some styling to TextField so it will look rounded ?
You can add rounded corners to a TextField within the decoration parameter of it
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
),
filled: true,
hintStyle: TextStyle(color: Colors.grey[800]),
hintText: "Type in your text",
fillColor: Colors.white70),
)
#Rockvole answer is correct but just in case u don't like the default border around the TextField, you can achieve it by overriding the borderSide attribute of the OutlineInputBorder like this:
TextField(
textAlign: TextAlign.center,
controller: searchCtrl,
keyboardType: TextInputType.text,
decoration: InputDecoration(
hintText: 'Enter a product name eg. pension',
hintStyle: TextStyle(fontSize: 16),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
contentPadding: EdgeInsets.all(16),
fillColor: colorSearchBg,
),
),
You can get desired output with the help of Container Have a look..
new Container(
child: new Text (
"Text with rounded edges.",
style: new TextStyle(
color: Colors.blue[500],
fontWeight: FontWeight.w900
)
),
decoration: new BoxDecoration (
borderRadius: new BorderRadius.all(new Radius.circular(10.0)),
color: Colors.black
),
padding: new EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 16.0),
),
You can try this code bellow:
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
)
I've collected this solution from several methods and solutions over the internet, it works very well with me, so it could help someone out there:
This solution will control the width and height of the TextField, and other properties
Container(
child: Theme(
data: Theme.of(context).copyWith(splashColor: Colors.transparent),
child: TextField(
autofocus: false,
style: TextStyle(fontSize: 15.0, color: Color(0xFFbdc6cf)),
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
hintText: 'Search',
contentPadding:
const EdgeInsets.only(left: 14.0, bottom: 12.0, top: 0.0),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(25.7),
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(25.7),
),
),
),
),
decoration: new BoxDecoration (
borderRadius: new BorderRadius.all(new Radius.circular(30.0)),
color: Colors.white ), width: 250, height: 50, margin: new EdgeInsets.fromLTRB(20, 20, 20, 210), padding: new EdgeInsets.fromLTRB(8, 8, 8, 8),
),
You could approach the purpose in a few different ways:
First Approach:
Container(
child: new Text (
"Some Text",
style: TextStyle(
color: Colors.black
)
),
decoration: BoxDecoration (
borderRadius: BorderRadius.all( Radius.circular(15)),
color: Colors.white
),
padding: EdgeInsets.all(16.0),
),
Second Approach:
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
Radius.circular(15.0),
),
),
filled: true,
hintStyle: new TextStyle(color: Colors.grey[600]),
hintText: "Hint text",
fillColor: Colors.white),
)
Third Approach:
TextField(
textAlign: TextAlign.center,
controller: someTextXontroller,
keyboardType: TextInputType.text,
decoration: InputDecoration(
hintText: 'Hint Text',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
contentPadding: EdgeInsets.all(16),
fillColor: Colors.blue,
),
),
Try this in TextFormField:
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(20.0),
),
),