Permalink
Browse files

Load Data from the Internet

  • Loading branch information...
dragosholban committed Jul 1, 2018
1 parent 24f5ac9 commit 9873cc02d7aa3fac2eccf4aae6ae685be5c9b959
@@ -128,6 +128,13 @@
</list>
</value>
</entry>
<entry key="intl">
<value>
<list>
<option value="$PROJECT_DIR$/../flutter/.pub-cache/hosted/pub.dartlang.org/intl-0.15.6/lib" />
</list>
</value>
</entry>
<entry key="io">
<value>
<list>
@@ -392,6 +399,7 @@
<root url="file://$PROJECT_DIR$/../flutter/.pub-cache/hosted/pub.dartlang.org/http-0.11.3+16/lib" />
<root url="file://$PROJECT_DIR$/../flutter/.pub-cache/hosted/pub.dartlang.org/http_multi_server-2.0.4/lib" />
<root url="file://$PROJECT_DIR$/../flutter/.pub-cache/hosted/pub.dartlang.org/http_parser-3.1.2/lib" />
<root url="file://$PROJECT_DIR$/../flutter/.pub-cache/hosted/pub.dartlang.org/intl-0.15.6/lib" />
<root url="file://$PROJECT_DIR$/../flutter/.pub-cache/hosted/pub.dartlang.org/io-0.3.2+1/lib" />
<root url="file://$PROJECT_DIR$/../flutter/.pub-cache/hosted/pub.dartlang.org/js-0.6.1/lib" />
<root url="file://$PROJECT_DIR$/../flutter/.pub-cache/hosted/pub.dartlang.org/kernel-0.3.0-alpha.12/lib" />

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -1,11 +1,33 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_weather/widgets/Weather.dart';
import 'package:flutter_weather/widgets/WeatherItem.dart';
import 'package:flutter_weather/models/WeatherData.dart';
import 'package:flutter_weather/models/ForecastData.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new MyAppState();
}
}
class MyAppState extends State<MyApp> {
bool isLoading = false;
WeatherData weatherData;
ForecastData forecastData;
@override
void initState() {
super.initState();
loadWeather();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
@@ -28,14 +50,17 @@ class MyApp extends StatelessWidget {
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Weather(),
child: weatherData != null ? Weather(weather: weatherData) : Container(),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: IconButton(
child: isLoading ? CircularProgressIndicator(
strokeWidth: 2.0,
valueColor: new AlwaysStoppedAnimation(Colors.white),
) : IconButton(
icon: new Icon(Icons.refresh),
tooltip: 'Refresh',
onPressed: () => null,
onPressed: loadWeather,
color: Colors.white,
),
),
@@ -47,11 +72,11 @@ class MyApp extends StatelessWidget {
padding: const EdgeInsets.all(8.0),
child: Container(
height: 200.0,
child: ListView.builder(
itemCount: 10,
child: forecastData != null ? ListView.builder(
itemCount: forecastData.list.length,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) => WeatherItem()
),
itemBuilder: (context, index) => WeatherItem(weather: forecastData.list.elementAt(index))
) : Container(),
),
),
)
@@ -61,4 +86,32 @@ class MyApp extends StatelessWidget {
),
);
}
loadWeather() async {
setState(() {
isLoading = true;
});
final lat = 40.730610;
final lon = -73.935242;
final weatherResponse = await http.get(
'https://api.openweathermap.org/data/2.5/weather?APPID=0721392c0ba0af8c410aa9394defa29e&lat=${lat
.toString()}&lon=${lon.toString()}');
final forecastResponse = await http.get(
'https://api.openweathermap.org/data/2.5/forecast?APPID=0721392c0ba0af8c410aa9394defa29e&lat=${lat
.toString()}&lon=${lon.toString()}');
if (weatherResponse.statusCode == 200 &&
forecastResponse.statusCode == 200) {
return setState(() {
weatherData = new WeatherData.fromJson(jsonDecode(weatherResponse.body));
forecastData = new ForecastData.fromJson(jsonDecode(forecastResponse.body));
isLoading = false;
});
}
setState(() {
isLoading = false;
});
}
}
@@ -0,0 +1,25 @@
import 'package:flutter_weather/models/WeatherData.dart';
class ForecastData {
final List list;
ForecastData({this.list});
factory ForecastData.fromJson(Map<String, dynamic> json) {
List list = new List();
for (dynamic e in json['list']) {
WeatherData w = new WeatherData(
date: new DateTime.fromMillisecondsSinceEpoch(e['dt'] * 1000, isUtc: false),
name: json['city']['name'],
temp: e['main']['temp'].toDouble(),
main: e['weather'][0]['main'],
icon: e['weather'][0]['icon']);
list.add(w);
}
return ForecastData(
list: list,
);
}
}
@@ -0,0 +1,19 @@
class WeatherData {
final DateTime date;
final String name;
final double temp;
final String main;
final String icon;
WeatherData({this.date, this.name, this.temp, this.main, this.icon});
factory WeatherData.fromJson(Map<String, dynamic> json) {
return WeatherData(
date: new DateTime.fromMillisecondsSinceEpoch(json['dt'] * 1000, isUtc: false),
name: json['name'],
temp: json['main']['temp'].toDouble(),
main: json['weather'][0]['main'],
icon: json['weather'][0]['icon'],
);
}
}
@@ -1,16 +1,23 @@
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:flutter_weather/models/WeatherData.dart';
class Weather extends StatelessWidget {
final WeatherData weather;
Weather({Key key, @required this.weather}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Text('New York', style: new TextStyle(color: Colors.white)),
Text('Rain', style: new TextStyle(color: Colors.white, fontSize: 32.0)),
Text('72°F', style: new TextStyle(color: Colors.white)),
Image.network('https://openweathermap.org/img/w/01d.png'),
Text('Jun 28, 2018', style: new TextStyle(color: Colors.white)),
Text('18:30', style: new TextStyle(color: Colors.white)),
Text(weather.name, style: new TextStyle(color: Colors.white)),
Text(weather.main, style: new TextStyle(color: Colors.white, fontSize: 32.0)),
Text('${weather.temp.toString()}°F', style: new TextStyle(color: Colors.white)),
Image.network('https://openweathermap.org/img/w/${weather.icon}.png'),
Text(new DateFormat.yMMMd().format(weather.date), style: new TextStyle(color: Colors.white)),
Text(new DateFormat.Hm().format(weather.date), style: new TextStyle(color: Colors.white)),
],
);
}
@@ -1,6 +1,13 @@
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:flutter_weather/models/WeatherData.dart';
class WeatherItem extends StatelessWidget {
final WeatherData weather;
WeatherItem({Key key, @required this.weather}) : super(key: key);
@override
Widget build(BuildContext context) {
return Card(
@@ -9,12 +16,12 @@ class WeatherItem extends StatelessWidget {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('New York', style: new TextStyle(color: Colors.black)),
Text('Rain', style: new TextStyle(color: Colors.black, fontSize: 24.0)),
Text('72°F', style: new TextStyle(color: Colors.black)),
Image.network('https://openweathermap.org/img/w/01d.png'),
Text('Jun 28, 2018', style: new TextStyle(color: Colors.black)),
Text('18:30', style: new TextStyle(color: Colors.black)),
Text(weather.name, style: new TextStyle(color: Colors.black)),
Text(weather.main, style: new TextStyle(color: Colors.black, fontSize: 24.0)),
Text('${weather.temp.toString()}°F', style: new TextStyle(color: Colors.black)),
Image.network('https://openweathermap.org/img/w/${weather.icon}.png'),
Text(new DateFormat.yMMMd().format(weather.date), style: new TextStyle(color: Colors.black)),
Text(new DateFormat.Hm().format(weather.date), style: new TextStyle(color: Colors.black)),
],
),
),
@@ -103,7 +103,7 @@ packages:
source: hosted
version: "0.13.3"
http:
dependency: transitive
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
@@ -123,6 +123,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.2"
intl:
dependency: "direct main"
description:
name: intl
url: "https://pub.dartlang.org"
source: hosted
version: "0.15.6"
io:
dependency: transitive
description:
@@ -9,6 +9,10 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
http: "^0.11.3+16"
intl: "^0.15.6"
dev_dependencies:
flutter_test:
sdk: flutter

0 comments on commit 9873cc0

Please sign in to comment.