Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make GestureDetector also work when touch empty space in Flutter

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');
    },
  ),
)
like image 533
Trần Đức Tâm Avatar asked Nov 27 '19 02:11

Trần Đức Tâm


People also ask

What is the difference between inkwell and GestureDetector in Flutter?

They both provide many common features like onTap , onLongPress etc. The main difference is GestureDetector provides more controls like dragging etc. on the other hand it doesn't include ripple effect tap, which InkWell does.

What happens if there are two or more gestures happening at the same time in Flutter?

Long story short, when multiple gestures are detected for the same widget (tree), Flutter will decide which GestureDetector will handle the event based on a combination of factors.

How do you increase touch area in Flutter?

ExpandTapWidget can be wrapped inside/outside GestureDetector: To avoid the conflict of gesture, it is better to apply on the innermost/smallest widget. The expanded area wouldn't exceed the boundary of the Parent Box. You can set debugPaintExpandAreaEnabled = true to display the actual expanded area.


1 Answers

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');
                },
              ),
like image 117
Darshan Avatar answered Sep 30 '22 01:09

Darshan