Add a refresh button! Yeehaw!
This commit is contained in:
parent
d8cc22ce80
commit
0f475773de
@ -1,5 +1,6 @@
|
||||
// Local
|
||||
import 'package:com_hyperling_buddy_website/secrets.dart';
|
||||
import '/var/secrets.dart';
|
||||
import '/var/locals.dart';
|
||||
|
||||
// Flutter / Dart
|
||||
import 'package:flutter/material.dart';
|
||||
@ -26,29 +27,46 @@ class _MainAppState extends State<MainApp> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_callOpenWeather();
|
||||
_refreshWeather();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
weather = loadText;
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget weatherLayout = Column(
|
||||
children: [Text(weather), CircularProgressIndicator()],
|
||||
);
|
||||
|
||||
if (weather == loadText) {
|
||||
Future.delayed(Duration(seconds: 1), () {
|
||||
setState(() {
|
||||
keepLoading = true;
|
||||
});
|
||||
});
|
||||
} else {
|
||||
weatherLayout = Column(
|
||||
children: [
|
||||
TextButton.icon(
|
||||
label: Text("Reload Weather"),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_refreshWeather();
|
||||
});
|
||||
},
|
||||
),
|
||||
Text(weather),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Center(
|
||||
child: Column(children: [Text('Hello World!'), Text(weather)]),
|
||||
child: Column(children: [Text('Weather Today!'), weatherLayout]),
|
||||
),
|
||||
),
|
||||
);
|
||||
@ -59,23 +77,23 @@ class _MainAppState extends State<MainApp> {
|
||||
Future<String> _hitOpenWeather(String zipCode, String? countryCode) async {
|
||||
countryCode ??= "US";
|
||||
|
||||
http.Response response = await http.get(
|
||||
Uri.parse(
|
||||
String url =
|
||||
'https://api.openweathermap.org/data/2.5/forecast'
|
||||
'?zip=$zipCode,$countryCode'
|
||||
'&units=imperial'
|
||||
'&appid=$openWeatherKey',
|
||||
),
|
||||
);
|
||||
'&appid=$openWeatherKey';
|
||||
|
||||
if (debug) debugPrint(url);
|
||||
http.Response response = await http.get(Uri.parse(url));
|
||||
|
||||
String data = "";
|
||||
if (response.statusCode == 200) {
|
||||
data = response.body;
|
||||
var decodedData = jsonDecode(data);
|
||||
print(decodedData);
|
||||
if (debug) debugPrint(decodedData.toString());
|
||||
data = decodedData.toString();
|
||||
} else {
|
||||
print(response.statusCode);
|
||||
debugPrint(response.statusCode.toString());
|
||||
}
|
||||
|
||||
return data;
|
||||
@ -84,4 +102,9 @@ class _MainAppState extends State<MainApp> {
|
||||
_callOpenWeather() async {
|
||||
weather = await _hitOpenWeather("47630", "US");
|
||||
}
|
||||
|
||||
_refreshWeather() {
|
||||
weather = loadText;
|
||||
_callOpenWeather();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user