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: [
|
||||
Text(name, style: Theme.of(context).textTheme.headline5),
|
||||
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),
|
||||
title: const Text("Locations"),
|
||||
onTap: (() {
|
||||
Navigator.push(context,
|
||||
MaterialPageRoute(builder: (context) => const Locations()));
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => const Locations()),
|
||||
);
|
||||
}),
|
||||
)
|
||||
],
|
||||
|
||||
+43
-32
@@ -17,41 +17,52 @@ class MyApp extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
List<String> _locations = [];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Wetter',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
textTheme: const TextTheme(
|
||||
headline1: TextStyle(fontSize: 72.0, fontWeight: FontWeight.bold),
|
||||
headline6: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic),
|
||||
bodyText2: TextStyle(fontSize: 14.0, fontFamily: 'Hind'),
|
||||
),
|
||||
),
|
||||
home: DefaultTabController(
|
||||
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)
|
||||
],
|
||||
),
|
||||
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
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Wetter',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
textTheme: const TextTheme(
|
||||
headline1: TextStyle(fontSize: 72.0, fontWeight: FontWeight.bold),
|
||||
headline6: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic),
|
||||
bodyText2: TextStyle(fontSize: 14.0, fontFamily: 'Hind'),
|
||||
),
|
||||
),
|
||||
home: buildContent());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,11 @@ class _LocationsState extends State<Locations> {
|
||||
});
|
||||
}
|
||||
|
||||
void deleteLocation(String name) {}
|
||||
void deleteLocation(String name) {
|
||||
setState(() {
|
||||
locations = UserPreferences.deleteLocation(name);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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 '../models/daily_weather_model.dart';
|
||||
@@ -17,8 +17,7 @@ class MainWeatherView extends StatefulWidget {
|
||||
|
||||
class _MainWeatherViewState extends State<MainWeatherView>
|
||||
with AutomaticKeepAliveClientMixin<MainWeatherView> {
|
||||
SharedPreferences? prefs;
|
||||
int _daysToLoad = 7;
|
||||
int _daysToLoad = UserPreferences.preferences!.getInt("daysToLoad") ?? 7;
|
||||
late Future<List<DailyWeatherModel>> dailyWeather;
|
||||
|
||||
@override
|
||||
@@ -27,18 +26,10 @@ class _MainWeatherViewState extends State<MainWeatherView>
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
initializePreferences();
|
||||
dailyWeather = BrightSkyAPI.fetchForecastForTimeRange(
|
||||
DateTime.now(), _daysToLoad, widget.lat, widget.lon);
|
||||
}
|
||||
|
||||
Future<void> initializePreferences() async {
|
||||
prefs = await SharedPreferences.getInstance();
|
||||
setState(() {
|
||||
_daysToLoad = prefs!.getInt("daysToLoad") ?? 7;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
super.build(context);
|
||||
@@ -46,7 +37,8 @@ class _MainWeatherViewState extends State<MainWeatherView>
|
||||
onRefresh: () {
|
||||
return Future(() {
|
||||
setState(() {
|
||||
_daysToLoad = prefs?.getInt("daysToLoad") ?? 7;
|
||||
_daysToLoad =
|
||||
UserPreferences.preferences!.getInt("daysToLoad") ?? 7;
|
||||
dailyWeather = BrightSkyAPI.fetchForecastForTimeRange(
|
||||
DateTime.now(), _daysToLoad, widget.lat, widget.lon);
|
||||
});
|
||||
|
||||
@@ -19,4 +19,20 @@ class UserPreferences {
|
||||
preferences!.setDouble("$name-lon", lon);
|
||||
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