Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

edit pick button text and color, hide searchbar if not usabale #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
19 changes: 13 additions & 6 deletions lib/src/google_map_place_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class GoogleMapPlacePicker extends StatelessWidget {
Key? key,
required this.initialTarget,
required this.appBarKey,
this.selectHereTitle = "Select here",
this.backgroundButtonColor,
this.selectedPlaceWidgetBuilder,
this.pinBuilder,
this.onSearchFailed,
Expand All @@ -54,6 +56,8 @@ class GoogleMapPlacePicker extends StatelessWidget {

final LatLng initialTarget;
final GlobalKey appBarKey;
final String selectHereTitle;
final Color? backgroundButtonColor;

final SelectedPlaceWidgetBuilder? selectedPlaceWidgetBuilder;
final PinBuilder? pinBuilder;
Expand Down Expand Up @@ -335,15 +339,18 @@ class GoogleMapPlacePicker extends StatelessWidget {
textAlign: TextAlign.center,
),
SizedBox(height: 10),
RaisedButton(
padding: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: backgroundButtonColor,
padding: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4.0),
),
),
child: Text(
"Select here",
selectHereTitle,
style: TextStyle(fontSize: 16),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4.0),
),
onPressed: () {
onPlacePicked!(result);
},
Expand Down
91 changes: 56 additions & 35 deletions lib/src/place_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ import 'package:provider/provider.dart';
import 'dart:io' show Platform;

enum PinState { Preparing, Idle, Dragging }

enum SearchingState { Idle, Searching }

