1+ import 'dart:convert' ;
12import 'package:flutter/material.dart' ;
3+ import 'package:http/http.dart' as http;
24
35import 'package:flutter_weather/widgets/Weather.dart' ;
46import 'package:flutter_weather/widgets/WeatherItem.dart' ;
7+ import 'package:flutter_weather/models/WeatherData.dart' ;
8+ import 'package:flutter_weather/models/ForecastData.dart' ;
59
610void 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}
0 commit comments