How to make the text above the textformfield? - flutter

This is the design I want to make
This is my current design
I'm new to flutter. My question is how to make the text above the textformfield. I have searched on the internet and tried to do it but still no success. I don't know where else to go to solve my problem. I hope overflow is willing to help me.
This is my code:
Container(
width: double.infinity,
margin: EdgeInsets.fromLTRB(10, 0, 10, 10),
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
border: Border.all(
color: Colors.transparent,
width: 1.0,
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
SizedBox(
height: 100,
width: 100,
child: Text('First Name'),
),
SizedBox(
width: 200,
child: TextFormField(
style: TextStyle(color: Colors.black),
controller: firstName,
onSaved: (String? value) {
firstName.text = value!;
},
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'First Name',
hintStyle: TextStyle(
color: Colors.black, fontSize: 16),
),
),
),
],
)
],
),
)

Your problem occurs because of wrong widget placement. Notice that you've Row inside a Column, but you don't really use Column's children in order to vertically place 'First Name' text widget.Also the row is basically redundant currently. You can make a Row of Columns I just shared below in order to achieve your desired UI. Try to play with it :)
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
const SizedBox(
height: 20,
width: 100,
child: Text('First Name'),
),
SizedBox(
width: 150,
child: TextFormField(
style: const TextStyle(color: Colors.black),
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'First Name',
hintStyle: TextStyle(color: Colors.black, fontSize: 16),
),
),
)
],
),

200px is too big and structure I will prefer
Row
-Expanded
- Column (CrossAxisAlignment.startMainAxisSize.min,)
- Text
- TextFormField
-Expanded
- Column (CrossAxisAlignment.startMainAxisSize.min,)
- Text
- TextFormField
-Expanded
- Column (CrossAxisAlignment.startMainAxisSize.min,)
- Text
- TextFormField
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Container(
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
border: Border.all(
color: Colors.transparent,
width: 1.0,
),
),
child: Row(
children: [
filed(),
filed(),
filed(),
],
),
)
],
),
);
}
Expanded filed() {
return Expanded(
child: Padding(
padding: EdgeInsets.all(8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: EdgeInsets.only(bottom: 20),
child: Text('First Name'),
),
TextFormField(
style: TextStyle(color: Colors.black),
onSaved: (String? value) {},
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'First Name',
hintStyle: TextStyle(color: Colors.black, fontSize: 16),
),
),
],
),
),
);
}
}

To create the TextField you want, you just need to use a Column like this:
Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('First Name', style: TextStyle(fontWeight: FontWeight.bold),),
SizedBox(
width: 200,
child: TextFormField(
style: TextStyle(color: Colors.black),
onSaved: (String? value) {
// firstName.text = value!;
},
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'First Name',
hintStyle: TextStyle(color: Colors.black, fontSize: 16),
),
),
)
],
)
The result is:

In the input decoration of TextFormField, use label instead of hintText.
decoration: InputDecoration(
border: OutlineInputBorder(),
label: Text('First Name',
style: TextStyle(color: Colors.black, fontSize: 16),),
),

List item
You use the the property 'Labletext' of TextFormFiled or TextField....
to give the above text of the textformFiled.

just use column widget first children is Text and second children as TextFormField.

Related

How to change textformfield spacing?

I am new to using flutter. I want to ask. How to change the spacing between textformfield? I have done the same as textformfield address, Postcode and District for textformfield firstname, last name, and relationship but still not successful. I have changed the container margin and padding and used spacing wrap for textformfield firstname, last name and relationship but still not successful. Where is my coding error? Please, someone, help me in solving this problem.
This is my code:
Container(
width: double.infinity,
//margin: EdgeInsets.symmetric(vertical: 50),
margin: EdgeInsets.symmetric(
horizontal: 10, vertical: 40),
padding: EdgeInsets.symmetric(horizontal: 210),
decoration: BoxDecoration(
border: Border.all(
color: Colors.transparent,
width: 1.0,
),
borderRadius: BorderRadius.circular(5)),
/*decoration: BoxDecoration(
border: Border(
/* top: BorderSide(
color: Colors.blue,
width: 2.0,
),*/
bottom: BorderSide(
color: Colors.blue, width: 2.0))),*/
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 30,
child: Text(
'Next of kin 1',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold),
),
),
Container(
//height: 51,
//width: 30,
width: double.infinity,
//margin: EdgeInsets.symmetric(horizontal: 10),
/*padding: EdgeInsets.symmetric(horizontal: 5),*/
decoration: BoxDecoration(
border: Border.all(
color: Colors.transparent,
width: 1.0,
),
borderRadius: BorderRadius.circular(5)),
child: Row(
children: [
Expanded(
child: Wrap(
direction: Axis.horizontal,
alignment: WrapAlignment.start,
//spacing: 20.0,
children: [
Column(
crossAxisAlignment:
CrossAxisAlignment.start,
mainAxisAlignment:
MainAxisAlignment.start,
children: [
SizedBox(
height: 30,
child: Text(
'First Name',
style: TextStyle(
fontWeight:
FontWeight.bold,
fontSize: 16),
),
),
SizedBox(
width: 300,
//height: 100,
child: TextFormField(
style: const TextStyle(
color: Colors.black),
controller:
nextOfKinFirstName,
onSaved: (String? value) {
nextOfKinFirstName.text =
value!;
},
decoration:
const InputDecoration(
border:
OutlineInputBorder(),
hintText: 'First Name',
hintStyle: TextStyle(
color: Colors.black,
fontSize: 16),
),
),
),
],
)
],
),
),
Expanded(
child: Wrap(
children: [
Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
SizedBox(
height: 30,
child: Text(
'Last Name',
style: TextStyle(
fontWeight:
FontWeight.bold,
fontSize: 16),
),
),
SizedBox(
width: 300,
//height: 100,
child: TextFormField(
style: const TextStyle(
color: Colors.black),
controller: nextOfKinLastName,
onSaved: (String? value) {
nextOfKinLastName.text =
value!;
},
decoration:
const InputDecoration(
border:
OutlineInputBorder(),
hintText: 'Last Name',
hintStyle: TextStyle(
color: Colors.black,
fontSize: 16),
),
),
),
],
),
],
)),
Expanded(
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
SizedBox(
height: 30,
child: Text(
'Relationship',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16),
),
),
Container(
height: 51,
padding: EdgeInsets.symmetric(
horizontal: 10, vertical: 5),
decoration: BoxDecoration(
border: Border.all(
color: Colors.black12,
width: 1.0,
),
borderRadius:
BorderRadius.circular(5)),
child: SizedBox(
width: 300,
child: DropdownButton<String>(
underline: Container(
color:
Colors.transparent),
hint: _nextOfKinRelationship ==
null
? Text('Relationship')
: Text(
_nextOfKinRelationship),
isExpanded: true,
value:
_nextOfKinRelationship,
items: <String>[
'Father',
'Mother',
'Husband',
'Wife',
'Son',
'Daughter',
'Brother',
'Sister',
'Grandfather',
'Grandmother',
'Grandson',
'Grandaughter',
'Uncle',
'Aunt',
'Cousin',
'Nephew',
'Neice',
'Father in law',
'Mother in law',
'Son in law',
'Daughter in law',
'Brother in law',
'Sister in law',
'Friend'
].map((String value) {
return new DropdownMenuItem<
String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (value) {
setState(() {
_nextOfKinRelationship =
value!;
});
},
)),
),
],
),
)
],
))
],
),
),
you use Row to cover 3 TextField and wrap it with expanded, the space that available will be devide into 3.
but for the TextField you wrap it with certain width (SizedBox). and i think current screen is wider than all children inside Row
my suggestion is change it with Container which is fit available space.
Row(
children: [
// option 1
Expanded(
child: Column(
children: [
Text("label"),
TextField(), // without sizebox ,
],
),
),
//option 2
Expanded(
child: Column(
children: [
Text("label"),
Container(
width: double.infinity, // push to maximum space available
child: TextField(),
) // use container insted if you want to make decoration ,
],
),
),
],
);

