Slider in settings to adjust days of forecast loaded. Settings are saved and loaded
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class SettingsForm extends StatefulWidget {
|
||||
const SettingsForm({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
SettingsFormState createState() => SettingsFormState();
|
||||
}
|
||||
|
||||
class SettingsFormState extends State<SettingsForm> {
|
||||
SharedPreferences? prefs;
|
||||
int _daysToLoad = 7;
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
initializePreferences().whenComplete(() => setState(() {
|
||||
_daysToLoad = prefs?.getInt("daysToLoad") ?? 7;
|
||||
}));
|
||||
}
|
||||
|
||||
Future<void> initializePreferences() async {
|
||||
prefs = await SharedPreferences.getInstance();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
children: [
|
||||
Slider(
|
||||
value: _daysToLoad.toDouble(),
|
||||
min: 1.0,
|
||||
max: 14.0,
|
||||
divisions: 14,
|
||||
label: _daysToLoad.round().toString(),
|
||||
onChanged: (double value) {
|
||||
setState(() {
|
||||
_daysToLoad = value.round();
|
||||
prefs?.setInt("daysToLoad", _daysToLoad);
|
||||
});
|
||||
},
|
||||
)
|
||||
],
|
||||
));
|
||||
}
|
||||
}
|
||||
+17
-5
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:wetter/components/compact_weather_data.dart';
|
||||
import 'package:wetter/components/main_menu.dart';
|
||||
import 'package:wetter/models/daily_weather_model.dart';
|
||||
@@ -14,13 +15,23 @@ class MyApp extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
SharedPreferences? prefs;
|
||||
int _daysToLoad = 7;
|
||||
late Future<List<DailyWeatherModel>> dailyWeather;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
dailyWeather = BrightSkyAPI.fetchForecastForTimeRange(
|
||||
DateTime.now(), DateTime.now().add(const Duration(days: 7)));
|
||||
initializePreferences();
|
||||
dailyWeather =
|
||||
BrightSkyAPI.fetchForecastForTimeRange(DateTime.now(), _daysToLoad);
|
||||
}
|
||||
|
||||
Future<void> initializePreferences() async {
|
||||
prefs = await SharedPreferences.getInstance();
|
||||
setState(() {
|
||||
_daysToLoad = prefs!.getInt("daysToLoad") ?? 7;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -44,9 +55,9 @@ class _MyAppState extends State<MyApp> {
|
||||
onRefresh: () {
|
||||
return Future(() {
|
||||
setState(() {
|
||||
_daysToLoad = prefs?.getInt("daysToLoad") ?? 7;
|
||||
dailyWeather = BrightSkyAPI.fetchForecastForTimeRange(
|
||||
DateTime.now(),
|
||||
DateTime.now().add(const Duration(days: 7)));
|
||||
DateTime.now(), _daysToLoad);
|
||||
});
|
||||
});
|
||||
},
|
||||
@@ -54,7 +65,8 @@ class _MyAppState extends State<MyApp> {
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
child: Center(
|
||||
child: FutureBuilder<List<DailyWeatherModel>>(
|
||||
future: dailyWeather,
|
||||
future: BrightSkyAPI.fetchForecastForTimeRange(
|
||||
DateTime.now(), _daysToLoad),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return Column(
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wetter/components/settings_form.dart';
|
||||
|
||||
class Settings extends StatelessWidget {
|
||||
const Settings({super.key});
|
||||
@@ -6,10 +7,12 @@ class Settings extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Settings'),
|
||||
),
|
||||
body: const Center(child: Text("Settings here")),
|
||||
);
|
||||
appBar: AppBar(
|
||||
title: const Text('Settings'),
|
||||
),
|
||||
body: const Padding(
|
||||
padding: EdgeInsets.all(20),
|
||||
child: Center(child: SettingsForm()),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,8 @@ class BrightSkyAPI {
|
||||
}
|
||||
|
||||
static Future<List<DailyWeatherModel>> fetchForecastForTimeRange(
|
||||
DateTime from, DateTime until) async {
|
||||
DateTime from, int noOfDays) async {
|
||||
DateTime until = from.add(Duration(days: noOfDays));
|
||||
String fromDay = "${from.year}-${from.month}-${from.day}";
|
||||
String untilDay = "${until.year}-${until.month}-${until.day}";
|
||||
final response = await http.get(
|
||||
|
||||
Reference in New Issue
Block a user