diff --git a/persist_key_value/.idea/workspace.xml b/persist_key_value/.idea/workspace.xml
index da88491..db58c71 100644
--- a/persist_key_value/.idea/workspace.xml
+++ b/persist_key_value/.idea/workspace.xml
@@ -2,13 +2,9 @@
-
-
-
-
+
-
@@ -27,8 +23,8 @@
-
-
+
+
@@ -37,8 +33,8 @@
-
-
+
+
@@ -46,13 +42,15 @@
-
+
+
-
+
+
@@ -63,8 +61,8 @@
-
-
+
+
@@ -72,17 +70,15 @@
-
-
-
-
-
-
-
-
-
+
+
+
@@ -91,6 +87,8 @@
+
+
@@ -100,7 +98,9 @@
-
+
+
+
@@ -120,6 +120,17 @@
+
+
+
+
+
+
+
+
+
+
+
@@ -133,9 +144,12 @@
-
+
+
+
+
@@ -165,6 +179,7 @@
+
@@ -221,17 +236,42 @@
-
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
@@ -241,6 +281,65 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Android
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 1.8
+
+
+
+
+
+
+
+
+
+
+
+ persist_key_value_android
+
+
+
+
+
+
+
+
+
@@ -252,6 +351,18 @@
+
+
+ Dart Packages
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/persist_key_value/lib/main.dart b/persist_key_value/lib/main.dart
index 17c34f1..3b8eac5 100644
--- a/persist_key_value/lib/main.dart
+++ b/persist_key_value/lib/main.dart
@@ -1,25 +1,101 @@
import 'package:flutter/material.dart';
+import 'package:shared_preferences/shared_preferences.dart';
void main() {
+ runApp(new MaterialApp(
+ // Disable the debug flag
+ debugShowCheckedModeBanner: false,
+ // Home
+ home: new MyHome()));
+}
+
+class MyHome extends StatefulWidget {
+ @override
+ MyHomeState createState() {
+ return new MyHomeState();
+ }
+}
+
+class MyHomeState extends State {
var nameOfApp = "Persist Key Value";
- runApp(new MaterialApp(
- // Title
- title: nameOfApp,
- // Home
- home: new Scaffold(
- // Appbar
- appBar: new AppBar(
- // Title
- title: new Text(nameOfApp),
- ),
- // Body
- body: new Container(
- // Center the content
- child: new Center(
- // Add Text
- child: new Text("Hello World!"),
+ var counter = 0;
+
+ // define a key to use later
+ var key = "counter";
+
+ @override
+ void initState() {
+ super.initState();
+ _loadSavedData();
+ }
+
+ _loadSavedData() async {
+ // Get shared preference instance
+ SharedPreferences prefs = await SharedPreferences.getInstance();
+ setState(() {
+ // Get value
+ counter = (prefs.getInt(key) ?? 0);
+ });
+ }
+
+ _onIncrementHit() async {
+ // Get shared preference instance
+ SharedPreferences prefs = await SharedPreferences.getInstance();
+
+ setState(() {
+ // Get value
+ counter = (prefs.getInt(key) ?? 0) + 1;
+ });
+
+ // Save Value
+ prefs.setInt(key, counter);
+ }
+
+ _onDecrementHit() async {
+ // Get shared preference instance
+ SharedPreferences prefs = await SharedPreferences.getInstance();
+
+ setState(() {
+ // Get value
+ counter = (prefs.getInt(key) ?? 0) - 1;
+ });
+
+ // Save Value
+ prefs.setInt(key, counter);
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return new Scaffold(
+ // Appbar
+ appBar: new AppBar(
+ // Title
+ title: new Text(nameOfApp),
+ ),
+ // Body
+ body: new Container(
+ // Center the content
+ child: new Center(
+ child: new Column(
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: [
+ new Text(
+ '$counter',
+ textScaleFactor: 10.0,
+ ),
+ new Padding(padding: new EdgeInsets.all(10.0)),
+ new RaisedButton(
+ onPressed: _onIncrementHit,
+ child: new Text('Increment Counter')),
+ new Padding(padding: new EdgeInsets.all(10.0)),
+ new RaisedButton(
+ onPressed: _onDecrementHit,
+ child: new Text('Decrement Counter')),
+ ],
),
),
- )));
+ ),
+ );
+ }
}