Last active
October 11, 2022 14:14
-
-
Save diegoveloper/c53b4a3158f2ed710a089a830ed2e4b5 to your computer and use it in GitHub Desktop.
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 'package:flutter/material.dart'; | |
import 'package:flutter_planets_tutorial/ui/weather/Res.dart'; | |
import 'package:http/http.dart' as http; | |
import 'dart:convert'; | |
import 'dart:async'; | |
class HourlyForecast extends StatefulWidget { | |
@override | |
_HourlyForecastState createState() => _HourlyForecastState(); | |
} | |
class _HourlyForecastState extends State<HourlyForecast> { | |
@override | |
Widget build(BuildContext context) { | |
return new Container( | |
color: Colors.white, | |
child: FutureBuilder<List>( | |
future: Network().fetchHourly(), | |
builder: (BuildContext context, AsyncSnapshot<List> snap) { | |
return snap.hasData | |
? ListView.builder( | |
itemCount: snap.data.length, | |
itemBuilder: (BuildContext context, int i) { | |
return new HourlyItem(snap.data[i].icon, | |
snap.data[i].time.toString(), snap.data[i].temp); | |
}, | |
) | |
: Center( | |
child: CircularProgressIndicator(), | |
); | |
}, | |
)); | |
} | |
} | |
class HourlyItem extends StatelessWidget { | |
final String icon; | |
final String time; | |
final String temp; | |
HourlyItem(this.icon, this.time, this.temp); | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
children: <Widget>[ | |
Text(time, | |
style: new TextStyle( | |
color: Colors.blue, | |
fontFamily: 'Poppins', | |
fontWeight: FontWeight.w600, | |
fontSize: 13, | |
)), | |
Image.network( | |
icon, | |
color: Colors.white, | |
), | |
Text('$temp °C', | |
style: new TextStyle( | |
height: 1.1, | |
color: Colors.blue, | |
fontFamily: 'Poppins', | |
fontWeight: FontWeight.w600, | |
fontSize: 13, | |
)), | |
], | |
); | |
} | |
} | |
class Hourly { | |
int time; | |
String icon; | |
String temp; | |
Hourly({this.time, this.icon, this.temp}); | |
} | |
class Network { | |
String url = | |
"****"; | |
List<Hourly> hourly = List(); | |
Future<List<Hourly>> fetchHourly() async { | |
final res = await http.get(url, headers: {"Accept": "aplication/json"}); | |
final jsonData = json.decode(res.body); | |
final dataHourly = jsonData['hourly']; | |
final data = dataHourly['data'] as List; | |
for (var h in data) { | |
Hourly hour = new Hourly( | |
time: h['time'], | |
icon: h['icon'].toString(), | |
temp: h['temperature'].toString()); | |
hourly.add(hour); | |
} | |
print("Status code: ${res.statusCode}"); | |
if (res.statusCode == 200) { | |
return hourly; | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment