import 'dart:async'; import 'package:car_provider_app/utils/navigator.dart'; import 'package:car_provider_app/widgets/show_fill_button.dart'; import 'package:flutter/material.dart'; import 'package:geocoding/geocoding.dart'; import 'package:geolocator/geolocator.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; import 'package:permission_handler/permission_handler.dart'; import 'map_selection_widget.dart'; class PickLocationPage extends StatefulWidget { Function(double, double, String) onPickAddress; PickLocationPage({required this.onPickAddress}); @override State createState() => _PickLocationPageState(); } class _PickLocationPageState extends State { double latitude = 0; double longitude = 0; final Set markers = new Set(); late AppMap appMap; static CameraPosition _kGooglePlex = CameraPosition( target: LatLng(37.42796133580664, -122.085749655962), zoom: 14.4746, ); late LatLng currentPostion; Completer mapController = Completer(); final ValueNotifier _counter = ValueNotifier(""); @override void initState() { appMap = AppMap( _kGooglePlex.toMap() as Map, onCameraMove: (camera) { _updatePosition(camera); }, onMapCreated: () { // goToCurrentLocation(); _getUserLocation(); setState(() {}); }, ); super.initState(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Pick Location"), ), body: Container( child: Stack( children: [ if (appMap != null) appMap, ValueListenableBuilder( builder: (BuildContext context, String value, Widget? child) { // This builder will only get called when the _counter // is updated. return value.isNotEmpty ? Container( width: double.infinity, margin: EdgeInsets.all(12), child: Card( child: Padding( padding: const EdgeInsets.all(12.0), child: Text(value ?? ""), )), ) : Container(); }, valueListenable: _counter, ), Align( alignment: Alignment.center, child: Icon( Icons.place, color: Colors.red, size: 50, ), ), Align( alignment: Alignment.bottomCenter, child: Padding( padding: const EdgeInsets.all(12.0), child: ShowFillButton( title: "Pick Address", onPressed: () { widget.onPickAddress(latitude, longitude, _counter.value); pop(context); }, ), ), ), ], ), ), ); } Future _updatePosition(CameraPosition _position) async { latitude = _position.target.latitude; longitude = _position.target.longitude; List placemarks = await placemarkFromCoordinates(latitude, longitude); _counter.value = (placemarks.first.name) ?? "" + " " + (placemarks.first.street ?? "") + " " + (placemarks.first.country ?? ""); } void _getUserLocation() async { if (await Permission.location.request().isGranted) { var position = await GeolocatorPlatform.instance.getCurrentPosition(); currentPostion = LatLng(position.latitude, position.longitude); latitude = position.latitude; longitude = position.longitude; setMap(); } else { requestPermissions().then( (value) async { if (value[Permission.location]!.isGranted) { var position = await GeolocatorPlatform.instance.getCurrentPosition(); currentPostion = LatLng(position.latitude, position.longitude); latitude = position.latitude; longitude = position.longitude; setMap(); } }, ); } } setMap() { setState(() { _kGooglePlex = CameraPosition( target: currentPostion, zoom: 14.4746, ); appMap.moveTo(cameraPostion: _kGooglePlex); }); } Future getCurrentAddress() async { List placemarks = await placemarkFromCoordinates(latitude, longitude); return placemarks.first.name ?? ""; } } Future> requestPermissions() async { var permissionResults = [Permission.calendar].request(); return permissionResults; }