Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Load Data from the Internet
  • Loading branch information
dragosholban committed Jul 1, 2018
1 parent 24f5ac9 commit 9873cc0
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 55 deletions.
8 changes: 8 additions & 0 deletions .idea/libraries/Dart_Packages.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

146 changes: 112 additions & 34 deletions .idea/workspace.xml

Large diffs are not rendered by default.

69 changes: 61 additions & 8 deletions lib/main.dart
@@ -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(
Expand All @@ -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,
),
),
Expand All @@ -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(),
),
),
)
Expand All @@ -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;
});
}
}
25 changes: 25 additions & 0 deletions lib/models/ForecastData.dart
@@ -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,
);
}
}
19 changes: 19 additions & 0 deletions lib/models/WeatherData.dart
@@ -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'],
);
}
}
19 changes: 13 additions & 6 deletions lib/widgets/Weather.dart
@@ -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)),
],
);
}
Expand Down
19 changes: 13 additions & 6 deletions lib/widgets/WeatherItem.dart
@@ -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(
Expand All @@ -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)),
],
),
),
Expand Down
9 changes: 8 additions & 1 deletion pubspec.lock
Expand Up @@ -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"
Expand All @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions pubspec.yaml
Expand Up @@ -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
Expand Down

0 comments on commit 9873cc0

Please sign in to comment.