Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google map in Flutter not responding to touch events

I got two different screens inside Flutter app that use Google Map (provided by this library).

Widget structure for first one is

Scaffold -> ListView -> Visibility -> Center -> SizedBox -> GoogleMap

and for second screen it is

Scaffold -> Container -> Column -> SizedBox -> GoogleMap

On both screens I got same map settings but for some reason on the first screen map is not responding to any touch events.

like image 400
moonvader Avatar asked Jan 20 '19 20:01

moonvader


3 Answers

I was with the same problem and I found the following solution after a long time of searching, just so it will work:

GoogleMap(
   gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>[
      new Factory<OneSequenceGestureRecognizer>(() => new EagerGestureRecognizer(),),
   ].toSet(),)

The reason is that the EagerGestureRecognizer is a gesture recognizer that eagerly claims victory in all gesture arenas.

Reference:

Manage gestures priority between two widgets

propriedade gestureRecognizers

like image 151
Marcelo Petrucio Avatar answered Oct 11 '22 15:10

Marcelo Petrucio


You need to tell the GoogleMap widget which gestures you want it to respond to by setting the gestureRecognizers property, something like this:

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

...

GoogleMap(
  initialCameraPosition: CameraPosition(
    target: LatLng(0, 0),
  ),
  gestureRecognizers: Set()
    ..add(Factory<PanGestureRecognizer>(() => PanGestureRecognizer()))
);

This is not specific to the GoogleMap widget, you would need to do this with any widget that uses AndroidView/UIKitView under the covers to handle gestures when it's placed inside a scrollable view.

like image 43
Jordan Davies Avatar answered Oct 11 '22 15:10

Jordan Davies


I think it depends of what kind of gestures your parent widget support.

This way works for me. My map is in TabBarView > ListView > GoogleMap

 GoogleMap(
      gestureRecognizers: Set()
        ..add(Factory<PanGestureRecognizer>(() => PanGestureRecognizer()))
        ..add(Factory<ScaleGestureRecognizer>(() => ScaleGestureRecognizer()))
        ..add(Factory<TapGestureRecognizer>(() => TapGestureRecognizer()))
        ..add(Factory<VerticalDragGestureRecognizer>(
            () => VerticalDragGestureRecognizer())),
like image 30
siega Avatar answered Oct 11 '22 13:10

siega