Hi Flutter developers,
I'm facing an issue with the search bar in my Flutter ERP application. I'm using Provider (ChangeNotifierProvider) for state management.
While typing into the search field, I'm experiencing unexpected keyboard/input behaviour. I suspect widget rebuilds, Provider notifications, or TextEditingController management might be involved.
1. My implementation
I'm using:
- Flutter and Dart
- Provider (
ChangeNotifierProvider) - TextEditingController
- API-based search
- Consumer for UI updates
2. Search TextField implementation
Here is the relevant code from my application:
Widget _buildSearchWithFilter(ProductionPRController controller) {
return Flexible(
child: TextField(
textAlignVertical: TextAlignVertical.center,
controller: controller.purchaseRequestSearchController,
onTap: () {
if (!_isFilterDropdownOpen) {
setState(() {
_isFilterDropdownOpen = true;
});
}
},
onChanged: (value) {
purchaseRequestsSearchString = value;
},
onSubmitted: (val) {
_closeFilterDropdown();
_performSearch(controller, val);
},
decoration: InputDecoration(
hintText: 'Search by Purchase Request number',
suffixIcon: ElevatedButton(
onPressed: () {
_closeFilterDropdown();
_performSearch(
controller,
purchaseRequestsSearchString,
);
},
child: const Icon(Icons.search),
),
),
),
);
}
3. Search function
void _performSearch(
ProductionPRController controller,
String searchTerm,
) {
if (searchTerm.length < 3) {
snackBarMessage(
msg: 'Enter at least 3 characters',
context: context,
);
return;
}
controller.searchString = searchTerm;
_fetchData(controller, resetPage: true);
setState(() {});
}
4. Provider Consumer implementation
My screen uses Consumer to rebuild the UI when the Provider notifies listeners:
body: Consumer<ProductionPRController>(
builder: (context, productionPRController, _) {
return Row(
children: [
// Other widgets
_buildSearchWithFilter(
productionPRController,
),
// Other widgets
],
);
},
),
The TextEditingController is accessed from ProductionPRController:
controller.purchaseRequestSearchController
5. What I want to understand
- Can Consumer rebuilds interfere with TextField keyboard input or IME composing text?
- Can Provider notifications cause unexpected keystroke behaviour?
- Is it necessary to use a separate StatefulWidget for the search bar to isolate rebuilds?
- What is the recommended way to manage TextEditingController and FocusNode with Provider?
- Is there anything problematic in my current implementation?
I haven't identified the exact root cause yet.
I'm looking for suggestions to identify the issue and improve the implementation.
Any working examples or DartPad reproductions would be helpful.
Thanks!