Last active
April 5, 2018 05:21
-
-
Save veltall/999f48a7e9191bc7fae2c1f2e621f970 to your computer and use it in GitHub Desktop.
Simple local restaurant query app
This file contains 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 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import './places.dart'; | |
const lat = 37.7576792; | |
const long = -122.5078112; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
static List<Place> _favPlaces = <Place>[]; // ignore, it's for the next feature | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Polymer Demo', | |
theme: new ThemeData( | |
primarySwatch: Colors.red, | |
), | |
home: new Scaffold( | |
appBar: new AppBar( | |
title: new Text('Home'), | |
), | |
body: new HomeScreen())); | |
} | |
} | |
class HomeScreen extends StatefulWidget { | |
HomeScreen({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_HomeScreenState createState() => new _HomeScreenState(); | |
} | |
class _HomeScreenState extends State<HomeScreen> { | |
List<Place> _places = <Place>[]; | |
@override | |
initState() { | |
super.initState(); | |
listenForPlaces(); | |
} | |
void listenForPlaces() async { | |
var stream = await getPlaces(lat, long); | |
stream.listen((place) => | |
// _places.add(place); | |
setState(() => _places.add(place))); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new ListView( | |
children: _places.map((place) => new PlaceWidget(place)).toList(), | |
); | |
} | |
} | |
class PlaceWidget extends StatefulWidget { | |
@override | |
_PlaceWidgetState createState() { | |
return new _PlaceWidgetState(place, false); | |
} | |
final Place place; | |
PlaceWidget(this.place, {Key key}) : super(key: key); | |
} | |
class _PlaceWidgetState extends State<PlaceWidget> { | |
final Place _place; | |
bool favorited; | |
_PlaceWidgetState(this._place, this.favorited); | |
@override | |
Widget build(BuildContext context) { | |
return new ListTile( | |
key: new PageStorageKey(_PlaceWidgetState), | |
title: new Text(_place.name), | |
subtitle: | |
new Text(_place.address, style: Theme.of(context).textTheme.caption), | |
leading: new CircleAvatar( | |
child: new Text(_place.rating.toString()), | |
backgroundColor: | |
favorited ? Colors.green : Theme.of(context).primaryColor, | |
), | |
trailing: new GestureDetector( | |
onTap: () { | |
final snackBar = | |
new SnackBar(content: new Text("Tapped on " + _place.name)); | |
Scaffold.of(context).showSnackBar(snackBar); | |
setState(() { | |
favorited = !favorited; | |
}); | |
MyApp._favPlaces.add(_place); | |
print(MyApp._favPlaces); | |
}, | |
child: favorited | |
? new Icon(Icons.favorite, color: Colors.red) | |
: new Icon(Icons.favorite_border), | |
), | |
); | |
} | |
} | |
class SecondScreen extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: new AppBar(title: new Text('Supplementary Screen')), | |
body: new Center( | |
child: new RaisedButton( | |
child: new Text('go back to screen 1'), | |
onPressed: () { | |
Navigator.pop(context); | |
}, | |
))); | |
} | |
} |
This file contains 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
// Copyright 2018 The Chromium Authors. All rights reserved. | |
// Use of this source code is governed by a BSD-style license that can be | |
// found in the LICENSE file. | |
import 'dart:convert'; | |
import 'package:http/http.dart' as http; | |
import 'dart:async'; | |
import '../key.dart' show key; | |
main() { | |
getPlaces(37.7576792, -122.5078112); | |
} | |
class Place { | |
final String name; | |
final double rating; | |
final String address; | |
Place.fromJson(Map jsonMap) : | |
name = jsonMap['name'], | |
rating = jsonMap['rating']?.toDouble() ?? -1.0, | |
address = jsonMap['vicinity']; | |
String toString() => 'Place: $name'; | |
} | |
Future<Stream<Place>> getPlaces(double lat, double lng) async { | |
var url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json' + | |
'?location=$lat,$lng' + | |
'&radius=1000&type=restaurant' + | |
'&key=$key'; | |
// http.get(url).then( | |
// (res) => print(res.body) | |
// ); | |
var client = new http.Client(); | |
var streamedRes = await client.send(new http.Request('get', Uri.parse(url))); | |
return streamedRes.stream | |
.transform(UTF8.decoder) | |
.transform(JSON.decoder) | |
.expand((jsonBody) => (jsonBody as Map)['results'] ) | |
.map((jsonPlace) => new Place.fromJson(jsonPlace)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment