Issue
I have a prefix icon in TextField. After tapping it , I get a string from service and by provider and Consumer I attached this string into TextField controller.
ChangeNotifierProvider(
create: (context) => provider,
child: Consumer<FacilitiesProvider>(
builder: (context, value, child) {
if (value.dateString != null &&
value.dateString.isNotEmpty) {
_controller.text = value.dateString;
}
return TextField(
controller: _controller,
Everything is ok .The problem is here.
When user tapped on TextField, soft keyboard is opened and when user deleted string and close the soft keyboard, Consumer is triggered and again value.dateString
set in _controller and again string is shown!!!
How can I prevent this from happening?
Solution
The problem is that your Widget tree is rebuilt when the keyboard shows/hides, causing your build method to be called again. The best solution is to handle the value assignment elsewhere, but if you are not concerned with the keyboard hiding part of the screen, you can set resizeToAvoidBottomPadding
in your Scaffold
to false
. If this does cover anything up, you can wrap the body
in a SingleChildScrollView
.
Answered By – Lee3
Answer Checked By – Gilberto Lyons (FlutterFixes Admin)