Created
June 1, 2020 07:07
-
-
Save yshean/1212f677b075aed766a73a271f512ae4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:google_places_flutter/address_search.dart'; | |
import 'package:google_places_flutter/place_service.dart'; | |
import 'package:uuid/uuid.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Google Places Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.amber, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: MyHomePage(title: 'Places Autocomplete Demo'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final _controller = TextEditingController(); | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Column( | |
children: <Widget>[ | |
TextField( | |
controller: _controller, | |
readOnly: true, | |
onTap: () async { | |
// generate a new token here | |
final sessionToken = Uuid().v4(); | |
final Suggestion result = await showSearch( | |
context: context, | |
delegate: AddressSearch(sessionToken), | |
); | |
// This will change the text displayed in the TextField | |
if (result != null) { | |
setState(() { | |
_controller.text = result.description; | |
}); | |
} | |
}, | |
decoration: InputDecoration( | |
icon: Container( | |
margin: EdgeInsets.only(left: 20), | |
width: 10, | |
height: 10, | |
child: Icon( | |
Icons.home, | |
color: Colors.black, | |
), | |
), | |
hintText: "Enter your shipping address", | |
border: InputBorder.none, | |
contentPadding: EdgeInsets.only(left: 8.0, top: 16.0), | |
), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment