Skip to content

Commit

Permalink
sp的本地存储功能
Browse files Browse the repository at this point in the history
  • Loading branch information
yanddongdong committed Jan 18, 2019
1 parent cc5a552 commit 9b15b40
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/http/HttpPage.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ydd/main/MainPage.dart';
import 'package:flutter_ydd/module/news.dart';
import 'package:flutter_ydd/module/user_detail.dart';
import 'package:flutter_ydd/utils/HttpGo.dart';
import 'package:shared_preferences/shared_preferences.dart';

class HttpPage extends StatefulWidget {
@override
Expand All @@ -12,6 +14,22 @@ class HttpPage extends StatefulWidget {
class _HttpPageState extends State<HttpPage> {
var httpContent = '显示网络数据';

@override
void initState() {
// TODO: implement initState
getValueForMainPage();
super.initState();
}

getValueForMainPage() async {
//测试sp的存储
SharedPreferences prefs = await SharedPreferences.getInstance();
String a = await prefs.getString(MainPageState.STRING_KEY);
int b = await prefs.getInt(MainPageState.INT_KEY);
bool c = await prefs.getBool(MainPageState.BOOL_KEY);
double d = await prefs.getDouble(MainPageState.DOUBLE_KEY);
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down
18 changes: 18 additions & 0 deletions lib/main/MainPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter_ydd/http/HttpPage.dart';
import 'package:flutter_ydd/info/NewsPage.dart';
import 'package:flutter_ydd/personal/PersonalPage.dart';
import 'package:flutter_ydd/type/TypePage.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MainPageWidget extends StatefulWidget {
@override
Expand All @@ -19,6 +20,12 @@ class MainPageState extends State<MainPageWidget> {

var _pageList;

static const STRING_KEY = 'string';
static const BOOL_KEY = 'bool';
static const INT_KEY = 'int';
static const DOUBLE_KEY = 'double';
static const LIST_KEY = 'list';

Image getTabIcon(int curIndex) {
if (curIndex == _tabIndex) {
return tabImages[curIndex][1];
Expand All @@ -43,9 +50,20 @@ class MainPageState extends State<MainPageWidget> {
@override
void initState() {
initData(); //初始化数据
setSp();
super.initState();
}

setSp() async {
var list = List<String>.generate(5, (i) => 'item$i');
SharedPreferences sp = await SharedPreferences.getInstance();
sp.setString(STRING_KEY, '我是从MainPage存的数据');
sp.setBool(BOOL_KEY, true);
sp.setInt(INT_KEY, 1);
sp.setDouble(DOUBLE_KEY, 231.111);
sp.setStringList(LIST_KEY, list);
}

void initData() {
tabImages = [
[
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies:
fluttertoast: ^2.2.6
json_serializable: ^2.0.0 # 解析
build_runner: ^1.1.2 # 解析
shared_preferences: ^0.4.2

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 9b15b40

Please sign in to comment.