dropdown_button2
dropdown_button2 copied to clipboard
[Bug] Keyboard opens and closes when clicking on the search bar in web while in mobile
Steps to reproduce
- Use the example in flutter web and open the web app in a mobile device.
- Open the dropdown and click on the searchbar.
Expected results
- When the searchbar is clicked inside the dropdown, the keyboard opens and stays open.
Actual results:
- When the searchbar is clicked inside the dropdown, the keyboard opens and closes rendering you unable to even type any text in it.
Code sample:
final List<String> items = [
'A_Item1',
'A_Item2',
'A_Item3',
'A_Item4',
'B_Item1',
'B_Item2',
'B_Item3',
'B_Item4',
];
final valueListenable = ValueNotifier<String?>(null);
final TextEditingController textEditingController = TextEditingController();
@override
void dispose() {
textEditingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: DropdownButtonHideUnderline(
child: DropdownButton2<String>(
isExpanded: true,
hint: Text(
'Select Item',
style: TextStyle(
fontSize: 14,
color: Theme.of(context).hintColor,
),
),
items: items
.map((item) => DropdownItem(
value: item,
height: 40,
child: Text(
item,
style: const TextStyle(
fontSize: 14,
),
),
))
.toList(),
valueListenable: valueListenable,
onChanged: (value) {
valueListenable.value = value;
},
buttonStyleData: const ButtonStyleData(
padding: EdgeInsets.symmetric(horizontal: 16),
height: 40,
width: 200,
),
dropdownStyleData: const DropdownStyleData(
maxHeight: 200,
),
dropdownSearchData: DropdownSearchData(
searchController: textEditingController,
searchBarWidgetHeight: 50,
searchBarWidget: Container(
height: 50,
padding: const EdgeInsets.only(
top: 8,
bottom: 4,
right: 8,
left: 8,
),
child: TextFormField(
expands: true,
maxLines: null,
controller: textEditingController,
decoration: InputDecoration(
isDense: true,
contentPadding: const EdgeInsets.symmetric(
horizontal: 10,
vertical: 8,
),
hintText: 'Search for an item...',
hintStyle: const TextStyle(fontSize: 12),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
),
noResultsWidget: const Padding(
padding: EdgeInsets.all(8),
child: Text('No Item Found!'),
),
searchMatchFn: (item, searchValue) {
return item.value.toString().contains(searchValue);
},
),
//This to clear the search value when you close the menu
onMenuStateChange: (isOpen) {
if (!isOpen) {
textEditingController.clear();
}
},
),
),
),
);
}
Details:
- dropdown_button2: 3.0.0-beta.22
- flutter: 3.29.2
- dart: 3.7.2
Update: Using TextField instead of TextFormField fixes the issue, however, having autofocus: true doesn't automatically open the keyboard when opening the dropdown.
I couldn't reproduce it. Can you provide more details?
https://github.com/user-attachments/assets/edd20bc1-779a-4e9d-a5ea-f54bda0bb350
Interestingly, I didn't test iOS web, I will check again and let you know, I was able to reproduce on Android web.
@AhmedLSayed9 I was not able to reproduce on iOS, this seems like an Android bug