Slider in settings to adjust days of forecast loaded. Settings are saved and loaded

This commit is contained in:
Konstantin Kollar
2022-08-31 22:09:47 +02:00
parent b20e7cfb46
commit 4fac2ecf0a
8 changed files with 244 additions and 11 deletions
+25
View File
@@ -0,0 +1,25 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "wetter",
"request": "launch",
"type": "dart"
},
{
"name": "wetter (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
},
{
"name": "wetter (release mode)",
"request": "launch",
"type": "dart",
"flutterMode": "release"
}
]
}
+50
View File
@@ -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
View File
@@ -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(
+8 -5
View File
@@ -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()),
));
}
}
+2 -1
View File
@@ -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(
@@ -5,6 +5,8 @@
import FlutterMacOS
import Foundation
import shared_preferences_macos
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
}
+139
View File
@@ -57,6 +57,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
ffi:
dependency: transitive
description:
name: ffi
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
file:
dependency: transitive
description:
name: file
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.4"
flutter:
dependency: "direct main"
description: flutter
@@ -74,6 +88,11 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
http:
dependency: "direct main"
description:
@@ -95,6 +114,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.17.0"
js:
dependency: transitive
description:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.4"
lints:
dependency: transitive
description:
@@ -130,6 +156,104 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.7"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.4"
path_provider_windows:
dependency: transitive
description:
name: path_provider_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.2"
platform:
dependency: transitive
description:
name: platform
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.2"
process:
dependency: transitive
description:
name: process
url: "https://pub.dartlang.org"
source: hosted
version: "4.2.4"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.15"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.12"
shared_preferences_ios:
dependency: transitive
description:
name: shared_preferences_ios
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
shared_preferences_macos:
dependency: transitive
description:
name: shared_preferences_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.4"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.4"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
sky_engine:
dependency: transitive
description: flutter
@@ -198,5 +322,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
win32:
dependency: transitive
description:
name: win32
url: "https://pub.dartlang.org"
source: hosted
version: "2.7.0"
xdg_directories:
dependency: transitive
description:
name: xdg_directories
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0+2"
sdks:
dart: ">=2.17.6 <3.0.0"
flutter: ">=3.0.0"
+1
View File
@@ -37,6 +37,7 @@ dependencies:
http: ^0.13.5
weather_icons: ^3.0.0
intl: ^0.17.0
shared_preferences: ^2.0.15
dev_dependencies:
flutter_test: