TextField (
decoration: InputDecoration(
hintText: 'Need extra ice or have another request?',
contentPadding: EdgeInsets.all(10),
),
//else {
// decoration: InputDecoration(
// hintText: Provider.of<OrderProvider>(context, listen: false).specialNotes,
// contentPadding: EdgeInsets.all(10),
// ),}
onChanged: (newText) {
Provider.of<OrderProvider>(context, listen: false)
.updateSpecialRequests(newText);
},
keyboardType: TextInputType.multiline,
maxLines: null,
),
I want to make a conditional statement that determines whether the code above the commented code is read or the commented code below it gets read. Textfield is an array of children. However I try to do this, it breaks my code. The TextField invariably is no longer lit up in gold as it is when the code is working. The online suggestions have not been working.
You can use tenary operators
TextField (
decoration: condition ? InputDecoration(
hintText: 'Need extra ice or have another request?',
contentPadding: EdgeInsets.all(10),
): InputDecoration(
hintText: Provider.of<OrderProvider>.specialNotes,
contentPadding: EdgeInsets.all(10),
),
onChanged: (newText) {
Provider.of<OrderProvider>(context, listen: false)
.updateSpecialRequests(newText);
},
keyboardType: TextInputType.multiline,
maxLines: null,
),
Related
There is a problem in Android Web Chrome. When Text inputType text or name does not work onsubmit and it is not possible to send a request. But for some reason, if you set the TextInputType.emailAddress parameter, then the submit key on the keyboard becomes a "right arrow" and everything works as it should work. What could be the reason for this behavior?
TextFormField(
enabled: isEnabled ?? true,
obscureText: isObscured ?? false,
controller: controller,
keyboardType: widget.keyboardType,
textInputAction: widget.textInputAction,
minLines: widget.minLines ?? 1,
maxLines: widget.maxLines ?? 1,
decoration: InputDecoration(
fillColor: AppColors.fieldBackground,
filled: true,
hintText: widget.hintText,
contentPadding: const EdgeInsets.only(
top: (Dimens.buttonHeight - 14) / 2,
bottom: (Dimens.buttonHeight - 14) / 2,
left: Dimens.horizontalPadding,
right: Dimens.horizontalPadding,
),
onChanged: (value) {
setState(() {
textLength = value.length;
});
},
focusNode: focusNode,
onFieldSubmitted: (query) {
if (query.length > 2 || query.isEmpty) {
serviceLocator<SearchBloc>().add(
FiltersChanged(query: query),
);
FocusScope.of(context).unfocus();
} else {
Messages.showInfoMessage(
context,
AppLocalizations.of(context)!.searchFieldShortQueryTitle,
AppLocalizations.of(context)!.searchFieldShortQueryMessage);
}
},
);
You can set the input action manually and it should work as expected
TextField(
textInputAction: TextInputAction.go,
)
How can I display a hint text in a TextField if it contains only space characters like ' '?
const TextField(
decoration: InputDecoration(
hintText: 'a hint text'
),
),
In this code example the hint text is only displayed if the content of the TextField is empty.
I need this because I need to detect when the user press the backspace button on an empty TextField. Read more here: https://medium.com/super-declarative/why-you-cant-detect-a-delete-action-in-an-empty-flutter-text-field-3cf53e47b631
After diving deeper into the problem I found a solution with InputDecorator:
const InputDecorator(
isEmpty: true, // if true, the hint text is shown
decoration: InputDecoration(
hintText: 'a hint text'
),
child: TextField(),
),
So my solution is this:
String _value = ''; // state variable
...
InputDecorator(
isEmpty: _value.trim().isEmpty, // override the default isEmpty behavior
decoration: const InputDecoration(
hintText: 'a hint text',
),
child: TextField(
onChanged: (v) => setState(() => _value = v),
),
),
Probably, you should use decoration property of your TextField.
Something like that:
SizedBox(
height: 50,
width: 100,
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
label: Text('Your stable hint')
),
)
),
Everything works fine except the space between the elements/user input and bottom bar. I tried different methods to get rid of that space: content padding, box constraints, prefix icon constraints etc. None worked. Eventually I limited the height of this widget, but then my error message was placed on top of user input and I was getting this message Another exception was thrown: BoxConstraints has a negative minimum height.
Here is the code:
Widget _emailField(FormBloc formBloc) {
return StreamBuilder(
stream: formBloc.email,
builder: (context, AsyncSnapshot<String> snapshot) {
return TextFormField(
autofocus: false,
keyboardType: TextInputType.emailAddress,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null) {
return "Email cannot be empty";
} else {
if (value.isEmpty) {
return "Email cannot be empty";
}
}
},
onChanged: formBloc.changeEmail,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
prefixIcon: const Icon(
Icons.email,
size: 15,
),
contentPadding: const EdgeInsets.all(0),
labelText: "Email",
labelStyle: const TextStyle(fontSize: 14),
errorText:
snapshot.error != null ? snapshot.error as String : null,
),
);
}
);
}
I need to make everything work as before, except this time I do need to have no space between bottom border and input elements, how can I do so?
First inside InputDecoration(), you can give zero padding to your prefix icon.
Then set prefixIconConstraints to BoxConstraints(maxHeight:0)
Still isn't enough you can give negative value to contentPadding: EdgeInsets.only(bottom:-5),.
InputDecoration(
prefixIcon: Padding(
padding: EdgeInsets.only(right:10),
child:const Icon(
Icons.email,
size: 15,
),
),
prefixIconConstraints: BoxConstraints(maxHeight:0),
contentPadding: EdgeInsets.only(bottom:-5),
labelText: "Email",
labelStyle: const TextStyle(fontSize: 14),
errorText:"error",
)
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;
});
},
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),
)