Add locations and save them, remove locations and delete them
This commit is contained in:
@@ -15,7 +15,9 @@ class ManageSingleLocation extends StatelessWidget {
|
|||||||
child: Row(children: [
|
child: Row(children: [
|
||||||
Text(name, style: Theme.of(context).textTheme.headline5),
|
Text(name, style: Theme.of(context).textTheme.headline5),
|
||||||
const Spacer(),
|
const Spacer(),
|
||||||
const Icon(Icons.delete_sharp)
|
GestureDetector(
|
||||||
|
onTap: () => delete(name),
|
||||||
|
child: const Icon(Icons.delete_sharp))
|
||||||
])));
|
])));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,8 +28,10 @@ class NavBar extends StatelessWidget {
|
|||||||
leading: const Icon(Icons.public),
|
leading: const Icon(Icons.public),
|
||||||
title: const Text("Locations"),
|
title: const Text("Locations"),
|
||||||
onTap: (() {
|
onTap: (() {
|
||||||
Navigator.push(context,
|
Navigator.push(
|
||||||
MaterialPageRoute(builder: (context) => const Locations()));
|
context,
|
||||||
|
MaterialPageRoute(builder: (context) => const Locations()),
|
||||||
|
);
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
|
|||||||
+35
-24
@@ -17,6 +17,40 @@ class MyApp extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _MyAppState extends State<MyApp> {
|
class _MyAppState extends State<MyApp> {
|
||||||
|
List<String> _locations = [];
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
_locations = UserPreferences.getSavedLocations();
|
||||||
|
super.initState();
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget buildContent() {
|
||||||
|
return DefaultTabController(
|
||||||
|
length: _locations.length,
|
||||||
|
child: Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: const Text('Wetter'),
|
||||||
|
bottom: (_locations.isEmpty)
|
||||||
|
? null
|
||||||
|
: TabBar(
|
||||||
|
tabs: [for (String name in _locations) Tab(icon: Text(name))],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
drawer: const NavBar(),
|
||||||
|
body: (_locations.isEmpty)
|
||||||
|
? const Text("No locations")
|
||||||
|
: TabBarView(
|
||||||
|
children: [
|
||||||
|
for (String name in _locations)
|
||||||
|
MainWeatherView(UserPreferences.getLat(name),
|
||||||
|
UserPreferences.getLon(name))
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
@@ -29,29 +63,6 @@ class _MyAppState extends State<MyApp> {
|
|||||||
bodyText2: TextStyle(fontSize: 14.0, fontFamily: 'Hind'),
|
bodyText2: TextStyle(fontSize: 14.0, fontFamily: 'Hind'),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
home: DefaultTabController(
|
home: buildContent());
|
||||||
length: 3,
|
|
||||||
child: Scaffold(
|
|
||||||
appBar: AppBar(
|
|
||||||
title: const Text('Wetter'),
|
|
||||||
bottom: const TabBar(
|
|
||||||
tabs: [
|
|
||||||
Tab(icon: Text("Hamburg")),
|
|
||||||
Tab(icon: Text("Frankfurt")),
|
|
||||||
Tab(icon: Text("Freiburg")),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
drawer: const NavBar(),
|
|
||||||
body: const TabBarView(
|
|
||||||
children: [
|
|
||||||
MainWeatherView(53.46, 9.98),
|
|
||||||
MainWeatherView(50.15, 8.70),
|
|
||||||
MainWeatherView(47.99, 7.85)
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,11 @@ class _LocationsState extends State<Locations> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void deleteLocation(String name) {}
|
void deleteLocation(String name) {
|
||||||
|
setState(() {
|
||||||
|
locations = UserPreferences.deleteLocation(name);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:wetter/services/user_preferences.dart';
|
||||||
|
|
||||||
import '../components/compact_weather_data.dart';
|
import '../components/compact_weather_data.dart';
|
||||||
import '../models/daily_weather_model.dart';
|
import '../models/daily_weather_model.dart';
|
||||||
@@ -17,8 +17,7 @@ class MainWeatherView extends StatefulWidget {
|
|||||||
|
|
||||||
class _MainWeatherViewState extends State<MainWeatherView>
|
class _MainWeatherViewState extends State<MainWeatherView>
|
||||||
with AutomaticKeepAliveClientMixin<MainWeatherView> {
|
with AutomaticKeepAliveClientMixin<MainWeatherView> {
|
||||||
SharedPreferences? prefs;
|
int _daysToLoad = UserPreferences.preferences!.getInt("daysToLoad") ?? 7;
|
||||||
int _daysToLoad = 7;
|
|
||||||
late Future<List<DailyWeatherModel>> dailyWeather;
|
late Future<List<DailyWeatherModel>> dailyWeather;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -27,18 +26,10 @@ class _MainWeatherViewState extends State<MainWeatherView>
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
initializePreferences();
|
|
||||||
dailyWeather = BrightSkyAPI.fetchForecastForTimeRange(
|
dailyWeather = BrightSkyAPI.fetchForecastForTimeRange(
|
||||||
DateTime.now(), _daysToLoad, widget.lat, widget.lon);
|
DateTime.now(), _daysToLoad, widget.lat, widget.lon);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> initializePreferences() async {
|
|
||||||
prefs = await SharedPreferences.getInstance();
|
|
||||||
setState(() {
|
|
||||||
_daysToLoad = prefs!.getInt("daysToLoad") ?? 7;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
super.build(context);
|
super.build(context);
|
||||||
@@ -46,7 +37,8 @@ class _MainWeatherViewState extends State<MainWeatherView>
|
|||||||
onRefresh: () {
|
onRefresh: () {
|
||||||
return Future(() {
|
return Future(() {
|
||||||
setState(() {
|
setState(() {
|
||||||
_daysToLoad = prefs?.getInt("daysToLoad") ?? 7;
|
_daysToLoad =
|
||||||
|
UserPreferences.preferences!.getInt("daysToLoad") ?? 7;
|
||||||
dailyWeather = BrightSkyAPI.fetchForecastForTimeRange(
|
dailyWeather = BrightSkyAPI.fetchForecastForTimeRange(
|
||||||
DateTime.now(), _daysToLoad, widget.lat, widget.lon);
|
DateTime.now(), _daysToLoad, widget.lat, widget.lon);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -19,4 +19,20 @@ class UserPreferences {
|
|||||||
preferences!.setDouble("$name-lon", lon);
|
preferences!.setDouble("$name-lon", lon);
|
||||||
return getSavedLocations();
|
return getSavedLocations();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static List<String> deleteLocation(String name) {
|
||||||
|
preferences!.remove("$name-lat");
|
||||||
|
preferences!.remove("$name-lon");
|
||||||
|
_savedLocations.remove(name);
|
||||||
|
preferences!.remove(name);
|
||||||
|
return getSavedLocations();
|
||||||
|
}
|
||||||
|
|
||||||
|
static getLat(String name) {
|
||||||
|
return preferences!.getDouble("$name-lat");
|
||||||
|
}
|
||||||
|
|
||||||
|
static getLon(String name) {
|
||||||
|
return preferences!.getDouble("$name-lon");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user