Input text being cutoff with overflow - flutter

Hi I'm new to flutter so this may be a trivial problem, but I have a search box which contains an TextField and an Icon.
It works fine while the input text is less than the width of the input box, but once it exceeds that it seems to get chopped in half such that only the top half of it can be seen.
From what I understand Flutter is supposed to automatically deal with overflows. This is the build method for a generalised Input widget which is what I use along with the Icon in a Row. It's used within a Row but I don't believe that is the issue.
This is what it looks like when the text overflows.
return Expanded(
child: TextField(
autofocus: autofocus,
style: Theme.of(context).textTheme.bodyText2,
cursorColor: Theme.of(context).cursorColor,
maxLines: 1,
controller: controller,
decoration: InputDecoration(
border: InputBorder.none,
hintText: placeholder,
hintStyle: TextStyle(color: Theme.of(context).hintColor),
),
onChanged: (String value) {
onChange(value);
},
onSubmitted: (String value) {
onChange(value);
},
),
);
This is the GitHub repo in case you want to see the contextual usage. It's used in lib/routes/MainPage.dart as well as lib/routes/SearchPage.dart.

It turns out it was being cutoff due to the contentPadding on the InputDecoration. For those of you who are facing the same issue, change decoration property to:
InputDecoration(
contentPadding: EdgeInsets.zero, // Removes padding
isDense: true, // Centers the text
border: InputBorder.none,
hintText: placeholder,
hintStyle: TextStyle(color: Theme.of(context).hintColor),
)

Related

onEditingComplete not working when there is a click on the check mark in the bottom right of keyboard

Here is the code:
TextField(
controller: _textEditingController,
cursorColor: Colors.white70,
autofocus: true,
textCapitalization:
TextCapitalization.sentences,
decoration: const InputDecoration(
fillColor: Color(0xFF616161),
filled: true,
contentPadding: EdgeInsets.all(
25,
),
border: InputBorder.none,
hintText:
'Enter a title to remember your scan',
hintStyle: TextStyle(
color: Colors.white70,
),
),
style: const TextStyle(
color: Colors.white,
),
onEditingComplete: () {
print('yes');
},
),
the print statement passed to the onEditingComplete parameter's function doesn't execute when tapped on the check mark in the keyboard placed in the bottom right. Am I doing something wrong?
The code you provided us with works perfectly fine, if there's a problem it might be something else overriding it or simply a bug, try flutter clean, and restarting the whole app instead of hot-reload.
Anyway I assume you want to pass the written text into a variable when the user is done editing. For that it's better to use onChanged. this will save the text every time its changed, which makes it a lot easier for the user, pretty much nobody uses the finish button on the keyboard these days, they just hop from textfield to textfield.
onChanged: (value) {
setState(() {
inputText = value;
});
},

How to make TextField which has 2 hintText?

I have ui of text field from Figma.
There are 2 hint texts where 1st hintText is located at the beginning of textfield and other one is at the end.
As can be seen, trailing is hint text which is disappearing when user inputs number.
If you have any idea please share how to make this kind of text field)
Try this:
TextField(
textAlign: TextAlign.end,
decoration: InputDecoration(
prefixIcon: Padding(
padding: EdgeInsets.all(15), child: Text('Prefix')),
border: OutlineInputBorder(),
hintText: "Cym",
),
),
You can just use the trailing for the hint and can you onChanged method to handle changes.. Like you will be managing your text on the behavior of input. For example if the input is empty then you text will be something like "A" if you input something text will be changed.
TextFormField(
decoration: InputDecoration(
suffix: Text(text)
),
onChanged: (value) {
// handle or change your text here
if(value.isEmpty){
setState(() { text = "A"});
}
else{
setState(() { myState = "any text"});
}
},
)

How to invert the color of TextFormField in flutter?

I changed my application color. However TextFormField have got very invisible.
Are there any way to modify the TextFormField as it is better.
Thanks.
LoVe is correct. There are constructors for the TextFormField class that you can use here: Flutter documentation. Another example:
TextFormField(
decoration: InputDecoration(
filled: true,
fillColor: Colors.lightBlue[50],
border: OutlineInputBorder(),
icon: Icon(Icons.person),
hintText: 'What do people call you?',
labelText: 'Name',
),
onSaved: (String value) {
// This optional block of code can be used to run
// code when the user saves the form.
},
),
you can change the style and fill color to make it fit with the current theme:
TextFormField(
style: someTextStyle,
decoration: InputDecoration(
hintStyle:someTextStyle
fillColor: someColor,
filled: true,
),
)

Change focus to next textfield in flutter

I have a code for making Text Fields as shown below
...Column(
children: <Widget>[
textField(text: 'User Name'),
SizedBox(height: 20),
textField(text: 'Password'), ])...
textField is a class that I made that has the following code to make a textfield. I did this to avoid code duplication,since I use textfields a lot in my applicatin.
...TextField(
textInputAction: TextInputAction.next,
obscureText: text == 'Password' ? true : false,
textCapitalization: TextCapitalization.words,
cursorColor: primaryColor,
textAlign: TextAlign.center,
decoration: InputDecoration(
labelText: text,
labelStyle: TextStyle(color: primaryColor, fontFamily: 'Bellotta'),
filled: true,
fillColor: Colors.white,
border: OutlineInputBorder(...
Since I use this function to create the text field I cant use focusNode to change focus.
How can I solve this issue??
u can specify two more parameter one for focusNode of current textfield & another one for focus u want to change e.g.
textField(
text: 'User Name',
focusNode: // your focus node,
nextFocusNode: // your next field focus node
),
now u can use :
FocusScope.of(context).requestFocus(nextFocus);
tip: if u want to decrease code duplication then instead of creating textfield method u should store ur InputDecoration in var/ method.

How to update keyboardType in Flutter (in the app)?

In normal Android development, you can change the keyboard type dynamically but I can't seem to find a way to do so in Flutter.
TextInputType roomKeyboardType = TextInputType.text; // right on top of the build function
My custom form field:
Padding(
padding: const EdgeInsets.all(8.0),
child: FormBuilderTextField(
attribute: "room",
controller: widget.controllers[1],
keyboardType: roomKeyboardType,
decoration: InputDecoration(
filled: true,
labelText: "Room",
prefixIcon: Icon(Icons.location_on, color: Colors.black),
suffixIcon: IconButton(
icon: Icon(Icons.keyboard, color: Colors.black),
onPressed: () {
if (roomKeyboardType == TextInputType.text) {
roomKeyboardType = TextInputType.number;
return;
}
roomKeyboardType = TextInputType.text;
},
)),
validators: [
FormBuilderValidators.required(
errorText: "Please enter the room number",
),
],
),
),
Right now, I have something that looks like this with a keyboard IconButton on the end of the form field. When I click on it, I expect my keyboard type to change, yet when I do, nothing happens. It's supposed to toggle between text and number keyboard types and it only stays on text as that's what I have the variable initially set to. The variable I know is changing, it's just the fact that Flutter probably isn't remaking the widget so the keyboard stays the same. Is there any way to get around this and am I able to change the keyboardType dynamically?
you can change your keyboard type in TextFieldWidgets and many input widgets
like this:
TextField(
keyboardType: TextInputType.number,
hintText: '6 Digit-Code',
),