flutter_place_picker 0.0.1-dev.3 flutter_place_picker: ^0.0.1-dev.3 copied to clipboard
A Flutter plugin which provides 'Picking Places' using OpenStreet, Here Maps and Google Maps.
import 'package:flutter/material.dart';
import 'package:flutter_place_picker/flutter_place_picker.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
// Your api key storage.
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// Light Theme
final ThemeData lightTheme = ThemeData.light().copyWith(
// Background color of the FloatingCard
cardColor: Colors.white,
buttonTheme: const ButtonThemeData(
// Select here's button color
buttonColor: Colors.black,
textTheme: ButtonTextTheme.primary,
),
);
// Dark Theme
final ThemeData darkTheme = ThemeData.dark().copyWith(
// Background color of the FloatingCard
cardColor: Colors.grey,
buttonTheme: const ButtonThemeData(
// Select here's button color
buttonColor: Colors.yellow,
textTheme: ButtonTextTheme.primary,
),
);
MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Place Picker Demo',
theme: lightTheme,
darkTheme: darkTheme,
themeMode: ThemeMode.light,
home: const HomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
PickResult? selectedPlace;
final LatLng kInitialPosition = const LatLng(-33.8567844, 151.213108);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Google Map Place Picker Demo"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
ElevatedButton(
child: const Text("Load Google Map"),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return FlutterPlacePicker(
apiKey:
"YOUR KEY HERE", // Needed to display google maps and
initialPosition: kInitialPosition,
useCurrentLocation: true,
selectInitialPosition: true,
usePlaceDetailSearch: false,
region: 'gh',
onPlacePicked: (result) {
selectedPlace = result;
print(result.geometry!.location.lat);
print(result.geometry!.location.lng);
print(result.placeId);
print(result.formattedAddress);
Navigator.of(context).pop();
setState(() {});
},
// serverUrl: "https://www.your-server.com/api/",
//automaticallyImplyAppBarLeading: false,
//autocompleteLanguage: "ko",
// selectedPlaceWidgetBuilder: (_, selectedPlace, state, isSearchBarFocused) {
// print("state: $state, isSearchBarFocused: $isSearchBarFocused");
// return isSearchBarFocused
// ? Container()
// : FloatingCard(
// bottomPosition: 0.0, // MediaQuery.of(context) will cause rebuild. See MediaQuery document for the information.
// leftPosition: 0.0,
// rightPosition: 0.0,
// width: 500,
// borderRadius: BorderRadius.circular(12.0),
// child: state == SearchingState.Searching
// ? Center(child: CircularProgressIndicator())
// : RaisedButton(
// child: Text("Pick Here"),
// onPressed: () {
// // IMPORTANT: You MUST manage selectedPlace data yourself as using this build will not invoke onPlacePicker as
// // this will override default 'Select here' Button.
// print("do something with [selectedPlace] data");
// Navigator.of(context).pop();
// },
// ),
// );
// },
// pinBuilder: (context, state) {
// if (state == PinState.Idle) {
// return Icon(Icons.favorite_border);
// } else {
// return Icon(Icons.favorite);
// }
// },
);
},
),
);
},
),
selectedPlace == null
? Container()
: Text(selectedPlace!.formattedAddress ?? ""),
],
),
));
}
}