From 6d0c12ea4fcaf80a976fe265a6b67794d0302615 Mon Sep 17 00:00:00 2001 From: Nishant Srivastava Date: Tue, 7 Aug 2018 01:11:10 +0200 Subject: [PATCH] added: implemented shared_preferences plugin in persist_key_value example --- persist_key_value/.idea/workspace.xml | 167 +++++++++++++++++++++----- persist_key_value/lib/main.dart | 110 ++++++++++++++--- 2 files changed, 232 insertions(+), 45 deletions(-) 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 @@ - - - - + - + + + @@ -91,6 +87,8 @@ @@ -100,7 +98,9 @@ \ 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')), + ], ), ), - ))); + ), + ); + } }