问题
I have 2 Text
widget inside an GestureDetector
. The onTap
callback only notify when I touch on the Text
but not the empty space inside my Container
. How to make this notify like I touch on a button?
+------------------------------------------------+
| Very very very long long ◎ng long text view |
| Short ◎xt ⦿ |
+------------------------------------------------+
- ◎: Print
Tapped
. - ⦿: Nothing happened.
My source code:
ListView.separated(
itemCount: _listModel.length,
padding: EdgeInsets.only(left: NOTIFICATION_LEFT_SPACE),
separatorBuilder: (context, index) => Divider(),
itemBuilder: (BuildContext context, int index) => GestureDetector(
child: Container(
child: Hero(
tag: _listModel[index].title,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Very very very long long long long text view'),
SizedBox(height: 10),
Text('Short text')
],
),
),
),
onTap: () {
print('Tapped');
},
),
)
回答1:
You can use behavior: HitTestBehavior.opaque
property of GestureDetector
widget, that helps to tap on the placeholder inside Container
even if Container
widget doesn't have any child.
GestureDetector(
behavior: HitTestBehavior.opaque,
child: Container(
child: Hero(
tag: 'test',
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Very very very long long long long text view'),
SizedBox(height: 10),
Text('Short text')
],
),
),
),
onTap: () {
print('Tapped');
},
),
来源:https://stackoverflow.com/questions/59062264/how-to-make-gesturedetector-also-work-when-touch-empty-space-in-flutter