Home > Software engineering >  Can't receive tap events on google maps widget flutter
Can't receive tap events on google maps widget flutter

Time:02-03

Here's the code that I have so far

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class TempView extends StatefulWidget {
  const TempView({Key? key}) : super(key: key);

  @override
  State<TempView> createState() => _TempViewState();
}

class _TempViewState extends State<TempView> {
  final Completer<GoogleMapController> _controller =
      Completer<GoogleMapController>();

  static const CameraPosition _kGooglePlex = CameraPosition(
    target: LatLng(37.42796133580664, -122.085749655962),
    zoom: 14.4746,
  );

  static const CameraPosition _kLake = CameraPosition(
      bearing: 192.8334901395799,
      target: LatLng(37.43296265331129, -122.08832357078792),
      tilt: 59.440717697143555,
      zoom: 19.151926040649414);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GoogleMap(
        onTap: (argument) => print("tapped"),
        mapType: MapType.hybrid,
        initialCameraPosition: _kGooglePlex,
        onMapCreated: (GoogleMapController controller) {
          _controller.complete(controller);
        },
      ),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: _goToTheLake,
        label: const Text('To the lake!'),
        icon: const Icon(Icons.directions_boat),
      ),
    );
  }

  Future<void> _goToTheLake() async {
    final GoogleMapController controller = await _controller.future;
    controller.animateCamera(CameraUpdate.newCameraPosition(_kLake));
  }
}

It's the same as the code on the pub.dev page for the google_maps_flutter widget, except I added an onTap.

For some reason the onTap never fires! I've even tried adding custom gesture recognisers into the gestureRecognizers set, like the EagerGestureRecognizer and the TapGestureRecognizer.

I'm on Flutter 3.7 macOS with Apple M1.

Why isn't it working, and how can I get around this?

CodePudding user response:

Wrap GoogleMap widget with InkWell Widget and use its property of onTap, it will work.

InkWell( onTap: () => , child: ),

CodePudding user response:

I ran your code and it seems to run just fine. tapped gets printed every time i tap on map.

Please make sure your tap is in GoogleMap region and that no other widget is overlaying if this isn’t the whole code.

  • Related