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

translate selectText #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
130 changes: 84 additions & 46 deletions lib/src/google_map_place_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,29 @@ typedef PinBuilder = Widget Function(
);

class GoogleMapPlacePicker extends StatelessWidget {
const GoogleMapPlacePicker({
Key? key,
required this.initialTarget,
required this.appBarKey,
this.selectedPlaceWidgetBuilder,
this.pinBuilder,
this.onSearchFailed,
this.onMoveStart,
this.onMapCreated,
this.debounceMilliseconds,
this.enableMapTypeButton,
this.enableMyLocationButton,
this.onToggleMapType,
this.onMyLocation,
this.onPlacePicked,
this.usePinPointingSearch,
this.usePlaceDetailSearch,
this.selectInitialPosition,
this.language,
this.forceSearchOnZoomChanged,
this.hidePlaceDetailsWhenDraggingPin,
}) : super(key: key);
const GoogleMapPlacePicker(
{Key? key,
required this.initialTarget,
required this.appBarKey,
this.selectedPlaceWidgetBuilder,
this.pinBuilder,
this.onSearchFailed,
this.onMoveStart,
this.onMapCreated,
this.debounceMilliseconds,
this.enableMapTypeButton,
this.enableMyLocationButton,
this.onToggleMapType,
this.onMyLocation,
this.onPlacePicked,
this.usePinPointingSearch,
this.usePlaceDetailSearch,
this.selectInitialPosition,
this.language,
this.forceSearchOnZoomChanged,
this.hidePlaceDetailsWhenDraggingPin,
this.selectText = "Select here"})
: super(key: key);

final LatLng initialTarget;
final GlobalKey appBarKey;
Expand All @@ -75,13 +76,16 @@ class GoogleMapPlacePicker extends StatelessWidget {
final bool? selectInitialPosition;

final String? language;
final String selectText;

final bool? forceSearchOnZoomChanged;
final bool? hidePlaceDetailsWhenDraggingPin;

_searchByCameraLocation(PlaceProvider provider) async {
// We don't want to search location again if camera location is changed by zooming in/out.
bool hasZoomChanged = provider.cameraPosition != null && provider.prevCameraPosition != null && provider.cameraPosition!.zoom != provider.prevCameraPosition!.zoom;
bool hasZoomChanged = provider.cameraPosition != null &&
provider.prevCameraPosition != null &&
provider.cameraPosition!.zoom != provider.prevCameraPosition!.zoom;

if (forceSearchOnZoomChanged == false && hasZoomChanged) {
provider.placeSearchingState = SearchingState.Idle;
Expand All @@ -90,12 +94,16 @@ class GoogleMapPlacePicker extends StatelessWidget {

provider.placeSearchingState = SearchingState.Searching;

final GeocodingResponse response = await provider.geocoding.searchByLocation(
Location(lat: provider.cameraPosition!.target.latitude, lng: provider.cameraPosition!.target.longitude),
final GeocodingResponse response =
await provider.geocoding.searchByLocation(
Location(
lat: provider.cameraPosition!.target.latitude,
lng: provider.cameraPosition!.target.longitude),
language: language,
);

if (response.errorMessage?.isNotEmpty == true || response.status == "REQUEST_DENIED") {
if (response.errorMessage?.isNotEmpty == true ||
response.status == "REQUEST_DENIED") {
print("Camera Location Search Error: " + response.errorMessage!);
if (onSearchFailed != null) {
onSearchFailed!(response.status);
Expand All @@ -105,23 +113,28 @@ class GoogleMapPlacePicker extends StatelessWidget {
}

if (usePlaceDetailSearch!) {
final PlacesDetailsResponse detailResponse = await provider.places.getDetailsByPlaceId(
final PlacesDetailsResponse detailResponse =
await provider.places.getDetailsByPlaceId(
response.results[0].placeId,
language: language,
);

if (detailResponse.errorMessage?.isNotEmpty == true || detailResponse.status == "REQUEST_DENIED") {
print("Fetching details by placeId Error: " + detailResponse.errorMessage!);
if (detailResponse.errorMessage?.isNotEmpty == true ||
detailResponse.status == "REQUEST_DENIED") {
print("Fetching details by placeId Error: " +
detailResponse.errorMessage!);
if (onSearchFailed != null) {
onSearchFailed!(detailResponse.status);
}
provider.placeSearchingState = SearchingState.Idle;
return;
}

provider.selectedPlace = PickResult.fromPlaceDetailResult(detailResponse.result);
provider.selectedPlace =
PickResult.fromPlaceDetailResult(detailResponse.result);
} else {
provider.selectedPlace = PickResult.fromGeocodingResult(response.results[0]);
provider.selectedPlace =
PickResult.fromGeocodingResult(response.results[0]);
}

provider.placeSearchingState = SearchingState.Idle;
Expand All @@ -144,7 +157,8 @@ class GoogleMapPlacePicker extends StatelessWidget {
selector: (_, provider) => provider.mapType,
builder: (_, data, __) {
PlaceProvider provider = PlaceProvider.of(context, listen: false);
CameraPosition initialCameraPosition = CameraPosition(target: initialTarget, zoom: 15);
CameraPosition initialCameraPosition =
CameraPosition(target: initialTarget, zoom: 15);

return GoogleMap(
myLocationButtonEnabled: false,
Expand Down Expand Up @@ -179,7 +193,8 @@ class GoogleMapPlacePicker extends StatelessWidget {
if (provider.debounceTimer?.isActive ?? false) {
provider.debounceTimer!.cancel();
}
provider.debounceTimer = Timer(Duration(milliseconds: debounceMilliseconds!), () {
provider.debounceTimer =
Timer(Duration(milliseconds: debounceMilliseconds!), () {
_searchByCameraLocation(provider);
});
}
Expand Down Expand Up @@ -208,7 +223,9 @@ class GoogleMapPlacePicker extends StatelessWidget {
},
// gestureRecognizers make it possible to navigate the map when it's a
// child in a scroll view e.g ListView, SingleChildScrollView...
gestureRecognizers: Set()..add(Factory<EagerGestureRecognizer>(() => EagerGestureRecognizer())),
gestureRecognizers: Set()
..add(Factory<EagerGestureRecognizer>(
() => EagerGestureRecognizer())),
);
});
}
Expand All @@ -221,7 +238,9 @@ class GoogleMapPlacePicker extends StatelessWidget {
if (pinBuilder == null) {
return _defaultPinBuilder(context, state);
} else {
return Builder(builder: (builderContext) => pinBuilder!(builderContext, state));
return Builder(
builder: (builderContext) =>
pinBuilder!(builderContext, state));
}
},
),
Expand Down Expand Up @@ -262,7 +281,8 @@ class GoogleMapPlacePicker extends StatelessWidget {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AnimatedPin(child: Icon(Icons.place, size: 36, color: Colors.red)),
AnimatedPin(
child: Icon(Icons.place, size: 36, color: Colors.red)),
SizedBox(height: 42),
],
),
Expand All @@ -283,23 +303,34 @@ class GoogleMapPlacePicker extends StatelessWidget {
}

Widget _buildFloatingCard() {
return Selector<PlaceProvider, Tuple4<PickResult?, SearchingState, bool, PinState>>(
selector: (_, provider) => Tuple4(provider.selectedPlace, provider.placeSearchingState, provider.isSearchBarFocused, provider.pinState),
return Selector<PlaceProvider,
Tuple4<PickResult?, SearchingState, bool, PinState>>(
selector: (_, provider) => Tuple4(
provider.selectedPlace,
provider.placeSearchingState,
provider.isSearchBarFocused,
provider.pinState),
builder: (context, data, __) {
if ((data.item1 == null && data.item2 == SearchingState.Idle) || data.item3 == true || data.item4 == PinState.Dragging && this.hidePlaceDetailsWhenDraggingPin!) {
if ((data.item1 == null && data.item2 == SearchingState.Idle) ||
data.item3 == true ||
data.item4 == PinState.Dragging &&
this.hidePlaceDetailsWhenDraggingPin!) {
return Container();
} else {
if (selectedPlaceWidgetBuilder == null) {
return _defaultPlaceWidgetBuilder(context, data.item1, data.item2);
} else {
return Builder(builder: (builderContext) => selectedPlaceWidgetBuilder!(builderContext, data.item1, data.item2, data.item3));
return Builder(
builder: (builderContext) => selectedPlaceWidgetBuilder!(
builderContext, data.item1, data.item2, data.item3));
}
}
},
);
}

Widget _defaultPlaceWidgetBuilder(BuildContext context, PickResult? data, SearchingState state) {
Widget _defaultPlaceWidgetBuilder(
BuildContext context, PickResult? data, SearchingState state) {
return FloatingCard(
bottomPosition: MediaQuery.of(context).size.height * 0.05,
leftPosition: MediaQuery.of(context).size.width * 0.025,
Expand All @@ -308,7 +339,9 @@ class GoogleMapPlacePicker extends StatelessWidget {
borderRadius: BorderRadius.circular(12.0),
elevation: 4.0,
color: Theme.of(context).cardColor,
child: state == SearchingState.Searching ? _buildLoadingIndicator() : _buildSelectionDetails(context, data!),
child: state == SearchingState.Searching
? _buildLoadingIndicator()
: _buildSelectionDetails(context, data!),
);
}

Expand Down Expand Up @@ -339,7 +372,7 @@ class GoogleMapPlacePicker extends StatelessWidget {
RaisedButton(
padding: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
child: Text(
"Select here",
selectText,
style: TextStyle(fontSize: 16),
),
shape: RoundedRectangleBorder(
Expand All @@ -355,7 +388,8 @@ class GoogleMapPlacePicker extends StatelessWidget {
}

Widget _buildMapIcons(BuildContext context) {
final RenderBox appBarRenderBox = appBarKey.currentContext!.findRenderObject() as RenderBox;
final RenderBox appBarRenderBox =
appBarKey.currentContext!.findRenderObject() as RenderBox;

return Positioned(
top: appBarRenderBox.size.height,
Expand All @@ -368,7 +402,9 @@ class GoogleMapPlacePicker extends StatelessWidget {
height: 35,
child: RawMaterialButton(
shape: CircleBorder(),
fillColor: Theme.of(context).brightness == Brightness.dark ? Colors.black54 : Colors.white,
fillColor: Theme.of(context).brightness == Brightness.dark
? Colors.black54
: Colors.white,
elevation: 8.0,
onPressed: onToggleMapType,
child: Icon(Icons.layers),
Expand All @@ -382,7 +418,9 @@ class GoogleMapPlacePicker extends StatelessWidget {
height: 35,
child: RawMaterialButton(
shape: CircleBorder(),
fillColor: Theme.of(context).brightness == Brightness.dark ? Colors.black54 : Colors.white,
fillColor: Theme.of(context).brightness == Brightness.dark
? Colors.black54
: Colors.white,
elevation: 8.0,
onPressed: onMyLocation,
child: Icon(Icons.my_location),
Expand Down