class PlacePicker extends StatefulWidget {
PlacePicker({
Key? key,
required this.apiKey,
this.selectHereTitle = "Select here",
this.backgroundButtonColor,
this.withoutSearchBar = false,
this.onPlacePicked,
required this.initialPosition,
this.useCurrentLocation,
Expand Down Expand Up @@ -157,6 +161,10 @@ class PlacePicker extends StatefulWidget {
/// Whether to display appbar backbutton. Defaults to true.
final bool automaticallyImplyAppBarLeading;

final String selectHereTitle;
final Color? backgroundButtonColor;
final bool withoutSearchBar;

/// Will perform an autocomplete search, if set to true. Note that setting
/// this to true, while providing a smoother UX experience, may cause
/// additional unnecessary queries to the Places API.
Expand Down Expand Up @@ -275,39 +283,43 @@ class _PlacePickerState extends State<PlacePicker> {
children: <Widget>[
widget.automaticallyImplyAppBarLeading
? IconButton(
onPressed: () => Navigator.maybePop(context),
onPressed: () => widget.withoutSearchBar
? Navigator.pop(context)
: Navigator.maybePop(context),
icon: Icon(
Platform.isIOS ? Icons.arrow_back_ios : Icons.arrow_back,
),
padding: EdgeInsets.zero)
: SizedBox(width: 15),
Expanded(
child: AutoCompleteSearch(
appBarKey: appBarKey,
searchBarController: searchBarController,
sessionToken: provider!.sessionToken,
hintText: widget.hintText,
searchingText: widget.searchingText,
debounceMilliseconds: widget.autoCompleteDebounceInMilliseconds,
onPicked: (prediction) {
_pickPrediction(prediction);
},
onSearchFailed: (status) {
if (widget.onAutoCompleteFailed != null) {
widget.onAutoCompleteFailed!(status);
}
},
autocompleteOffset: widget.autocompleteOffset,
autocompleteRadius: widget.autocompleteRadius,
autocompleteLanguage: widget.autocompleteLanguage,
autocompleteComponents: widget.autocompleteComponents,
autocompleteTypes: widget.autocompleteTypes,
strictbounds: widget.strictbounds,
region: widget.region,
initialSearchString: widget.initialSearchString,
searchForInitialValue: widget.searchForInitialValue,
autocompleteOnTrailingWhitespace: widget.autocompleteOnTrailingWhitespace),
),
if (!widget.withoutSearchBar)
Expanded(
child: AutoCompleteSearch(
appBarKey: appBarKey,
searchBarController: searchBarController,
sessionToken: provider!.sessionToken,
hintText: widget.hintText,
searchingText: widget.searchingText,
debounceMilliseconds: widget.autoCompleteDebounceInMilliseconds,
onPicked: (prediction) {
_pickPrediction(prediction);
},
onSearchFailed: (status) {
if (widget.onAutoCompleteFailed != null) {
widget.onAutoCompleteFailed!(status);
}
},
autocompleteOffset: widget.autocompleteOffset,
autocompleteRadius: widget.autocompleteRadius,
autocompleteLanguage: widget.autocompleteLanguage,
autocompleteComponents: widget.autocompleteComponents,
autocompleteTypes: widget.autocompleteTypes,
strictbounds: widget.strictbounds,
region: widget.region,
initialSearchString: widget.initialSearchString,
searchForInitialValue: widget.searchForInitialValue,
autocompleteOnTrailingWhitespace:
widget.autocompleteOnTrailingWhitespace),
),
SizedBox(width: 5),
],
);
Expand All @@ -316,13 +328,15 @@ class _PlacePickerState extends State<PlacePicker> {
_pickPrediction(Prediction prediction) async {
provider!.placeSearchingState = SearchingState.Searching;

final PlacesDetailsResponse response = await provider!.places.getDetailsByPlaceId(
final PlacesDetailsResponse response =
await provider!.places.getDetailsByPlaceId(
prediction.placeId!,
sessionToken: provider!.sessionToken,
language: widget.autocompleteLanguage,
);

if (response.errorMessage?.isNotEmpty == true || response.status == "REQUEST_DENIED") {
if (response.errorMessage?.isNotEmpty == true ||
response.status == "REQUEST_DENIED") {
if (widget.onAutoCompleteFailed != null) {
widget.onAutoCompleteFailed!(response.status);
}
Expand All @@ -334,7 +348,8 @@ class _PlacePickerState extends State<PlacePicker> {
// Prevents searching again by camera movement.
provider!.isAutoCompleteSearching = true;

await _moveTo(provider!.selectedPlace!.geometry!.location.lat, provider!.selectedPlace!.geometry!.location.lng);
await _moveTo(provider!.selectedPlace!.geometry!.location.lat,
provider!.selectedPlace!.geometry!.location.lng);

provider!.placeSearchingState = SearchingState.Idle;
}
Expand All @@ -355,22 +370,25 @@ class _PlacePickerState extends State<PlacePicker> {

_moveToCurrentPosition() async {
if (provider!.currentPosition != null) {
await _moveTo(provider!.currentPosition!.latitude, provider!.currentPosition!.longitude);
await _moveTo(provider!.currentPosition!.latitude,
provider!.currentPosition!.longitude);
}
}

Widget _buildMapWithLocation() {
if (widget.useCurrentLocation != null && widget.useCurrentLocation!) {
return FutureBuilder(
future: provider!.updateCurrentLocation(widget.forceAndroidLocationManager),
future: provider!
.updateCurrentLocation(widget.forceAndroidLocationManager),
builder: (context, snap) {
if (snap.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else {
if (provider!.currentPosition == null) {
return _buildMap(widget.initialPosition);
} else {
return _buildMap(LatLng(provider!.currentPosition!.latitude, provider!.currentPosition!.longitude));
return _buildMap(LatLng(provider!.currentPosition!.latitude,
provider!.currentPosition!.longitude));
}
}
});
Expand Down Expand Up @@ -405,6 +423,8 @@ class _PlacePickerState extends State<PlacePicker> {
language: widget.autocompleteLanguage,
forceSearchOnZoomChanged: widget.forceSearchOnZoomChanged,
hidePlaceDetailsWhenDraggingPin: widget.hidePlaceDetailsWhenDraggingPin,
selectHereTitle: widget.selectHereTitle,
backgroundButtonColor: widget.backgroundButtonColor,
onToggleMapType: () {
provider!.switchMapType();
},
Expand All @@ -415,7 +435,8 @@ class _PlacePickerState extends State<PlacePicker> {
Timer(Duration(seconds: widget.myLocationButtonCooldown), () {
provider!.isOnUpdateLocationCooldown = false;
});
await provider!.updateCurrentLocation(widget.forceAndroidLocationManager);
await provider!
.updateCurrentLocation(widget.forceAndroidLocationManager);
await _moveToCurrentPosition();
}
},
Expand Down