Facing assertion error when trying to add row inside a column

I am trying to make a login page which looks like this
But whenever I try to add row which has another column iside it for textinput field and text It shows assertion error. I also tried removing sized box before text row still it is showing the same error
My flutter code:
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: color,
body: SafeArea(
child: Center(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: 20.0,
),
Text('Life Drop',
style: TextStyle(fontSize: 40, color: Colors.white)),
SizedBox(
height: 10.0,
),
Text("your blood can save lives",
style: TextStyle(fontSize: 12, color: Colors.white)),
SizedBox(
height:20.0
),
Row(
children: [
Column(children: [
Text('Login'),
TextFormField(
controller: emailController,
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: 'Email',
),
),
SizedBox(width: 20),
],)
],
)
],
),
),
)
)
);
}
edit your row like below code:
Expanded(
child: Row(
children: [
Expanded(
child: Column(
children: [
const Text('Login'),
TextFormField(
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: 'Email',
),
),
const SizedBox(width: 20),
],
)),
],
))

Flutter:(TextFormField)Problem when opening the keyboard and start typing

When I open the keyboard to start typing, the problem appears that there is too much space
[1]: https://i.stack.imgur.com/Nk4l6.png
[2]: https://i.stack.imgur.com/12jNe.png
the code:
import 'package:awesome_dialog/awesome_dialog.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
class SignUpScreen extends StatelessWidget {
#override
var Email, Pass;
var txt;
GlobalKey<FormState> formstate = new GlobalKey<FormState>();
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
toolbarHeight: 0,
leading: Container(),
backgroundColor: Color(0xFF03045E),
),
body: Container(
width: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [Color(0xFF03045E),Color(0xFF1F21D5),]
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
width: 45,
height: 45,
margin: EdgeInsets.only(top: 70,right: 35),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Color(0xff383989),
),
child: FlatButton(
onPressed: (){
Get.back();
},
child: Image(
image: AssetImage('assets/images/cancel-icon.png'),
color: Colors.white,
width: double.infinity,
),
),
),
],
),
Container(
margin: EdgeInsets.symmetric(horizontal: 35),
child: Column(
children: [
Container(
margin: EdgeInsets.only(bottom: 40),
child: Row(
children: [
Text('Create\n Account',style: TextStyle(
color: Colors.white,
fontSize: 30
),),
],
),
),
Row(
children: [
Text('Create account and enjoy 7 days free trial',style: TextStyle(
color: Color(0xffB2B2DC),
fontSize: 15
),),
],
),
],
),
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Row(
children: [
Expanded(flex: 1 , child: Container(),),
Expanded(
flex: 10,
child: TextFormField(
keyboardType: TextInputType.name,
cursorColor: Colors.white,
style: TextStyle(fontSize: 18, color: Colors.white),
decoration: InputDecoration(
filled: true,
fillColor: Colors.white24,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
contentPadding: EdgeInsets.symmetric(vertical: 25.0, horizontal: 20.0),
hintText: 'Your first and last name?',
labelText: 'Full Name',
labelStyle: TextStyle(color:Colors.white60 , fontSize: 18),
hintStyle: TextStyle(color:Colors.white60 , fontSize: 18),
),
onSaved: (val) {
Email = val;
},
validator: (val) {
if (val == null || val.isEmpty) {
return 'Please enter some text';
}
},
),
),
Expanded(flex: 1 , child: Container(),)
],
),
Row(
children: [
Expanded(flex: 1 , child: Container(),),
Expanded(
flex: 10,
child: TextFormField(
keyboardType: TextInputType.emailAddress,
cursorColor: Colors.white,
style: TextStyle(fontSize: 18, color: Colors.white),
decoration: InputDecoration(
filled: true,
fillColor: Colors.white24,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
contentPadding: EdgeInsets.symmetric(vertical: 25.0, horizontal: 20.0),
labelText: 'Email',
labelStyle: TextStyle(color:Colors.white60 , fontSize: 18),
hintStyle: TextStyle(color:Colors.white60 , fontSize: 18),
),
onSaved: (val) {
Email = val;
},
validator: (val) {
if (val == null || val.isEmpty) {
return 'Please enter some text';
}
},
),
),
Expanded(flex: 1 , child: Container(),)
],
),
Row(
children: [
Expanded(flex: 1 , child: Container(),),
Expanded(
flex: 10,
child: TextFormField(
keyboardType: TextInputType.visiblePassword,
cursorColor: Colors.white,
style: TextStyle(fontSize: 18, color: Colors.white),
decoration: InputDecoration(
filled: true,
fillColor: Colors.white24,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
contentPadding: EdgeInsets.symmetric(vertical: 25.0, horizontal: 20.0),
labelText: 'password',
labelStyle: TextStyle(color:Colors.white60 , fontSize: 18),
hintStyle: TextStyle(color:Colors.white60 , fontSize: 18),
),
onSaved: (val) {
Email = val;
},
validator: (val) {
if (val == null || val.isEmpty) {
return 'Please enter some text';
}
},
),
),
Expanded(flex: 1 , child: Container(),)
],
),
Row(
children: [
Expanded(flex: 1 , child: Container(),),
Expanded(
flex: 10,
child: TextFormField(
keyboardType: TextInputType.visiblePassword,
cursorColor: Colors.white,
style: TextStyle(fontSize: 18, color: Colors.white),
decoration: InputDecoration(
filled: true,
fillColor: Colors.white24,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
contentPadding: EdgeInsets.symmetric(vertical: 25.0, horizontal: 20.0),
labelText: 're-password',
labelStyle: TextStyle(color:Colors.white60 , fontSize: 18),
hintStyle: TextStyle(color:Colors.white60 , fontSize: 18),
),
onSaved: (val) {
Email = val;
},
validator: (val) {
if (val == null || val.isEmpty) {
return 'Please enter some text';
}
},
),
),
Expanded(flex: 1 , child: Container(),)
],
),
Container(
margin: EdgeInsets.only(bottom: 50,top: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(flex: 1 , child: Container(),),
Expanded(
flex: 10,
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)
),
color: Colors.white,
height: 70,
onPressed: (){},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Text('Sign up',textAlign:TextAlign.center,
style: TextStyle(
color: Color(0xff03045E),
fontSize: 18.0,
),),
),
],
),
),
),
Expanded(flex: 1 , child: Container(),),
],
),
),
],
),
),
],
),),
],
),
),
);
}
}
======== Exception caught by rendering library =====================================================
The following assertion was thrown during layout:
A RenderFlex overflowed by 51 pixels on the bottom.
The relevant error-causing widget was:
Column Column:file:///M:/Android%20PG/lockvpn0/lib/view/auth/sign-up.dart:87:26
The overflowing RenderFlex has an orientation of Axis.vertical.
The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and black striped pattern. This is usually caused by the contents being too big for the RenderFlex.
Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the RenderFlex to fit within the available space instead of being sized to their natural size.
This is considered an error condition because it indicates that there is content that cannot be seen. If the content is legitimately bigger than the available space, consider clipping it with a ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex, like a ListView.
The specific RenderFlex in question is: RenderFlex#d12d3 relayoutBoundary=up5 OVERFLOWING
... needs compositing
... parentData: offset=Offset(0.0, 260.0); flex=1; fit=FlexFit.tight (can use size)
... constraints: BoxConstraints(0.0<=w<=411.4, h=388.9)
... size: Size(411.4, 388.9)
... direction: vertical
... mainAxisAlignment: spaceEvenly
... mainAxisSize: max
... crossAxisAlignment: center
... verticalDirection: down
◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤
You can wrap your column with a SingleChildScrollView. That should fix the overflow issue.
If you set resizeToAvoidBottomInset to true on your scaffold, the column won't be squished but you also won't be able to see your TextFields.

Make the two text field always align together

How to make the length of the textField always same?
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Project"),
SizedBox(
width: 25,
),
Expanded(
child: TextField(
style: TextStyle(fontSize: 12),
// onChanged: (text) => _assetBloc.assetSink.add(text),
controller: _projectController,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius:
const BorderRadius.all(const Radius.circular(5.0))),
contentPadding: EdgeInsets.all(10),
hintText: "Phase 1",
labelText: "Phase 1",
),
))
],
),SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("To"),
SizedBox(
width: 25,
),
Expanded(
child: TextField(
style: TextStyle(fontSize: 12),
// onChanged: (text) => _assetBloc.assetSink.add(text),
controller: _projectController,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius:
const BorderRadius.all(const Radius.circular(5.0))),
contentPadding: EdgeInsets.all(10),
hintText: "Phase 1",
labelText: "Phase 1",
),
))
],
)
Try this code:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('Welcome to Flutter'),
),
body: Center(
child: TextFields()
),
),
);
}
}
class TextFields extends StatelessWidget {
#override
Widget build(BuildContext context) {
// TODO: implement build
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
width: MediaQuery.of(context).size.width/7,
child: Text("Project")),
SizedBox(
width: 25,
),
Flexible(
child: TextField(
style: TextStyle(fontSize: 12),
// onChanged: (text) => _assetBloc.assetSink.add(text),
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(5.0))),
contentPadding: EdgeInsets.all(10),
hintText: "Phase 1",
labelText: "Phase 1",
),
))
],
),
SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
width: MediaQuery.of(context).size.width/7,
child: Text("To")),
SizedBox(
width: 25,
),
Expanded(
child: TextField(
style: TextStyle(fontSize: 12),
// onChanged: (text) => _assetBloc.assetSink.add(text),
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(5.0))),
contentPadding: EdgeInsets.all(10),
hintText: "Phase 1",
labelText: "Phase 1",
),
))
],
)
],
),
);
}
}
Since you are using an Expanded widget your Textfield always take whatever room is left.
I think the trick here would be to wrap your Text Widget in a Container of fix width.
You could use a Row and 2 Columns for this
Row(
children: [
Column(
children: [
Text("Project"),
Text("To")
]
),
SizedBox(width: 10),
Expanded(
child: Column(
children: [
TextField(),
TextField()
]
)
)

Resize TextField based on content in Flutter

How a TextField width can be resized automatically based on content in Flutter?
An example with 3 TextField which autoresize depending on the content :
Only place the TextField inside an Expanded that will be in a row.
return Scaffold(
body: Container(
padding: EdgeInsets.only(top: 50),
child: new Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Expanded(
child: Padding(
child: new TextField(
onTap: () {//action of TextField
},
keyboardType: TextInputType.text,
decoration: InputDecoration(
border: InputBorder.none, hintText: 'Password'),
style: Theme.of(context).textTheme.body1,
),
padding: EdgeInsets.only(left: 40),
)),
IconButton(
icon: Icon(Icons.apps),
onPressed: () {//action of iconbutton
},
)
],
)
])),
);
Set the maxLines property of TextField to null. maxLines: null
TextField(
controller: new TextEditingController(
text: comment
),
maxLines: null,
style: TextStyle(
color: Colors.black,
fontSize: 12.0,
),
textAlign: TextAlign.left,
decoration: InputDecoration(
filled: true,
fillColor: HexColor('#ecedec'),
border: OutlineInputBorder(
borderSide:BorderSide.none,
borderRadius: BorderRadius.all(Radius.circular(10.0)),
),
),
)
I found the solution using the IntrinsicWidth class
IntrinsicWidth(
child: TextField(...
),