The Problem
I need to detect when a dropdown button was tapped so that I can remove focus from some TextField
s in my view.
What I have done so far
I tried to propagate a tap event through a GestureDetector
to the DropdownButton
. But the HitTestBehavior
does not work as mentioned in the docs.
class MainApplication extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeView(),
);
}
}
class HomeView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Data'),
),
body: Center(
child: Stack(
children: [
DropdownButton<String>(
onChanged: (value) => print(value),
items: ['Apple', 'Orange', 'Carrot'].map((item) {
return DropdownMenuItem<String>(
child: Text(item),
value: item,
);
}).toList(),
),
Positioned.fill(
child: GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
print('Tapped GD');
},
child: Container(
color: Colors.green.withAlpha(60),
),
),
),
],
),
),
);
}
}
Ideal Result
When I tap on the button, the onTap
callback should fire and the dropdown should expand simulaneously.