I am trying to add a search input control to the app header in my Flutter web app.
See...
Widget _buildSearchControl() {
return Container(
alignment: Alignment.centerLeft,
color: Colors.white,
padding: EdgeInsets.only(right: 20),
margin: EdgeInsets.all(10),
width: 300,
child: TextField(
style: TextStyle(backgroundColor: Colors.white, color: Colors.black),
decoration: InputDecoration(
prefixIcon: const Icon(Icons.search,color: Colors.black,),
suffixIcon: IconButton(
icon: const Icon(Icons.clear,color: Colors.black,),
onPressed: () {
/* Clear the search field */
},
),
hintText: 'Search...',
border: InputBorder.none),
),
);
}
However, when entering the text, the characters are not vertically centre aligned as I wpuld expect
Is there a way I can have the characters input to the TextField control vertically centered so that they line up with the icons?
Have you tried this textAlignVertical property?
TextField(
textAlignVertical: TextAlignVertical.center,
....
),
I wrap the Container with Padding.
And add contentPadding: const EdgeInsets.all(5), to InputDecoration
...
Padding(
padding: const EdgeInsets.all(8.0).copyWith(bottom: 0),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Theme.of(context).primaryColor, width: 2),
borderRadius: const BorderRadius.all(Radius.circular(10)),
color: Colors.white
),
child: TextFormField(
controller: _controller,
onChanged: (string) {},
style: TextStyle(color: Theme.of(context).primaryColor),
cursorColor: Theme.of(context).primaryColor,
decoration: InputDecoration(
contentPadding: const EdgeInsets.all(5),
enabledBorder: InputBorder.none,
focusedBorder: InputBorder.none,
labelText: 'Enter',
labelStyle: TextStyle(
color: Theme.of(context).primaryColor,
fontSize: 16,
fontWeight: FontWeight.w500),
prefixIcon: Icon(
Icons.document_scanner_sharp,
color: Theme.of(context).primaryColor,
),
suffixIcon: Padding(
padding: const EdgeInsets.only(top: 5.0),
child: IconButton(
alignment: Alignment.topLeft,
icon: const Icon(
Icons.clear,
size: 25,
color: Colors.red,
),
tooltip: 'Clear',
onPressed: () {
FocusScope.of(context).unfocus();
_controller.clear();
}
),
)
)
),
),
),
Use this , maybe it depends on your contentPadding
Container(
width: 300,
height: 56,
margin: EdgeInsets.fromLTRB(0, 0, 0, 20),
child: TextFormField(
key: widget.globalKey,
autocorrect: false,
onTap: widget.onClick,
minLines: widget.minLines,
maxLines: widget.maxLines,
textInputAction: TextInputAction.done,
onChanged: (val) => widget.onChangeValue(val),
style: getInputStyle,
initialValue: widget.initialValue,
textAlign: TextAlign.left,
autovalidateMode: AutovalidateMode.onUserInteraction,
controller: widget.controller,
keyboardAppearance: Brightness.dark,
decoration: InputDecoration(
contentPadding: REdgeInsets.fromLTRB(
14, 16, 0, 16),
labelText: widget.labelText,
hintText: widget.hintText,
floatingLabelBehavior: FloatingLabelBehavior.auto,
labelStyle: getLabelStyle,
hintStyle: getLabelStyle,
fillColor: widget.backGroundColor ,
filled: true,
prefix: widget.prefix,
),
),
)
I was trying to make two cards with Expanded. Everything works fine except that the card is taking too much space in the bottom and won't stop when there is no more TextFormField.
This is my code and below there is a screenshot.
Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => Navigator.of(context).pop(),
),
title: Text(
"Ajouter Un Nouveau Fournisseur",
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'Montserrat',
color: Colors.black,
fontSize: 20,
fontWeight: FontWeight.normal),
),
elevation: 10,
),
backgroundColor: Color(0xFFFFFFFE),
body: SafeArea(
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
flex: 6,
child: Padding(
padding: EdgeInsets.all(30),
child: Card(
color: Color(0xFFF5F5F5),
elevation: 20,
clipBehavior: Clip.antiAliasWithSaveLayer,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
child: Column(
children: [
Padding(
padding: EdgeInsets.fromLTRB(0, 20, 0, 10),
child: Text(
'Informations Générales',
style: TextStyle(
fontFamily: 'Montserrat',
color: Colors.black,
fontSize: 15,
fontWeight: FontWeight.bold),
),
),
Divider(
thickness: 1,
),
Form(
key: formKey,
child: Padding(
padding: EdgeInsets.all(15.0),
child: Column(
children: [
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Nom Du Fournisseur',
//prefixIcon: Icon(Icons.email),
icon: Icon(Icons.perm_identity)),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Siége Social',
//prefixIcon: Icon(Icons.email),
icon: Icon(Icons.perm_identity)),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Pays',
//prefixIcon: Icon(Icons.email),
icon: Icon(Icons.location_on)),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Ville/Région',
//prefixIcon: Icon(Icons.email),
icon: Icon(
Icons.location_city,
),
),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Numéro de Téléphone',
//prefixIcon: Icon(Icons.email),
icon: Icon(
Icons.phone,
),
),
),
),
],
),
),
),
],
),
),
),
),
Expanded(
flex: 6,
child: Padding(
padding: EdgeInsets.all(30),
child: Card(
color: Color(0xFFF5F5F5),
elevation: 20,
clipBehavior: Clip.antiAliasWithSaveLayer,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
child: Container(
width: MediaQuery.of(context).size.width * 0.35,
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.rectangle,
),
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Padding(
padding: EdgeInsets.fromLTRB(0, 20, 0, 10),
child: Text(
'Informations Financieres',
style: TextStyle(
fontFamily: 'Montserrat',
color: Colors.black,
fontSize: 15,
fontWeight: FontWeight.bold),
),
),
Divider(
thickness: 1,
),
Form(
key: formKey,
autovalidateMode: AutovalidateMode.disabled,
child: Padding(
padding: EdgeInsets.all(15.0),
child: Column(
children: [
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Nom Du Fournisseur',
//prefixIcon: Icon(Icons.email),
icon: Icon(Icons.perm_identity)),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Siége Social',
//prefixIcon: Icon(Icons.email),
icon: Icon(Icons.perm_identity)),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Pays',
//prefixIcon: Icon(Icons.email),
icon: Icon(Icons.location_on)),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Ville/Région',
//prefixIcon: Icon(Icons.email),
icon: Icon(
Icons.location_city,
),
),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Numéro de Téléphone',
//prefixIcon: Icon(Icons.email),
icon: Icon(
Icons.phone,
),
),
),
),
],
),
),
),
],
),
),
),
),
),
],
),
),
);
For every Column widget, use mainAxisSize: MainAxisSize.min,.
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
More about mainaxissize property.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => Navigator.of(context).pop(),
),
title: Text(
"Ajouter Un Nouveau Fournisseur",
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'Montserrat',
color: Colors.black,
fontSize: 20,
fontWeight: FontWeight.normal),
),
elevation: 10,
),
backgroundColor: Color(0xFFFFFFFE),
body: SafeArea(
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
flex: 6,
child: Padding(
padding: EdgeInsets.all(30),
child: Card(
color: Color(0xFFF5F5F5),
elevation: 20,
clipBehavior: Clip.antiAliasWithSaveLayer,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: EdgeInsets.fromLTRB(0, 20, 0, 10),
child: Text(
'Informations Générales',
style: TextStyle(
fontFamily: 'Montserrat',
color: Colors.black,
fontSize: 15,
fontWeight: FontWeight.bold),
),
),
Divider(
thickness: 1,
),
Form(
key: formKey,
child: Padding(
padding: EdgeInsets.all(15.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Nom Du Fournisseur',
//prefixIcon: Icon(Icons.email),
icon: Icon(Icons.perm_identity)),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Siége Social',
//prefixIcon: Icon(Icons.email),
icon: Icon(Icons.perm_identity)),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Pays',
//prefixIcon: Icon(Icons.email),
icon: Icon(Icons.location_on)),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Ville/Région',
//prefixIcon: Icon(Icons.email),
icon: Icon(
Icons.location_city,
),
),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Numéro de Téléphone',
//prefixIcon: Icon(Icons.email),
icon: Icon(
Icons.phone,
),
),
),
),
],
),
),
),
],
),
),
),
),
Expanded(
flex: 6,
child: Padding(
padding: EdgeInsets.all(30),
child: Card(
color: Color(0xFFF5F5F5),
elevation: 20,
clipBehavior: Clip.antiAliasWithSaveLayer,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
child: Container(
width: MediaQuery.of(context).size.width * 0.35,
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.rectangle,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: EdgeInsets.fromLTRB(0, 20, 0, 10),
child: Text(
'Informations Financieres',
style: TextStyle(
fontFamily: 'Montserrat',
color: Colors.black,
fontSize: 15,
fontWeight: FontWeight.bold),
),
),
Divider(
thickness: 1,
),
Form(
key: formKey,
autovalidateMode: AutovalidateMode.disabled,
child: Padding(
padding: EdgeInsets.all(15.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Nom Du Fournisseur',
//prefixIcon: Icon(Icons.email),
icon: Icon(Icons.perm_identity)),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Siége Social',
//prefixIcon: Icon(Icons.email),
icon: Icon(Icons.perm_identity)),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Pays',
//prefixIcon: Icon(Icons.email),
icon: Icon(Icons.location_on)),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Ville/Région',
//prefixIcon: Icon(Icons.email),
icon: Icon(
Icons.location_city,
),
),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Numéro de Téléphone',
//prefixIcon: Icon(Icons.email),
icon: Icon(
Icons.phone,
),
),
),
),
],
),
),
),
],
),
),
),
),
),
],
),
),
);
}
}
I need a Text Area with an enforced maxLength of 250 chars and its label aligned with the hint text. My code is below.
However, the widget is rendering the text on top of the hint. Does anyone know a solution?
It's like if the counter widget forces the actual text field to move upwards.
TextField with maxLength = 250 and alignLabelWithHint = true
TextField(
expands: false,
minLines: null,
maxLines: null,
maxLengthEnforced: maxLengthEnforced,
maxLength: maxLength,
controller: controller,
onChanged: (String value){
print(value);
},
cursorColor: Colors.deepOrange,
keyboardType: keyboardType,
decoration: InputDecoration(
alignLabelWithHint: true,
labelText: text,
labelStyle: TextStyle(color: Colors.grey),
hintText: text,
prefixIcon: Container(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 15.0),
child: Material(
elevation: 0,
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(30)),
child: Icon(
icon,
color: Colors.red,
),
),
),
],
),
),
border: InputBorder.none,
contentPadding: EdgeInsets.only(right: 10, top: 5, bottom: 5)),
)
The purpose of a hint is to disappear when the user start typing I tried your code and it worked with no problem.
TextField(
expands: false,
minLines: null,
maxLines: null,
maxLengthEnforced: true,
maxLength: 250,
// controller: controller,
onChanged: (String value) {
print(value);
},
cursorColor: Colors.deepOrange,
// keyboardType: keyboardType,
decoration: InputDecoration(
alignLabelWithHint: true,
labelText: "hi",
labelStyle: TextStyle(color: Colors.grey),
hintText: "hi",
prefixIcon: Container(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 15.0),
child: Material(
elevation: 0,
color: Colors.white,
borderRadius:
BorderRadius.all(Radius.circular(30)),
child: Icon(
Icons.highlight,
color: Colors.red,
),
),
),
],
),
),
border: InputBorder.none,
contentPadding:
EdgeInsets.only(right: 10, top: 5, bottom: 5)),
),