Skip to content

Commit

Permalink
add sp
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxu317317 committed Feb 1, 2019
1 parent f55a285 commit 54c884c
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 10 deletions.
4 changes: 4 additions & 0 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
333E5DAE7FC10AC69FEC26C0 /* libPods-Runner.a in Frameworks */ = {isa = PBXBuildFile; fileRef = DDA792F029EDD7A11295D192 /* libPods-Runner.a */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
2D5378261FAA1A9400D5DBA9 /* flutter_assets in Resources */ = {isa = PBXBuildFile; fileRef = 2D5378251FAA1A9400D5DBA9 /* flutter_assets */; };
3B80C3941E831B6300D905FE /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; };
3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
9705A1C61CF904A100538489 /* Flutter.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9740EEBA1CF902C7004384FC /* Flutter.framework */; };
Expand Down Expand Up @@ -42,6 +43,7 @@
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
2D5378251FAA1A9400D5DBA9 /* flutter_assets */ = {isa = PBXFileReference; lastKnownFileType = folder; name = flutter_assets; path = Flutter/flutter_assets; sourceTree = SOURCE_ROOT; };
3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
7AFFD8ED1D35381100E5BB4D /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -92,6 +94,7 @@
children = (
3B80C3931E831B6300D905FE /* App.framework */,
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */,
2D5378251FAA1A9400D5DBA9 /* flutter_assets */,
9740EEBA1CF902C7004384FC /* Flutter.framework */,
9740EEB21CF90195004384FC /* Debug.xcconfig */,
7AFA3C8E1D35360C0083082E /* Release.xcconfig */,
Expand Down Expand Up @@ -209,6 +212,7 @@
files = (
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */,
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */,
2D5378261FAA1A9400D5DBA9 /* flutter_assets in Resources */,
084A20882202E4FD00428FF5 /* flutter go.png in Resources */,
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */,
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */,
Expand Down
5 changes: 3 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import 'package:flutter/rendering.dart';
import 'routers/routers.dart';
import 'routers/application.dart';
import 'package:flutter_go/utils/provider.dart';

import 'package:flutter_go/utils/shared_preferences.dart';
import 'views/welcome_page/index.dart';

const int ThemeColor = 0xFFC91B3A;

class MyApp extends StatelessWidget {
MyApp() {
MyApp() {
final router = new Router();
Routes.configureRoutes(router);
Application.router = router;
Expand Down Expand Up @@ -46,6 +46,7 @@ var db;
void main() async {
final provider = new Provider();
await provider.init(true);
Application.sharePeferences = await SpUtil.instance;
db = Provider.db;
runApp(new MyApp());
}
23 changes: 15 additions & 8 deletions lib/resources/shared_preferences_keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@
* tartget: xxx
*/

import 'package:flutter/material.dart';

class Demo extends StatefulWidget {
_Demo createState() => _Demo();
enum DateType {
Int,
Double,
Bool,
String,
Dynamic
}

class _Demo extends State<Demo> {
class spKey {
String name;
DateType type;

spKey({this.name, this.type});
}

Widget build(BuildContext context) {
return Container();
}
class sharedPreferencesKeys {
static spKey loginWelcome = spKey(name: 'loginWelcone', type: DateType.Bool);
}

3 changes: 3 additions & 0 deletions lib/routers/application.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_go/utils/shared_preferences.dart';
class Application {
static Router router;
static TabController controller;
static SpUtil sharePeferences;

static Map<String, String> github = {
'widgetsURL':'https://github.com/alibaba-paimai-frontend/flutter-common-widgets-app/tree/develop/lib/widgets/',
//'develop':'https://github.com/alibaba-paimai-frontend/flutter-common-widgets-app/tree/develop/lib/widgets/',
Expand Down
111 changes: 111 additions & 0 deletions lib/utils/shared_preferences.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@

import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';

///
/// 用来做shared_preferences的存储
class SpUtil {
static SpUtil _instance;
static Future<SpUtil> get instance async {
return await _getInstance();
}

static SharedPreferences _spf;


SpUtil._();

Future _init() async {
_spf = await SharedPreferences.getInstance();
}

static Future<SpUtil> _getInstance() async {
if (_instance == null) {
_instance = new SpUtil._();
await _instance._init();

}
return _instance;
}

static bool _beforCheck() {
String a;
if (_spf == null) {
return throw new StateError("SharedPreferences must be initialized first, but the insurance is null;");
}
}

static Set<String> getKeys() {
if (_beforCheck()) return null;
return _spf.getKeys();
}
dynamic get(String key) {
if (_beforCheck()) return null;
return _spf.getKeys();
}
static String getString(String key) {
if (_beforCheck()) return null;
return _spf.getString(key);
}

static Future<bool> putString(String key, String value) {
if (_beforCheck()) return null;
return _spf.setString(key, value);
}

static bool getBool(String key) {
if (_beforCheck()) return null;
return _spf.getBool(key);
}

static Future<bool> putBool(String key, bool value) {
if (_beforCheck()) return null;
return _spf.setBool(key, value);
}

static int getInt(String key) {
if (_beforCheck()) return null;
return _spf.getInt(key);
}

static Future<bool> putInt(String key, int value) {
if (_beforCheck()) return null;
return _spf.setInt(key, value);
}

static double getDouble(String key) {
if (_beforCheck()) return null;
return _spf.getDouble(key);
}

static Future<bool> putDouble(String key, double value) {
if (_beforCheck()) return null;
return _spf.setDouble(key, value);
}

static List<String> getStringList(String key) {
return _spf.getStringList(key);
}

static Future<bool> putStringList(String key, List<String> value) {
if (_beforCheck()) return null;
return _spf.setStringList(key, value);
}

static dynamic getDynamic(String key) {
if (_beforCheck()) return null;
return _spf.get(key);
}



static Future<bool> remove(String key) {
if (_beforCheck()) return null;
return _spf.remove(key);
}

static Future<bool> clear() {
if (_beforCheck()) return null;
return _spf.clear();
}
}

0 comments on commit 54c884c

Please sign in to comment.