Skip to content
Snippets Groups Projects
Commit a486be5e authored by Anager's avatar Anager
Browse files

added shared preferences

parent dea9d537
No related branches found
No related tags found
1 merge request!2Home page chaitanya
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class SettingScreen extends StatelessWidget { class SettingScreen extends StatelessWidget {
const SettingScreen({Key? key}) : super(key: key); const SettingScreen({Key? key}) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
...@@ -40,12 +42,15 @@ class _DropdownSettingButtonState extends State<DropdownSettingButton> { ...@@ -40,12 +42,15 @@ class _DropdownSettingButtonState extends State<DropdownSettingButton> {
bool _expanded = false; bool _expanded = false;
String currentCity = "Deggendorf"; String currentCity = "Deggendorf";
void setCurrentCity(bool isDeg){ getCurrentCityFromSharedPreferences() async {
if (isDeg){ SharedPreferences prefs = await SharedPreferences.getInstance();
currentCity = "Deggendorf"; String val = prefs.getString('currentCity') ?? "Not Selected";
}else{ currentCity = val;
currentCity = "Plattling"; }
}
setCurrentCityInSharedPreferences(String newCity) async{
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('currentCity', newCity);
} }
...@@ -55,44 +60,46 @@ class _DropdownSettingButtonState extends State<DropdownSettingButton> { ...@@ -55,44 +60,46 @@ class _DropdownSettingButtonState extends State<DropdownSettingButton> {
animationDuration: Duration(microseconds: 2000), animationDuration: Duration(microseconds: 2000),
children:[ children:[
ExpansionPanel( ExpansionPanel(
canTapOnHeader: true, canTapOnHeader: true,
isExpanded: _expanded, isExpanded: _expanded,
headerBuilder: (context, isExpanded){ headerBuilder: (context, isExpanded){
return ListTile( return ListTile(
title: Text("Aktuelle Stadt: ${currentCity}", title: Text(currentCity,
style: TextStyle(fontSize: 22, color: Color.fromRGBO( style: TextStyle(fontSize: 22, color: Color.fromRGBO(
1, 1, 1, 0.49411764705882355)) 1, 1, 1, 0.49411764705882355))
), ),
); );
}, },
body: Column( body: Column(
children: <Widget> [ children: <Widget> [
TextButton( TextButton(
onPressed: (){ onPressed: (){
setCurrentCity(true); setCurrentCityInSharedPreferences("Deggendorf");
_expanded = false; _expanded = false;
setState(() { getCurrentCityFromSharedPreferences();
}); setState(() {
}, });
child: Text( },
"Deggendorf", style: TextStyle(fontSize: 20, color: Color.fromRGBO( child: Text(
1, 1, 1, 0.49411764705882355)), "Deggendorf", style: TextStyle(fontSize: 20, color: Color.fromRGBO(
) 1, 1, 1, 0.49411764705882355)),
), )
TextButton( ),
onPressed: (){ TextButton(
setCurrentCity(false); onPressed: (){
_expanded = false; setCurrentCityInSharedPreferences("Plattling");
setState(() { _expanded = false;
}); getCurrentCityFromSharedPreferences();
}, setState(() {
child: Text( });
"Plattling", style: TextStyle(fontSize: 20, color: Color.fromRGBO( },
1, 1, 1, 0.49411764705882355)), child: Text(
) "Plattling", style: TextStyle(fontSize: 20, color: Color.fromRGBO(
), 1, 1, 1, 0.49411764705882355)),
], )
) ),
],
)
) )
], ],
expansionCallback: (panelIndex, isExpanded){ expansionCallback: (panelIndex, isExpanded){
......
...@@ -269,7 +269,7 @@ packages: ...@@ -269,7 +269,7 @@ packages:
source: hosted source: hosted
version: "5.0.0" version: "5.0.0"
shared_preferences: shared_preferences:
dependency: transitive dependency: "direct main"
description: description:
name: shared_preferences name: shared_preferences
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
......
...@@ -37,6 +37,7 @@ dependencies: ...@@ -37,6 +37,7 @@ dependencies:
cupertino_icons: ^1.0.4 cupertino_icons: ^1.0.4
http: ^0.13.4 http: ^0.13.4
flutter_settings_screens: ^0.2.2+1 flutter_settings_screens: ^0.2.2+1
shared_preferences: ^2.0.13
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment