How to decrease the height of Input Text Field in Flutter? - flutter

Well, I am designing something that needs a compact layout. The design that I had made in Figma is what I want but the result I am getting while doing so, that's a bit different. I am not able to attain that level of compactness that I need.
What I designed in Figma:
What I coded in Flutter:
You see, there's an extra amount of space present above the text. I want to remove that text any how. So could anyone please help me in cutting out the extra padding that I don't need.
Here's my code:
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
class CardHolderTextField extends StatelessWidget {
const CardHolderTextField({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
width: 175,
height: 55,
child: TextField(
keyboardType: TextInputType.name,
maxLength: 30,
minLines: 1,
textCapitalization: TextCapitalization.characters,
style: GoogleFonts.mavenPro(
fontSize: 10,
color: Colors.white,
textBaseline: TextBaseline.alphabetic,
),
decoration: InputDecoration(
contentPadding: EdgeInsets.all(0),
counter: Text(""),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
),
),
disabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
),
),
border: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
),
),
fillColor: Colors.white,
focusColor: Colors.white,
),
),
);
}
}
I tried a lot, playing with the size of the container, but that doesn't work, instead pops out a -ve error.
This happened when I tried to decrease the size of the container to 40:
TextAlignVertical.top() also fails to solve the problem when container size is reduced to 40.
What I guess is it's not the problem of container, it's the problem of Text Field.
Anyways, thanks in advance!

TextField size depends on parent size. In this case, you can tweak the value according to your UI and providing contentPadding from TextFiled>decoration:. How-ever, there could be space-problem depending on fontSize+ etc. I prefer using stack.
I removed Container from your CardHolderTextField
class CardHolderTextField extends StatelessWidget {
const CardHolderTextField({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return TextField(
keyboardType: TextInputType.name,
maxLength: 30,
minLines: 1,
textCapitalization: TextCapitalization.characters,
style: GoogleFonts.mavenPro(
color: Colors.white,
fontSize: 12,
textBaseline: TextBaseline.alphabetic,
),
decoration: InputDecoration(
contentPadding: EdgeInsets.all(0),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
),
),
disabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
),
),
border: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
),
),
fillColor: Colors.white,
// filled: true,
focusColor: Colors.white,
),
);
}
}
And use with Stack
SizedBox(
height: 80,
width: 175,
child: Stack(
children: [
Positioned(
top: 0,
left: 0,
child: Text(
"card holder".toUpperCase(),
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: CardHolderTextField(),
),
],
),
)
In this case, the result will be
If you consider removing bottom line border: InputBorder.none and use Divider on Stack, you will get more control over UI.

Container(
width: 100.0,
child: TextField(
style: TextStyle(
fontSize: 40.0,
height: 2.0,
color: Colors.black
)
)
)

Related

Passing a function in onChanged Flutter

I'm having these 4 inputs (TextFormFields) , and I want to handle a change that whenver the user changes what is written inside , a function is triggered and updates the sum of another TextFormField.
I thought using the onChanged is the best way to handle these changes , but I'm unable to make it.
I have a TextEditingController that I want to update whenever there is a change in the inputs :
TextEditingController totalDocument;
void _CalculTotal() {
setState(() {
total = total + double.tryParse(widget.controllers.last.text);
totalDocument.text = total.toString();
});
}
These are my inputs inside a class :
class InputRefNomProduit extends StatefulWidget {
final String label, label2, label3, label4;
final String content, content2, content3, content4;
var fieldController = TextEditingController();
var fieldController2 = TextEditingController();
var fieldController3 = TextEditingController();
var fieldController4 = TextEditingController();
final FunctionStringCallback Prix;
FormFieldValidator<String> fieldValidator = (_) {};
InputRefNomProduit(
{this.label,
this.content,
this.label2,
this.content2,
this.label3,
this.content3,
this.label4,
this.content4,
this.fieldController,
this.fieldController2,
this.fieldValidator,
this.fieldController3,
this.fieldController4,
**this.Prix**});
. . .
Expanded(
flex: 3,
child: Container(
width: MediaQuery.of(context).size.width / 3.7,
color: Color.fromARGB(255, 255, 255, 255),
child: TextFormField(
**onChanged: widget.Prix,**
enabled: true,
controller: widget.fieldController4,
validator: widget.fieldValidator,
style: TextStyle(
fontSize: 15.0,
color: Colors.black,
),
decoration: InputDecoration(
contentPadding: EdgeInsets.all(10.0),
hintText: "${widget.content4}",
hintStyle: TextStyle(
color: Color.fromARGB(255, 190, 190, 190),
fontSize: 14),
fillColor: Color.fromARGB(255, 0, 0, 0),
),
),
),
),
The variable Prix is used to get the function _CalculTotal().
This is my sum TextFormField that will hold the total :
SizedBox(
width: 200,
child: Padding(
padding: const EdgeInsets.only(top: 15),
child: TextFormField(
**controller: totalDocument,**
keyboardType: TextInputType.text,
decoration: InputDecoration(
enabled: false,
hintText: '$total',
hintStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.w400,
color: Colors.grey.shade400,
),
prefixIcon: Icon(
Icons.attach_money_outlined,
color: Colors.grey.shade400),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
width: 1,
color: Colors.grey.shade200),
borderRadius:
BorderRadius.circular(5),
),
disabledBorder: OutlineInputBorder(
borderSide: BorderSide(
width: 1,
color: Colors.grey.shade200),
borderRadius:
BorderRadius.circular(5),
),
focusedBorder: OutlineInputBorder(
borderRadius:
BorderRadius.circular(5),
borderSide: BorderSide(
color: Colors.grey.shade400,
width: 1,
),
),
focusedErrorBorder: OutlineInputBorder(
borderRadius:
BorderRadius.circular(5),
borderSide: BorderSide(
color: Colors.grey.shade400,
width: 1,
),
),
errorBorder: OutlineInputBorder(
borderRadius:
BorderRadius.circular(5),
borderSide: BorderSide(
color: Colors.grey.shade400,
width: 1,
),
),
contentPadding:
EdgeInsets.fromLTRB(15, 0, 15, 0),
),
),
),
),
Use the addListener() method of your TextEditingController to listen to changes in the TextFormFields where you assigned your TextEditingController as a controller.
Checkout this flutter cookbook article for more details. It clearly explains the difference between setting the onChanged attribute of a TextFormField and using the addListener() of a controller to handle changes in your TextFields.
https://docs.flutter.dev/cookbook/forms/text-field-changes

Flutter Textformfield prefix text

I am trying to create a textformfield with a prefix text that looks the images below. This one is before entering any text
and this is after entering text
This is the code I have written
TextFormField(
decoration: InputDecoration(
prefixIcon: Text(
'+254',
textAlign: TextAlign.center,
style: theme.textTheme.bodyText2!.copyWith(
color: Color(0xff000000),
fontWeight: FontWeight.w500),
),
hintText: getTranslated(context, "label_hint")!,
hintStyle: theme.textTheme.bodyText2!.copyWith(
color: primaryLight.withOpacity(0.75),
fontWeight: FontWeight.w400,
),
fillColor: const Color(0xffF5F5F5),
enabledBorder: OutlineInputBorder(
borderSide:
const BorderSide(width: 1, color: Color(0xffCCCCCC)),
borderRadius: BorderRadius.circular(5),
),
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(width: 1, color: Color(0xffF38E30)),
borderRadius: BorderRadius.circular(5),
),
),
),
This is how my attempt looks
I'd like to achieve something close to the first two images, what can I change in my code
Try wrapping the text widget in prefixIcon: Text('+254', with a column, and give its main axis alignment a center. Or wrap your text widget with a Center widget.
Wrap Text widget in a Row widget and assign it's mainAxisSize property to MainAxisSize.min and mainAxisAlignment property to mainAxisAlignment.center to achieve the prefixText inline with hint text
try this code -
TextFormField(
decoration: InputDecoration(
prefixIcon: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Text(
'+254',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w400,
),
),
],
),
hintText: 'mobile number',
hintStyle: const TextStyle(
color: Colors.grey,
fontWeight: FontWeight.w400,
),
fillColor: const Color(0xffF5F5F5),
enabledBorder: OutlineInputBorder(
borderSide:
const BorderSide(width: 1, color: Color(0xffCCCCCC)),
borderRadius: BorderRadius.circular(5),
),
focusedBorder: OutlineInputBorder(
borderSide:
const BorderSide(width: 1, color: Color(0xffF38E30)),
borderRadius: BorderRadius.circular(5),
),
),
),

Flutter: How to underline in textformfield

I'm trying to make it such that the user can enter his pin, and the pin is supposed to be 4 digits long. Now, the 4 digits that he can enter should be underscored in the text form field (even before he enters the pin), somewhat like ____.
So something like this:
Basically just a normal input field that has the 4 underscores. Is there anyway I can do this in textformfield?
Thanks in advance!
I have tried this on Android Kotlin by using TextWatcher, and I think this could be done by this link below:
Here's a reference!
Try This : https://api.flutter.dev/flutter/material/UnderlineInputBorder-class.html
Full Code :
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key}) : super(key: key);
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('TextField Input Decoration Demo'),
),
body: new SafeArea(
top: true,
bottom: true,
child: Column(
children: [
Padding(
padding: EdgeInsets.all(20),
child: Row(
children: [
Container(
width: 40,
child: TextFormField(
style: TextStyle(fontSize: 50),
textInputAction: TextInputAction.next,
decoration: const InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.black,
width: 10.0,
)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.black,
width: 10.0,
),
),
),
)),
SizedBox(width: 5),
Container(
width: 40,
child: TextFormField(
style: TextStyle(fontSize: 50),
textInputAction: TextInputAction.next,
decoration: const InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.black,
width: 10.0,
)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.black,
width: 10.0,
),
),
),
)),
SizedBox(width: 5),
Container(
width: 40,
child: TextFormField(
style: TextStyle(fontSize: 50),
textInputAction: TextInputAction.next,
decoration: const InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.black,
width: 10.0,
)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.black,
width: 10.0,
),
),
),
)),
SizedBox(width: 5),
Container(
width: 40,
child: TextFormField(
style: TextStyle(fontSize: 50),
textInputAction: TextInputAction.next,
decoration: const InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.black,
width: 10.0,
)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.black,
width: 10.0,
),
),
),
)),
],
)),
new Divider(
color: Colors.black,
),
],
)));
}
}

Textfield text alignment is not aligned vertically

I am trying to build a form with custom textfield height, i would like to make the textfield text and hit text to be center vertically. Here is my code
SizedBox(
height: 40,
child:
TextField(
style: TextStyle(
fontSize: 14,
),
textAlignVertical: TextAlignVertical.center,
textAlign: TextAlign.left,
maxLines: 1,
decoration: InputDecoration(
filled: true,
fillColor: Color(0xff5a9fd6).withOpacity(0.15),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Color(0xff5a9fd6).withOpacity(1.0),
),
borderRadius: BorderRadius.circular(2.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.transparent,
),
borderRadius: BorderRadius.circular(1.0),
)
),
)
),
When i decrease the font size then the text is aligned vertically but i would like to use the font size as 14 and above.
Do not wrap the TextField from SizeBox to manage the height of the TextField. You can do this by using vertical contentPadding as follows. It keeps your text in the center.
TextField(
style: TextStyle(
fontSize: 14,
),
textAlignVertical: TextAlignVertical.center,
textAlign: TextAlign.left,
maxLines: 1,
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 50, horizontal: 20),
filled: true,
fillColor: Color(0xff5a9fd6).withOpacity(0.15),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Color(0xff5a9fd6).withOpacity(1.0),
),
borderRadius: BorderRadius.circular(2.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.transparent,
),
borderRadius: BorderRadius.circular(1.0),
)
),
),
You can set the contentPadding of the TextField as 0 or according to your requirement.
SizedBox(
height: 40,
child:
TextField(
style: TextStyle(
fontSize: 14,
),
textAlignVertical: TextAlignVertical.center,
textAlign: TextAlign.left,
maxLines: 1,
decoration: InputDecoration(
contentPadding: const EdgeInsets.all(0),
filled: true,
fillColor: Color(0xff5a9fd6).withOpacity(0.15),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Color(0xff5a9fd6).withOpacity(1.0),
),
borderRadius: BorderRadius.circular(2.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.transparent,
),
borderRadius: BorderRadius.circular(1.0),
)
),
)
),
You can to adjust contentPadding to center your text vertically, for example:
TextField(
decoration: InputDecoration(
contentPadding: const EdgeInsets.all(<your value>),
)

Flutter| While keyboard pops up there will be BottomOverFlowed. Need exact design as shown in image

When keyboard pops up, there will be bottom over flowed. I need to add a expanded card as background for login form and submit button.
And also need that expanded card to move up when there is keyboard.
I have checked through many other solution for keyboard popup, but I couldn't get exact solution for mine design.
return new Scaffold(
body: new Column(children: <Widget>[
SizedBox(
height: 70,
),
new Image.asset(
'assets/logo.png',
fit: BoxFit.cover,
),
new Text(
"Login",
style: TextStyle(fontSize: 20, color: Colors.white),
),
new Text(
"It has noon , but also the Type roman, remaining essentially unchanged for the wish i could do.",
textAlign: TextAlign.center,
),
Column(
children: <Widget>[
Expanded(
child: Container(
child: Container(
padding: EdgeInsets.all(24),
decoration: new BoxDecoration(
color: Colors.white,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(25.0),
topRight: const Radius.circular(25.0))),
child: new ListView(
children: <Widget>[
TextField(
style: new TextStyle(
color: Color(0xff651515),
),
autofocus: false,
obscureText: false,
keyboardType: TextInputType.text,
decoration: InputDecoration(
labelText: "email",
hintText: "email",
labelStyle: TextStyle(
color: Color(0xffa4a4a4),
fontSize: 14,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4)),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4)),
borderSide: BorderSide(
width: 2,
color: Color(0xff55c882),
style: BorderStyle.solid),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4)),
borderSide: BorderSide(
width: 1,
color: Color(0xffa4a4a4),
),
),
),
),
SizedBox(
height: 20,
),
TextField(
style: new TextStyle(
color: Color(0xff651515),
),
autofocus: false,
obscureText: false,
keyboardType: TextInputType.text,
decoration: InputDecoration(
labelText: "password",
hintText: "password",
labelStyle: TextStyle(
color: Color(0xffa4a4a4),
fontSize: 14,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4)),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4)),
borderSide: BorderSide(
width: 2,
color: Color(0xff55c882),
style: BorderStyle.solid),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4)),
borderSide: BorderSide(
width: 1,
color: Color(0xffa4a4a4),
),
),
),
),
],
),
),
))
],
)
]));
you can just wrap your column with a:
SingleChildScrollView(
child: put your column here
)
hope it helped