Skip to content

Commit

Permalink
Separate shared preference view
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeguzman committed May 16, 2019
1 parent 3aa42f5 commit b914c24
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 108 deletions.
115 changes: 7 additions & 108 deletions lib/screens/dashboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_persistence_demo/widgets/shared_preference_view.dart';

class DashboardScreen extends StatefulWidget {
@override
Expand All @@ -13,123 +13,22 @@ class DashboardScreen extends StatefulWidget {
}

class DashboardScreenState extends State<DashboardScreen> {
final String KEY_COUNTER = 'counter';
int _counter = 0;

@override
void initState() {
super.initState();

// Update view with the initial value
_retrievePersistedCounter();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Persistence Demo'),
),
body: Container(
padding: EdgeInsets.all(20),
padding: EdgeInsets.all(20),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Storing simple data (shared_preferences 0.5.2)',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
Padding(padding: EdgeInsets.only(bottom: 5)),
Text(
'Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android). Not recommended for storing critical data.',
style: TextStyle(
fontSize: 14,
),
),
Padding(padding: EdgeInsets.only(bottom: 10)),
ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
color: Colors.blue,
height: 175,
padding: EdgeInsets.all(10),
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
'Counter (int)',
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
),
Padding(padding: EdgeInsets.only(bottom: 5)),
Text(
'$_counter',
style: TextStyle(
fontSize: 30,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
Padding(padding: EdgeInsets.only(bottom: 10)),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('Increment'),
onPressed: _incrementCounter,
),
Padding(padding: EdgeInsets.all(10)),
RaisedButton(
child: Text('Decrement'),
onPressed: _decreaseCounter,
),
],
),
],
),
),
),
SharedPreferenceView(),
],
)),
),
),
),
);
}

_retrievePersistedCounter() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
int counter = (preferences.getInt(KEY_COUNTER) ?? 0) + 1;

setState(() {
_counter = counter;
});
}

_incrementCounter() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
int counter = (preferences.getInt(KEY_COUNTER) ?? 0) + 1;
await preferences.setInt(KEY_COUNTER, counter);

setState(() {
_counter = counter;
});
}

_decreaseCounter() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
int counter = (preferences.getInt(KEY_COUNTER) ?? 0) - 1;
await preferences.setInt(KEY_COUNTER, counter);

setState(() {
_counter = counter;
});
}
}
128 changes: 128 additions & 0 deletions lib/widgets/shared_preference_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright 2019 Joshua de Guzman (https://jmdg.io). All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SharedPreferenceView extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return SharedPreferenceViewState();
}
}

class SharedPreferenceViewState extends State<SharedPreferenceView> {
final String KEY_COUNTER = 'counter';
int _counter = 0;

@override
void initState() {
super.initState();

// Update view with the initial value
_retrievePersistedCounter();
}

@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Storing simple data (shared_preferences 0.5.2)',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
Padding(padding: EdgeInsets.only(bottom: 5)),
Text(
'Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android). Not recommended for storing critical data.',
style: TextStyle(
fontSize: 14,
),
),
Padding(padding: EdgeInsets.only(bottom: 10)),
ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
color: Colors.blue,
height: 175,
padding: EdgeInsets.all(10),
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
'Counter (int)',
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
),
Padding(padding: EdgeInsets.only(bottom: 5)),
Text(
'$_counter',
style: TextStyle(
fontSize: 30,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
Padding(padding: EdgeInsets.only(bottom: 10)),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('Increment'),
onPressed: _incrementCounter,
),
Padding(padding: EdgeInsets.all(10)),
RaisedButton(
child: Text('Decrement'),
onPressed: _decreaseCounter,
),
],
),
],
),
),
)
],
);
}

_retrievePersistedCounter() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
int counter = (preferences.getInt(KEY_COUNTER) ?? 0) + 1;

setState(() {
_counter = counter;
});
}

_incrementCounter() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
int counter = (preferences.getInt(KEY_COUNTER) ?? 0) + 1;
await preferences.setInt(KEY_COUNTER, counter);

setState(() {
_counter = counter;
});
}

_decreaseCounter() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
int counter = (preferences.getInt(KEY_COUNTER) ?? 0) - 1;
await preferences.setInt(KEY_COUNTER, counter);

setState(() {
_counter = counter;
});
}
}

0 comments on commit b914c24

Please sign in to comment.