google_places_suggestions 1.0.0
google_places_suggestions: ^1.0.0 copied to clipboard
GooglePlacesSuggestions is a Flutter package that delivers real-time location suggestions using the Google Places API.
library;
import 'package:flutter/material.dart';
import 'package:google_places_suggestions/google_places_suggestions.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
debugShowCheckedModeBanner: false,
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Place Search'),
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: ListView(
children: [
// Implementation of GooglePlacesSuggestions widget
GooglePlacesSuggestions(
// Required parameters
googleMapKey: 'YOUR_GOOGLE_MAPS_API_KEY',
onPlaceSelected: (String place) {
// Handle selected place
print('Selected place: $place');
},
// Optional customization
hint: 'Search for a location...',
accentColor: Theme.of(context).primaryColor,
debounceTime: const Duration(milliseconds: 300),
// Error handling
onError: (String error) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(error)),
);
},
// Custom empty state
noResultsWidget: const Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.search_off, size: 48, color: Colors.grey),
SizedBox(height: 8),
Text(
'No locations found',
style: TextStyle(
fontSize: 16,
color: Colors.grey,
),
),
],
),
),
),
],
),
),
),
);
}
}