Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"geolocator_apple","path":"/Users/lzyct/.pub-cache/hosted/pub.dev/geolocator_apple-2.3.13/","shared_darwin_source":true,"native_build":true,"dependencies":[],"dev_dependency":false},{"name":"google_maps_flutter_ios","path":"/Users/lzyct/.pub-cache/hosted/pub.dev/google_maps_flutter_ios-2.15.2/","native_build":true,"dependencies":[],"dev_dependency":false}],"android":[{"name":"flutter_plugin_android_lifecycle","path":"/Users/lzyct/.pub-cache/hosted/pub.dev/flutter_plugin_android_lifecycle-2.0.28/","native_build":true,"dependencies":[],"dev_dependency":false},{"name":"geolocator_android","path":"/Users/lzyct/.pub-cache/hosted/pub.dev/geolocator_android-5.0.1+1/","native_build":true,"dependencies":[],"dev_dependency":false},{"name":"google_maps_flutter_android","path":"/Users/lzyct/.pub-cache/hosted/pub.dev/google_maps_flutter_android-2.16.1/","native_build":true,"dependencies":["flutter_plugin_android_lifecycle"],"dev_dependency":false}],"macos":[{"name":"geolocator_apple","path":"/Users/lzyct/.pub-cache/hosted/pub.dev/geolocator_apple-2.3.13/","shared_darwin_source":true,"native_build":true,"dependencies":[],"dev_dependency":false}],"linux":[],"windows":[{"name":"geolocator_windows","path":"/Users/lzyct/.pub-cache/hosted/pub.dev/geolocator_windows-0.2.5/","native_build":true,"dependencies":[],"dev_dependency":false}],"web":[{"name":"geolocator_web","path":"/Users/lzyct/.pub-cache/hosted/pub.dev/geolocator_web-4.1.3/","dependencies":[],"dev_dependency":false},{"name":"google_maps_flutter_web","path":"/Users/lzyct/.pub-cache/hosted/pub.dev/google_maps_flutter_web-0.5.12/","dependencies":[],"dev_dependency":false}]},"dependencyGraph":[{"name":"flutter_plugin_android_lifecycle","dependencies":[]},{"name":"geolocator","dependencies":["geolocator_android","geolocator_apple","geolocator_web","geolocator_windows"]},{"name":"geolocator_android","dependencies":[]},{"name":"geolocator_apple","dependencies":[]},{"name":"geolocator_web","dependencies":[]},{"name":"geolocator_windows","dependencies":[]},{"name":"google_maps_flutter","dependencies":["google_maps_flutter_android","google_maps_flutter_ios","google_maps_flutter_web"]},{"name":"google_maps_flutter_android","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"google_maps_flutter_ios","dependencies":[]},{"name":"google_maps_flutter_web","dependencies":[]}],"date_created":"2025-05-03 17:26:41.464963","version":"3.29.3","swift_package_manager_enabled":{"ios":false,"macos":false}}
2 changes: 1 addition & 1 deletion example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PickerDemoState extends State<PickerDemo> {
return Scaffold(
appBar: AppBar(title: const Text('Picker Example')),
body: Center(
child: FlatButton(
child: TextButton(
child: Text("Pick Delivery location"),
onPressed: () {
showPlacePicker();
Expand Down
8 changes: 8 additions & 0 deletions lib/entities/app_bar_options.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:flutter/material.dart';

class AppBarOptions {
final bool showBackButton;
final Widget? leading;

AppBarOptions({this.showBackButton = false, this.leading});
}
1 change: 1 addition & 0 deletions lib/entities/entities.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export 'address_component.dart';
export 'app_bar_options.dart';
export 'auto_complete_item.dart';
export 'location_result.dart';
export 'near_by_place.dart';
3 changes: 2 additions & 1 deletion lib/entities/localization_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class LocalizationItem {
String noResultsFound;
String unnamedLocation;
String tapToSelectLocation;

String searchHint;

LocalizationItem({
this.languageCode = 'en_us',
Expand All @@ -14,5 +14,6 @@ class LocalizationItem {
this.noResultsFound = 'No results found',
this.unnamedLocation = 'Unnamed location',
this.tapToSelectLocation = 'Tap to select this location',
this.searchHint = 'Search place',
});
}
81 changes: 43 additions & 38 deletions lib/widgets/place_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_api_headers/google_api_headers.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:http/http.dart' as http;
import 'package:place_picker/entities/entities.dart';
Expand All @@ -23,6 +24,7 @@ class PlacePicker extends StatefulWidget {
/// API key generated from Google Cloud Console. You can get an API key
/// [here](https://cloud.google.com/maps-platform/)
final String apiKey;
final AppBarOptions? appBarOptions;

/// Location to be displayed when screen is showed. If this is set or not null, the
/// map does not pan to the user's current location.
Expand All @@ -31,7 +33,10 @@ class PlacePicker extends StatefulWidget {
LatLng defaultLocation = LatLng(10.5381264, 73.8827201);

PlacePicker(this.apiKey,
{this.displayLocation, this.localizationItem, LatLng? defaultLocation}) {
{this.displayLocation,
this.localizationItem,
LatLng? defaultLocation,
this.appBarOptions}) {
if (this.localizationItem == null) {
this.localizationItem = new LocalizationItem();
}
Expand Down Expand Up @@ -90,14 +95,9 @@ class PlacePickerState extends State<PlacePicker> {
super.initState();
if (widget.displayLocation == null) {
_getCurrentLocation().then((value) {
if (value != null) {
setState(() {
_currentLocation = value;
});
} else {
//Navigator.of(context).pop(null);
print("getting current location null");
}
setState(() {
_currentLocation = value;
});
setState(() {
_loadMap = true;
});
Expand Down Expand Up @@ -131,22 +131,26 @@ class PlacePickerState extends State<PlacePicker> {

@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {
if (Platform.isAndroid) {
locationResult = null;
_delayedPop();
return Future.value(false);
} else {
return Future.value(true);
return PopScope(
canPop: false,
onPopInvokedWithResult: (didPop, result) {
if (!didPop) {
if (Platform.isAndroid) {
locationResult = null;
_delayedPop();
} else {
Navigator.of(context).pop(result);
}
}
},
child: Scaffold(
appBar: AppBar(
key: this.appBarKey,
title: SearchInput(searchPlace),
title: SearchInput(searchPlace, widget.localizationItem!),
centerTitle: true,
automaticallyImplyLeading: false,
leading: widget.appBarOptions?.leading,
automaticallyImplyLeading:
widget.appBarOptions?.showBackButton ?? false,
),
body: Column(
children: <Widget>[
Expand Down Expand Up @@ -193,7 +197,8 @@ class PlacePickerState extends State<PlacePicker> {
Padding(
child: Text(widget.localizationItem!.nearBy,
style: TextStyle(fontSize: 16)),
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 8),
padding:
EdgeInsets.symmetric(horizontal: 24, vertical: 8),
),
Expanded(
child: ListView(
Expand Down Expand Up @@ -236,10 +241,6 @@ class PlacePickerState extends State<PlacePicker> {

previousSearchTerm = place;

if (context == null) {
return;
}

clearOverlay();

setState(() {
Expand Down Expand Up @@ -281,7 +282,7 @@ class PlacePickerState extends State<PlacePicker> {
),
);

Overlay.of(context)?.insert(this.overlayEntry!);
Overlay.of(context).insert(this.overlayEntry!);

autoCompleteSearch(place);
}
Expand All @@ -291,6 +292,7 @@ class PlacePickerState extends State<PlacePicker> {
try {
place = place.replaceAll(" ", "+");

final headers = await const GoogleApiHeaders().getHeaders();
var endpoint =
"https://maps.googleapis.com/maps/api/place/autocomplete/json?"
"key=${widget.apiKey}&"
Expand All @@ -302,7 +304,7 @@ class PlacePickerState extends State<PlacePicker> {
"${this.locationResult!.latLng?.longitude}";
}

final response = await http.get(Uri.parse(endpoint));
final response = await http.get(Uri.parse(endpoint), headers: headers);

if (response.statusCode != 200) {
throw Error();
Expand Down Expand Up @@ -353,12 +355,13 @@ class PlacePickerState extends State<PlacePicker> {
clearOverlay();

try {
final headers = await const GoogleApiHeaders().getHeaders();
final url = Uri.parse(
"https://maps.googleapis.com/maps/api/place/details/json?key=${widget.apiKey}&" +
"language=${widget.localizationItem!.languageCode}&" +
"placeid=$placeId");

final response = await http.get(url);
final response = await http.get(url, headers: headers);

if (response.statusCode != 200) {
throw Error();
Expand Down Expand Up @@ -397,7 +400,7 @@ class PlacePickerState extends State<PlacePicker> {
),
);

Overlay.of(context)?.insert(this.overlayEntry!);
Overlay.of(context).insert(this.overlayEntry!);
}

/// Utility function to get clean readable name of a location. First checks
Expand Down Expand Up @@ -434,12 +437,13 @@ class PlacePickerState extends State<PlacePicker> {
/// Fetches and updates the nearby places to the provided lat,lng
void getNearbyPlaces(LatLng latLng) async {
try {
final headers = await const GoogleApiHeaders().getHeaders();
final url = Uri.parse(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
"key=${widget.apiKey}&location=${latLng.latitude},${latLng.longitude}"
"&radius=150&language=${widget.localizationItem!.languageCode}");

final response = await http.get(url);
final response = await http.get(url, headers: headers);

if (response.statusCode != 200) {
throw Error();
Expand Down Expand Up @@ -477,12 +481,13 @@ class PlacePickerState extends State<PlacePicker> {
/// to be the road name and the locality.
void reverseGeocodeLatLng(LatLng latLng) async {
try {
final headers = await const GoogleApiHeaders().getHeaders();
final url = Uri.parse("https://maps.googleapis.com/maps/api/geocode/json?"
"latlng=${latLng.latitude},${latLng.longitude}&"
"language=${widget.localizationItem!.languageCode}&"
"key=${widget.apiKey}");

final response = await http.get(url);
final response = await http.get(url, headers: headers);

if (response.statusCode != 200) {
throw Error();
Expand Down Expand Up @@ -514,9 +519,6 @@ class PlacePickerState extends State<PlacePicker> {
var tmp = result['address_components'][i];
var types = tmp["types"] as List<dynamic>;
var shortName = tmp['short_name'];
if (types == null) {
continue;
}
if (i == 0) {
// [street_number]
name = shortName;
Expand Down Expand Up @@ -639,13 +641,16 @@ class PlacePickerState extends State<PlacePicker> {
'Location permissions are permanently denied, we cannot request permissions.');
}
try {
final locationData =
await Geolocator.getCurrentPosition(timeLimit: Duration(seconds: 30));
final locationData = await Geolocator.getCurrentPosition(
locationSettings: LocationSettings(
timeLimit: Duration(seconds: 30),
),
);
LatLng target = LatLng(locationData.latitude, locationData.longitude);
//moveToLocation(target);
print('target:$target');
return target;
} on TimeoutException catch (e) {
} on TimeoutException catch (_) {
final locationData = await Geolocator.getLastKnownPosition();
if (locationData != null) {
return LatLng(locationData.latitude, locationData.longitude);
Expand Down Expand Up @@ -712,8 +717,8 @@ class PlacePickerState extends State<PlacePicker> {
Future<bool> _delayedPop() async {
Navigator.of(context, rootNavigator: true).push(
PageRouteBuilder(
pageBuilder: (_, __, ___) => WillPopScope(
onWillPop: () async => false,
pageBuilder: (_, __, ___) => PopScope(
canPop: false,
child: Scaffold(
backgroundColor: Colors.transparent,
body: Center(
Expand Down
15 changes: 10 additions & 5 deletions lib/widgets/rich_suggestion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,23 @@ class RichSuggestion extends StatelessWidget {
final List<TextSpan> result = [];
final style = TextStyle(color: Colors.grey, fontSize: 15);

final startText = autoCompleteItem.text?.substring(0, autoCompleteItem.offset);
final startText =
autoCompleteItem.text?.substring(0, autoCompleteItem.offset);
if (startText?.isNotEmpty == true) {
result.add(TextSpan(text: startText, style: style));
}

final boldText =
autoCompleteItem.text?.substring(autoCompleteItem.offset!, autoCompleteItem.offset! + autoCompleteItem.length!);
final boldText = autoCompleteItem.text?.substring(autoCompleteItem.offset!,
autoCompleteItem.offset! + autoCompleteItem.length!);
result.add(
TextSpan(text: boldText, style: style.copyWith(color: Theme.of(context).textTheme.bodyText1?.color)),
TextSpan(
text: boldText,
style: style.copyWith(
color: Theme.of(context).textTheme.bodyMedium?.color)),
);

final remainingText = autoCompleteItem.text?.substring(autoCompleteItem.offset! + autoCompleteItem.length!);
final remainingText = autoCompleteItem.text
?.substring(autoCompleteItem.offset! + autoCompleteItem.length!);
result.add(TextSpan(text: remainingText, style: style));

return result;
Expand Down
11 changes: 8 additions & 3 deletions lib/widgets/search_input.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:place_picker/entities/localization_item.dart';

/// Custom Search input field, showing the search and clear icons.
class SearchInput extends StatefulWidget {
final ValueChanged<String> onSearchInput;
final LocalizationItem localizationItem;

SearchInput(this.onSearchInput);
SearchInput(this.onSearchInput, this.localizationItem);

@override
State<StatefulWidget> createState() => SearchInputState();
Expand Down Expand Up @@ -57,11 +59,14 @@ class SearchInputState extends State<SearchInput> {
padding: EdgeInsets.symmetric(horizontal: 8),
child: Row(
children: <Widget>[
Icon(Icons.search, color: Theme.of(context).textTheme.bodyText1?.color),
Icon(Icons.search,
color: Theme.of(context).textTheme.bodyMedium?.color),
SizedBox(width: 8),
Expanded(
child: TextField(
decoration: InputDecoration(hintText: "Search place", border: InputBorder.none),
decoration: InputDecoration(
hintText: widget.localizationItem.searchHint,
border: InputBorder.none),
controller: this.editController,
onChanged: (value) {
setState(() {
Expand Down
Loading