115 lines
3.6 KiB
Dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:flutter_google_places/flutter_google_places.dart';
import 'package:google_maps_webservice/places.dart';
import 'package:google_api_headers/google_api_headers.dart';
class Map extends StatefulWidget {
@override
State<Map> createState() => MapState();
}
const kGoogleApiKey = "AIzaSyAUmhd6Xxud8SwgDxJ4LlYlcntm01FGoSk";
final homeSacffoldKey = GlobalKey<ScaffoldState>();
class MapState extends State<Map> {
final Completer<GoogleMapController> _controller = Completer();
final TextEditingController _searchController = TextEditingController();
static const CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(55.427320, 13.819257),
zoom: 14.4746,
);
Set<Marker> markersList = {};
late GoogleMapController googleMapController;
final Mode _mode = Mode.fullscreen;
int currentIndex = 0;
final screens =[
Map(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
key: homeSacffoldKey,
//leading: IconButton(icon: Icon(Icons.search), onPressed:() {},),
actions: <Widget>[
IconButton(icon: const Icon(Icons.search), onPressed:() {
},),
],
title: TextFormField(
controller: _searchController,
textCapitalization: TextCapitalization.words,
decoration: const InputDecoration(hintText: 'Find your place'),
onChanged: (value) {
print(value);
},
),
backgroundColor: const Color.fromARGB(255, 190, 146, 160),
),
body: Stack (
children: [
GoogleMap(
mapType: MapType.normal,
initialCameraPosition: _kGooglePlex,
markers: markersList,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
ElevatedButton(onPressed: _handelPressButton
,child: const Text("Search Placses"))
],
)
);
}
Future<void> _handelPressButton() async {
Prediction? p = await PlacesAutocomplete.show(
context: context,
apiKey: kGoogleApiKey,
mode: _mode, // Mode.fullscreen
language: "en",
strictbounds: false,
decoration: InputDecoration(
hintText:'serach',
focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(20), borderSide: BorderSide(color: Colors.white))
),
types: [""],
components: [Component(Component.country, "se")]);
if (p != null) {
displayPrediction(p,homeSacffoldKey.currentState);
}
}
Future<void> displayPrediction(Prediction p, ScaffoldState? currentState) async {
GoogleMapsPlaces places = GoogleMapsPlaces(
apiKey: kGoogleApiKey,
apiHeaders: await const GoogleApiHeaders().getHeaders()
);
PlacesDetailsResponse detail = await places.getDetailsByPlaceId(p.placeId!);
final lat = detail.result.geometry!.location.lat;
final lng = detail.result.geometry!.location.lng;
markersList.clear();
markersList.add(Marker(markerId: const MarkerId("0"), position: LatLng(lat, lng), infoWindow: InfoWindow(title: detail.result.name)));
setState(() {});
googleMapController.animateCamera(CameraUpdate.newLatLngZoom(LatLng(lat,lng), 14.0));
}
}