From a486be5ea83597df67af8c85cfadec3c15e56211 Mon Sep 17 00:00:00 2001 From: Anager <fridrich1900@gmail.com> Date: Sun, 3 Apr 2022 08:51:28 +0200 Subject: [PATCH] added shared preferences --- .../settings/settings.dart | 95 ++++++++++--------- pubspec.lock | 2 +- pubspec.yaml | 1 + 3 files changed, 53 insertions(+), 45 deletions(-) diff --git a/lib/bottom_navigation_bar_buttons/settings/settings.dart b/lib/bottom_navigation_bar_buttons/settings/settings.dart index 8b3eb8e..0df9d4d 100644 --- a/lib/bottom_navigation_bar_buttons/settings/settings.dart +++ b/lib/bottom_navigation_bar_buttons/settings/settings.dart @@ -1,10 +1,12 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; class SettingScreen extends StatelessWidget { const SettingScreen({Key? key}) : super(key: key); + @override Widget build(BuildContext context) { return Scaffold( @@ -40,12 +42,15 @@ class _DropdownSettingButtonState extends State<DropdownSettingButton> { bool _expanded = false; String currentCity = "Deggendorf"; - void setCurrentCity(bool isDeg){ - if (isDeg){ - currentCity = "Deggendorf"; - }else{ - currentCity = "Plattling"; - } + getCurrentCityFromSharedPreferences() async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + String val = prefs.getString('currentCity') ?? "Not Selected"; + currentCity = val; + } + + setCurrentCityInSharedPreferences(String newCity) async{ + SharedPreferences prefs = await SharedPreferences.getInstance(); + prefs.setString('currentCity', newCity); } @@ -55,44 +60,46 @@ class _DropdownSettingButtonState extends State<DropdownSettingButton> { animationDuration: Duration(microseconds: 2000), children:[ ExpansionPanel( - canTapOnHeader: true, - isExpanded: _expanded, - headerBuilder: (context, isExpanded){ - return ListTile( - title: Text("Aktuelle Stadt: ${currentCity}", - style: TextStyle(fontSize: 22, color: Color.fromRGBO( - 1, 1, 1, 0.49411764705882355)) - ), - ); - }, - body: Column( - children: <Widget> [ - TextButton( - onPressed: (){ - setCurrentCity(true); - _expanded = false; - setState(() { - }); - }, - child: Text( - "Deggendorf", style: TextStyle(fontSize: 20, color: Color.fromRGBO( - 1, 1, 1, 0.49411764705882355)), - ) - ), - TextButton( - onPressed: (){ - setCurrentCity(false); - _expanded = false; - setState(() { - }); - }, - child: Text( - "Plattling", style: TextStyle(fontSize: 20, color: Color.fromRGBO( - 1, 1, 1, 0.49411764705882355)), - ) - ), - ], - ) + canTapOnHeader: true, + isExpanded: _expanded, + headerBuilder: (context, isExpanded){ + return ListTile( + title: Text(currentCity, + style: TextStyle(fontSize: 22, color: Color.fromRGBO( + 1, 1, 1, 0.49411764705882355)) + ), + ); + }, + body: Column( + children: <Widget> [ + TextButton( + onPressed: (){ + setCurrentCityInSharedPreferences("Deggendorf"); + _expanded = false; + getCurrentCityFromSharedPreferences(); + setState(() { + }); + }, + child: Text( + "Deggendorf", style: TextStyle(fontSize: 20, color: Color.fromRGBO( + 1, 1, 1, 0.49411764705882355)), + ) + ), + TextButton( + onPressed: (){ + setCurrentCityInSharedPreferences("Plattling"); + _expanded = false; + getCurrentCityFromSharedPreferences(); + setState(() { + }); + }, + child: Text( + "Plattling", style: TextStyle(fontSize: 20, color: Color.fromRGBO( + 1, 1, 1, 0.49411764705882355)), + ) + ), + ], + ) ) ], expansionCallback: (panelIndex, isExpanded){ diff --git a/pubspec.lock b/pubspec.lock index 2e93033..4c10e63 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -269,7 +269,7 @@ packages: source: hosted version: "5.0.0" shared_preferences: - dependency: transitive + dependency: "direct main" description: name: shared_preferences url: "https://pub.dartlang.org" diff --git a/pubspec.yaml b/pubspec.yaml index f7c4583..4d4effe 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -37,6 +37,7 @@ dependencies: cupertino_icons: ^1.0.4 http: ^0.13.4 flutter_settings_screens: ^0.2.2+1 + shared_preferences: ^2.0.13 dev_dependencies: flutter_test: -- GitLab