Skip to content

Commit

Permalink
Add sp_store_util
Browse files Browse the repository at this point in the history
  • Loading branch information
chengww committed Mar 29, 2019
1 parent 6a24d66 commit c40401a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
17 changes: 3 additions & 14 deletions lib/pages/home_page.dart
@@ -1,8 +1,7 @@
import 'package:flutter/material.dart';
import 'package:one_article/bean/article_bean.dart';
import 'package:one_article/db/database.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:one_article/utils/sp_store_util.dart';

class HomePage extends StatefulWidget {
final ArticleBean article;
Expand All @@ -22,7 +21,7 @@ class _HomePageState extends State<HomePage> {

@override
Widget build(BuildContext context) {
_getFontSize().then((value) {
getFontSize().then((value) {
if (value != _fontSize) {
setState(() {
_fontSize = value;
Expand Down Expand Up @@ -106,7 +105,7 @@ class _HomePageState extends State<HomePage> {
Expanded(
child: Slider(
onChanged: (double value) {
_storeFontSize(value.roundToDouble());
storeFontSize(value.roundToDouble());
mSetState(() => _fontSize = value.roundToDouble());
setState(() {});
},
Expand Down Expand Up @@ -135,14 +134,4 @@ class _HomePageState extends State<HomePage> {
}


final String fontSizeKey = "fontSize";

_storeFontSize(double value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setDouble(fontSizeKey, value);
}

Future<double> _getFontSize() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getDouble(fontSizeKey) ?? 18;
}
13 changes: 13 additions & 0 deletions lib/utils/sp_store_util.dart
@@ -0,0 +1,13 @@
import 'package:shared_preferences/shared_preferences.dart';

final String fontSizeKey = "fontSize";

storeFontSize(double value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setDouble(fontSizeKey, value);
}

Future<double> getFontSize() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getDouble(fontSizeKey) ?? 18;
}

0 comments on commit c40401a

Please sign in to comment.