Skip to content

Commit 9873cc0

Browse files
committed
Load Data from the Internet
1 parent 24f5ac9 commit 9873cc0

File tree

9 files changed

+263
-55
lines changed

9 files changed

+263
-55
lines changed

.idea/libraries/Dart_Packages.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 112 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/main.dart

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1+
import 'dart:convert';
12
import 'package:flutter/material.dart';
3+
import 'package:http/http.dart' as http;
24

35
import 'package:flutter_weather/widgets/Weather.dart';
46
import 'package:flutter_weather/widgets/WeatherItem.dart';
7+
import 'package:flutter_weather/models/WeatherData.dart';
8+
import 'package:flutter_weather/models/ForecastData.dart';
59

610
void main() => runApp(new MyApp());
711

8-
class MyApp extends StatelessWidget {
12+
class MyApp extends StatefulWidget {
13+
@override
14+
State<StatefulWidget> createState() {
15+
return new MyAppState();
16+
}
17+
}
18+
19+
class MyAppState extends State<MyApp> {
20+
bool isLoading = false;
21+
WeatherData weatherData;
22+
ForecastData forecastData;
23+
24+
@override
25+
void initState() {
26+
super.initState();
27+
28+
loadWeather();
29+
}
30+
931
@override
1032
Widget build(BuildContext context) {
1133
return MaterialApp(
@@ -28,14 +50,17 @@ class MyApp extends StatelessWidget {
2850
children: <Widget>[
2951
Padding(
3052
padding: const EdgeInsets.all(8.0),
31-
child: Weather(),
53+
child: weatherData != null ? Weather(weather: weatherData) : Container(),
3254
),
3355
Padding(
3456
padding: const EdgeInsets.all(8.0),
35-
child: IconButton(
57+
child: isLoading ? CircularProgressIndicator(
58+
strokeWidth: 2.0,
59+
valueColor: new AlwaysStoppedAnimation(Colors.white),
60+
) : IconButton(
3661
icon: new Icon(Icons.refresh),
3762
tooltip: 'Refresh',
38-
onPressed: () => null,
63+
onPressed: loadWeather,
3964
color: Colors.white,
4065
),
4166
),
@@ -47,11 +72,11 @@ class MyApp extends StatelessWidget {
4772
padding: const EdgeInsets.all(8.0),
4873
child: Container(
4974
height: 200.0,
50-
child: ListView.builder(
51-
itemCount: 10,
75+
child: forecastData != null ? ListView.builder(
76+
itemCount: forecastData.list.length,
5277
scrollDirection: Axis.horizontal,
53-
itemBuilder: (context, index) => WeatherItem()
54-
),
78+
itemBuilder: (context, index) => WeatherItem(weather: forecastData.list.elementAt(index))
79+
) : Container(),
5580
),
5681
),
5782
)
@@ -61,4 +86,32 @@ class MyApp extends StatelessWidget {
6186
),
6287
);
6388
}
89+
90+
loadWeather() async {
91+
setState(() {
92+
isLoading = true;
93+
});
94+
95+
final lat = 40.730610;
96+
final lon = -73.935242;
97+
final weatherResponse = await http.get(
98+
'https://api.openweathermap.org/data/2.5/weather?APPID=0721392c0ba0af8c410aa9394defa29e&lat=${lat
99+
.toString()}&lon=${lon.toString()}');
100+
final forecastResponse = await http.get(
101+
'https://api.openweathermap.org/data/2.5/forecast?APPID=0721392c0ba0af8c410aa9394defa29e&lat=${lat
102+
.toString()}&lon=${lon.toString()}');
103+
104+
if (weatherResponse.statusCode == 200 &&
105+
forecastResponse.statusCode == 200) {
106+
return setState(() {
107+
weatherData = new WeatherData.fromJson(jsonDecode(weatherResponse.body));
108+
forecastData = new ForecastData.fromJson(jsonDecode(forecastResponse.body));
109+
isLoading = false;
110+
});
111+
}
112+
113+
setState(() {
114+
isLoading = false;
115+
});
116+
}
64117
}

lib/models/ForecastData.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import 'package:flutter_weather/models/WeatherData.dart';
2+
3+
class ForecastData {
4+
final List list;
5+
6+
ForecastData({this.list});
7+
8+
factory ForecastData.fromJson(Map<String, dynamic> json) {
9+
List list = new List();
10+
11+
for (dynamic e in json['list']) {
12+
WeatherData w = new WeatherData(
13+
date: new DateTime.fromMillisecondsSinceEpoch(e['dt'] * 1000, isUtc: false),
14+
name: json['city']['name'],
15+
temp: e['main']['temp'].toDouble(),
16+
main: e['weather'][0]['main'],
17+
icon: e['weather'][0]['icon']);
18+
list.add(w);
19+
}
20+
21+
return ForecastData(
22+
list: list,
23+
);
24+
}
25+
}

lib/models/WeatherData.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class WeatherData {
2+
final DateTime date;
3+
final String name;
4+
final double temp;
5+
final String main;
6+
final String icon;
7+
8+
WeatherData({this.date, this.name, this.temp, this.main, this.icon});
9+
10+
factory WeatherData.fromJson(Map<String, dynamic> json) {
11+
return WeatherData(
12+
date: new DateTime.fromMillisecondsSinceEpoch(json['dt'] * 1000, isUtc: false),
13+
name: json['name'],
14+
temp: json['main']['temp'].toDouble(),
15+
main: json['weather'][0]['main'],
16+
icon: json['weather'][0]['icon'],
17+
);
18+
}
19+
}

lib/widgets/Weather.dart

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
import 'package:flutter/material.dart';
2+
import 'package:intl/intl.dart';
3+
4+
import 'package:flutter_weather/models/WeatherData.dart';
25

36
class Weather extends StatelessWidget {
7+
final WeatherData weather;
8+
9+
Weather({Key key, @required this.weather}) : super(key: key);
10+
411
@override
512
Widget build(BuildContext context) {
613
return Column(
714
children: <Widget>[
8-
Text('New York', style: new TextStyle(color: Colors.white)),
9-
Text('Rain', style: new TextStyle(color: Colors.white, fontSize: 32.0)),
10-
Text('72°F', style: new TextStyle(color: Colors.white)),
11-
Image.network('https://openweathermap.org/img/w/01d.png'),
12-
Text('Jun 28, 2018', style: new TextStyle(color: Colors.white)),
13-
Text('18:30', style: new TextStyle(color: Colors.white)),
15+
Text(weather.name, style: new TextStyle(color: Colors.white)),
16+
Text(weather.main, style: new TextStyle(color: Colors.white, fontSize: 32.0)),
17+
Text('${weather.temp.toString()}°F', style: new TextStyle(color: Colors.white)),
18+
Image.network('https://openweathermap.org/img/w/${weather.icon}.png'),
19+
Text(new DateFormat.yMMMd().format(weather.date), style: new TextStyle(color: Colors.white)),
20+
Text(new DateFormat.Hm().format(weather.date), style: new TextStyle(color: Colors.white)),
1421
],
1522
);
1623
}

lib/widgets/WeatherItem.dart

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import 'package:flutter/material.dart';
2+
import 'package:intl/intl.dart';
3+
4+
import 'package:flutter_weather/models/WeatherData.dart';
25

36
class WeatherItem extends StatelessWidget {
7+
final WeatherData weather;
8+
9+
WeatherItem({Key key, @required this.weather}) : super(key: key);
10+
411
@override
512
Widget build(BuildContext context) {
613
return Card(
@@ -9,12 +16,12 @@ class WeatherItem extends StatelessWidget {
916
child: Column(
1017
mainAxisAlignment: MainAxisAlignment.center,
1118
children: <Widget>[
12-
Text('New York', style: new TextStyle(color: Colors.black)),
13-
Text('Rain', style: new TextStyle(color: Colors.black, fontSize: 24.0)),
14-
Text('72°F', style: new TextStyle(color: Colors.black)),
15-
Image.network('https://openweathermap.org/img/w/01d.png'),
16-
Text('Jun 28, 2018', style: new TextStyle(color: Colors.black)),
17-
Text('18:30', style: new TextStyle(color: Colors.black)),
19+
Text(weather.name, style: new TextStyle(color: Colors.black)),
20+
Text(weather.main, style: new TextStyle(color: Colors.black, fontSize: 24.0)),
21+
Text('${weather.temp.toString()}°F', style: new TextStyle(color: Colors.black)),
22+
Image.network('https://openweathermap.org/img/w/${weather.icon}.png'),
23+
Text(new DateFormat.yMMMd().format(weather.date), style: new TextStyle(color: Colors.black)),
24+
Text(new DateFormat.Hm().format(weather.date), style: new TextStyle(color: Colors.black)),
1825
],
1926
),
2027
),

pubspec.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ packages:
103103
source: hosted
104104
version: "0.13.3"
105105
http:
106-
dependency: transitive
106+
dependency: "direct main"
107107
description:
108108
name: http
109109
url: "https://pub.dartlang.org"
@@ -123,6 +123,13 @@ packages:
123123
url: "https://pub.dartlang.org"
124124
source: hosted
125125
version: "3.1.2"
126+
intl:
127+
dependency: "direct main"
128+
description:
129+
name: intl
130+
url: "https://pub.dartlang.org"
131+
source: hosted
132+
version: "0.15.6"
126133
io:
127134
dependency: transitive
128135
description:

pubspec.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ dependencies:
99
# Use with the CupertinoIcons class for iOS style icons.
1010
cupertino_icons: ^0.1.2
1111

12+
http: "^0.11.3+16"
13+
14+
intl: "^0.15.6"
15+
1216
dev_dependencies:
1317
flutter_test:
1418
sdk: flutter

0 commit comments

Comments
 (0)