thl преди 6 години
родител
ревизия
3335c63716
променени са 8 файла, в които са добавени 205 реда и са изтрити 39 реда
  1. 4 0
      CHANGELOG.md
  2. 77 15
      README.md
  3. 6 0
      doc/UPDATELOG.md
  4. 54 1
      example/lib/main.dart
  5. 7 18
      lib/src/screen_util.dart
  6. 54 1
      lib/src/sp_util.dart
  7. 1 1
      lib/src/widget_util.dart
  8. 2 3
      pubspec.yaml

+ 4 - 0
CHANGELOG.md

@@ -1,3 +1,7 @@
+## 0.2.3
+
+* TODO: synchronized : any , shared_preferences : any , SpUtil add putObject,getObject,putObjectList,getObjectList。
+
 ## 0.2.2
 
 * TODO: SpUtil add get default value.

+ 77 - 15
README.md

@@ -3,11 +3,6 @@
 
 ## [flustars] Flutter常用工具类库。主要对第三方库封装,以便于使用。如果你有好的工具类欢迎PR. 
 
-## [更新说明](./doc/UPDATELOG.md)
-v0.2.2 SpUtil新增get默认值。  
-SpUtil.getString('key', defValue: '');    
-SpUtil.getInt('key', defValue: 0);
- 
 ### 关于使用本开源库
 如果您是用于公司项目,请随意使用~  
 如果您是用于开源项目,未经本人许可,请勿copy源码到您的项目使用!  
@@ -15,8 +10,68 @@ SpUtil.getInt('key', defValue: 0);
 ### 使用方式:
 ```yaml
 dependencies:
-  flustars: ^0.2.2
+  flustars: ^0.2.3
+```
+
+## [更新说明](./doc/UPDATELOG.md)
+v0.2.3 (2019.02.26)  
+shared_preferences & synchronized 修改为动态依赖~  
+SpUtil 新增putObject,getObject,putObjectList,getObjectList。
 ```
+class City {
+  String name;
+
+  City({this.name});
+
+  City.fromJson(Map<String, dynamic> json) : name = json['name'];
+
+  Map<String, dynamic> toJson() => {
+        'name': name,
+      };
+
+  @override
+  String toString() {
+    StringBuffer sb = new StringBuffer('{');
+    sb.write("\"name\":\"$name\"");
+    sb.write('}');
+    return sb.toString();
+  }
+}
+
+void _initAsync() async {
+    await SpUtil.getInstance();
+
+    /// save object example.
+    /// 存储实体对象示例。
+    City city = new City();
+    city.name = "成都市";
+    SpUtil.putObject("loc_city", city);
+
+    Map dataStr = SpUtil.getObject("loc_city");
+    City hisCity = dataStr == null ? null : City.fromJson(dataStr);
+    print("thll Str: " + (hisCity == null ? "null" : hisCity.toString()));
+
+    /// save object list example.
+    /// 存储实体对象List示例。
+    List<City> list = new List();
+    list.add(new City(name: "成都市"));
+    list.add(new City(name: "北京市"));
+    SpUtil.putObjectList("loc_city_list", null);
+
+    List<Map> dataList = SpUtil.getObjectList("loc_city_list");
+    List<City> _cityList = dataList?.map((value) {
+      return City.fromJson(value);
+    })?.toList();
+
+    print("thll List: " + (_cityList == null ? "null" : _cityList.toString()));
+}
+    
+```
+
+v0.2.2 SpUtil新增get默认值。  
+SpUtil.getString('key', defValue: '');    
+SpUtil.getInt('key', defValue: 0);
+ 
 
 ### [Flutter工具类库 flustars][flustars_github]   
  1、SpUtil       : 单例"同步" SharedPreferences 工具类.  
@@ -42,6 +97,10 @@ dependencies:
 ### APIs
 * #### SpUtil -> [Example](https://github.com/Sky24n/flutter_wanandroid/blob/master/lib/ui/pages/demos/sp_util_page.dart)
 ```
+putObject
+getObject
+putObjectList
+getObjectList
 getString
 putString
 getBool
@@ -53,6 +112,7 @@ putDouble
 getStringList
 putStringList
 getDynamic
+haveKey
 getKeys
 remove
 clear
@@ -130,23 +190,25 @@ getWidgetBounds           : 获取widget 宽高.
 getWidgetLocalToGlobal    : 获取widget在屏幕上的坐标.
 ```
 
-## 关于作者,欢迎关注~
- [![jianshu][jianshuSvg]][jianshu]   [![juejin][juejinSvg]][juejin] 
- 
-## 最后,如果您觉得本项目不错的话,来个star支持下作者吧! 
+### 关于作者
+GitHub : [Sky24n](https://github.com/Sky24n)  
+简书 &nbsp;&nbsp;&nbsp;&nbsp;: [Sky24n](https://github.com/Sky24n)  
+掘金 &nbsp;&nbsp;&nbsp;&nbsp;: [Sky24n](https://github.com/Sky24n)  
+Pub &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: [Sky24n](https://pub.flutter-io.cn/packages?q=email%3A863764940%40qq.com)    
+Email &nbsp;&nbsp;: 863764940@qq.com  
+如果您觉得本项目不错的话,来个star支持下作者吧! 
 
-## Demo Github :  
- [flutter_wanandroid][flutter_wanandroid_github] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [flutter_demos][flutter_demos_github]
-## 点击下载APK :  
- [v0.1.2][flutter_wanandroid_apk] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [v1.0.4][flutter_demos_apk]
+### Demo Github : [flutter_wanandroid][flutter_wanandroid_github] 
+## 点击下载APK : [v0.1.2][flutter_wanandroid_apk] 
 ## 扫码下载APK :
-  ![flutter_wanandroid][flutter_wanandroid_qr] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ![flutter_demos][flutter_demos_qr]
+  ![flutter_wanandroid][flutter_wanandroid_qr] 
 
 ### Screenshot
 <img src="https://raw.githubusercontent.com/Sky24n/LDocuments/master/AppImgs/flutter_demos/Screenshot_20181003-234414.jpg" width="200">   <img src="https://raw.githubusercontent.com/Sky24n/LDocuments/master/AppImgs/flutter_demos/Screenshot_20181003-211011.jpg" width="200">   <img src="https://raw.githubusercontent.com/Sky24n/LDocuments/master/AppImgs/flutter_demos/Screenshot_20180930-012302.jpg" width="200">  
 <img src="https://raw.githubusercontent.com/Sky24n/LDocuments/master/AppImgs/flutter_demos/Screenshot_20180930-012431.jpg" width="200">  <img src="https://raw.githubusercontent.com/Sky24n/LDocuments/master/AppImgs/flutter_demos/Screenshot_20180919-231618.jpg" width="200">   <img src="https://raw.githubusercontent.com/Sky24n/LDocuments/master/AppImgs/flutter_demos/Screenshot_20180926-144840.png" width="200">  
 <img src="https://raw.githubusercontent.com/Sky24n/LDocuments/master/AppImgs/flutter_demos/Screenshot_20180919-224204.jpg" width="200">   <img src="https://raw.githubusercontent.com/Sky24n/LDocuments/master/AppImgs/flutter_demos/Screenshot_20180919-224146.jpg" width="200">   <img src="https://raw.githubusercontent.com/Sky24n/LDocuments/master/AppImgs/flutter_demos/Screenshot_20180919-224231.jpg" width="200">   
 
+
 [flutter_wanandroid_github]: https://github.com/Sky24n/flutter_wanandroid
 [flutter_wanandroid_apk]: https://raw.githubusercontent.com/Sky24n/LDocuments/master/AppStore/flutter_wanandroid.apk
 [flutter_wanandroid_qr]: https://raw.githubusercontent.com/Sky24n/LDocuments/master/AppImgs/flutter_wanandroid/qrcode.png

+ 6 - 0
doc/UPDATELOG.md

@@ -1,4 +1,10 @@
 ## 更新说明
+
+#### v0.2.2(2019.02.04) 
+v0.2.2 SpUtil新增get默认值。  
+SpUtil.getString('key', defValue: '');    
+SpUtil.getInt('key', defValue: 0);
+
 #### v0.1.9(2019.01.07)   
 移除DioUtil,如有需要,请到[flutter_wanandroid][flutter_wanandroid_github]该项目中copy。
 

+ 54 - 1
example/lib/main.dart

@@ -9,10 +9,31 @@ class MyApp extends StatefulWidget {
   _MyAppState createState() => new _MyAppState();
 }
 
+class City {
+  String name;
+
+  City({this.name});
+
+  City.fromJson(Map<String, dynamic> json) : name = json['name'];
+
+  Map<String, dynamic> toJson() => {
+        'name': name,
+      };
+
+  @override
+  String toString() {
+    StringBuffer sb = new StringBuffer('{');
+    sb.write("\"name\":\"$name\"");
+    sb.write('}');
+    return sb.toString();
+  }
+}
+
 class _MyAppState extends State<MyApp> {
   @override
   void initState() {
     super.initState();
+    _initAsync();
 
     /// 配置设计稿尺寸
     /// 如果设计稿尺寸默认配置一致,无需该设置。默认 width:360.0 / height:640.0 / density:3.0
@@ -21,6 +42,34 @@ class _MyAppState extends State<MyApp> {
     setDesignWHD(360.0, 640.0, density: 3);
   }
 
+  void _initAsync() async {
+    await SpUtil.getInstance();
+
+    /// save object example.
+    /// 存储实体对象示例。
+    City city = new City();
+    city.name = "成都市";
+    SpUtil.putObject("loc_city", city);
+
+    Map dataStr = SpUtil.getObject("loc_city");
+    City hisCity = dataStr == null ? null : City.fromJson(dataStr);
+    print("thll Str: " + (hisCity == null ? "null" : hisCity.toString()));
+
+    /// save object list example.
+    /// 存储实体对象list示例。
+    List<City> list = new List();
+    list.add(new City(name: "成都市"));
+    list.add(new City(name: "北京市"));
+    SpUtil.putObjectList("loc_city_list", null);
+
+    List<Map> dataList = SpUtil.getObjectList("loc_city_list");
+    List<City> _cityList = dataList?.map((value) {
+      return City.fromJson(value);
+    })?.toList();
+
+    print("thll List: " + (_cityList == null ? "null" : _cityList.toString()));
+  }
+
   @override
   Widget build(BuildContext context) {
     return new MaterialApp(
@@ -50,7 +99,11 @@ class MainPageState extends State<MainPage> {
     double statusBar = ScreenUtil.getInstance().statusBarHeight;
     double width = ScreenUtil.getInstance().screenWidth;
     double height = ScreenUtil.getInstance().screenHeight;
-    print("MainPage statusBar: $statusBar, width: $width, height: $height");
+    double density = ScreenUtil.getInstance().screenDensity;
+    double sp = ScreenUtil.getInstance().getSp(24);
+    double spc = ScreenUtil.getScaleSp(context, 24);
+    print(
+        "MainPage statusBar: $statusBar, width: $width, height: $height, density: $density, sp: $sp, spc: $spc");
 
     return new Scaffold(
       // 一个不需要GlobalKey就可以openDrawer的AppBar

+ 7 - 18
lib/src/screen_util.dart

@@ -4,8 +4,8 @@ import 'dart:ui' as ui show window;
 /**
  * @Author: thl
  * @GitHub: https://github.com/Sky24n
- * @JianShu: https://www.jianshu.com/u/cbf2ad25d33a
  * @Email: 863764940@qq.com
+ * @Email: sky24no@gmail.com
  * @Description: Screen Util.
  * @Date: 2018/9/8
  */
@@ -38,7 +38,6 @@ class ScreenUtil {
   double _statusBarHeight = 0.0;
   double _bottomBarHeight = 0.0;
   double _appBarHeight = 0.0;
-  double _textScaleFactor = 1.0;
   MediaQueryData _mediaQueryData;
 
   static final ScreenUtil _singleton = ScreenUtil();
@@ -57,7 +56,6 @@ class ScreenUtil {
       _screenDensity = mediaQuery.devicePixelRatio;
       _statusBarHeight = mediaQuery.padding.top;
       _bottomBarHeight = mediaQuery.padding.bottom;
-      _textScaleFactor = mediaQuery.textScaleFactor;
       _appBarHeight = kToolbarHeight;
     }
   }
@@ -146,16 +144,11 @@ class ScreenUtil {
   }
 
   /// returns the font size after adaptation according to the screen density.
-  /// 返回根据屏幕Density适配后字体尺寸
+  /// 返回根据屏幕适配后字体尺寸
   /// fontSize 字体尺寸
-  /// sySystem 是否跟随系统字体大小设置,默认 true。Whether to follow the system font size settings, default true.
-  static double getScaleSp(BuildContext context, double fontSize,
-      {bool sySystem: true}) {
+  static double getScaleSp(BuildContext context, double fontSize) {
     if (context == null || getScreenDensity(context) == 0.0) return fontSize;
-    return (sySystem ? MediaQuery.of(context).textScaleFactor : 1.0) *
-        fontSize *
-        getScreenDensity(context) /
-        _designD;
+    return fontSize * getScreenW(context) / _designW;
   }
 
   /// Orientation
@@ -198,14 +191,10 @@ class ScreenUtil {
   }
 
   /// returns the font size after adaptation according to the screen density.
-  /// 返回根据屏幕Density适配后字体尺寸
+  /// 返回根据屏幕适配后字体尺寸
   /// fontSize 字体尺寸
-  /// sySystem 是否跟随系统字体大小设置,默认 true。Whether to follow the system font size settings, default true.
-  double getSp(double fontSize, {bool sySystem: true}) {
+  double getSp(double fontSize) {
     if (_screenDensity == 0.0) return fontSize;
-    return (sySystem ? _textScaleFactor : 1.0) *
-        fontSize *
-        _screenDensity /
-        _designD;
+    return fontSize * _screenWidth / _designW;
   }
 }

+ 54 - 1
lib/src/sp_util.dart

@@ -1,4 +1,5 @@
 import 'dart:async';
+import 'dart:convert';
 
 import 'package:shared_preferences/shared_preferences.dart';
 import 'package:synchronized/synchronized.dart';
@@ -6,8 +7,8 @@ import 'package:synchronized/synchronized.dart';
 /**
  * @Author: thl
  * @GitHub: https://github.com/Sky24n
- * @JianShu: https://www.jianshu.com/u/cbf2ad25d33a
  * @Email: 863764940@qq.com
+ * @Email: sky24no@gmail.com
  * @Description: Sp Util.
  * @Date: 2018/9/8
  */
@@ -39,72 +40,124 @@ class SpUtil {
     _prefs = await SharedPreferences.getInstance();
   }
 
+  /// put object.
+  static Future<bool> putObject(String key, Object value) {
+    if (_prefs == null) return null;
+    return _prefs.setString(key, value == null ? "" : json.encode(value));
+  }
+
+  /// get object.
+  static Map getObject(String key) {
+    if (_prefs == null) return null;
+    String _data = _prefs.getString(key);
+    return (_data == null || _data.isEmpty) ? null : json.decode(_data);
+  }
+
+  /// put object list.
+  static Future<bool> putObjectList(String key, List<Object> list) {
+    if (_prefs == null) return null;
+    List<String> _dataList = list?.map((value) {
+      return json.encode(value);
+    })?.toList();
+    return _prefs.setStringList(key, _dataList);
+  }
+
+  /// get object list.
+  static List<Map> getObjectList(String key) {
+    if (_prefs == null) return null;
+    List<String> dataLis = _prefs.getStringList(key);
+    return dataLis?.map((value) {
+      Map _dataMap = json.decode(value);
+      return _dataMap;
+    })?.toList();
+  }
+
+  /// get string.
   static String getString(String key, {String defValue: ''}) {
     if (_prefs == null) return defValue;
     return _prefs.getString(key) ?? defValue;
   }
 
+  /// put string.
   static Future<bool> putString(String key, String value) {
     if (_prefs == null) return null;
     return _prefs.setString(key, value);
   }
 
+  /// get bool.
   static bool getBool(String key, {bool defValue: false}) {
     if (_prefs == null) return defValue;
     return _prefs.getBool(key) ?? defValue;
   }
 
+  /// put bool.
   static Future<bool> putBool(String key, bool value) {
     if (_prefs == null) return null;
     return _prefs.setBool(key, value);
   }
 
+  /// get int.
   static int getInt(String key, {int defValue: 0}) {
     if (_prefs == null) return defValue;
     return _prefs.getInt(key) ?? defValue;
   }
 
+  /// put int.
   static Future<bool> putInt(String key, int value) {
     if (_prefs == null) return null;
     return _prefs.setInt(key, value);
   }
 
+  /// get double.
   static double getDouble(String key, {double defValue: 0.0}) {
     if (_prefs == null) return defValue;
     return _prefs.getDouble(key) ?? defValue;
   }
 
+  /// put double.
   static Future<bool> putDouble(String key, double value) {
     if (_prefs == null) return null;
     return _prefs.setDouble(key, value);
   }
 
+  /// get string list.
   static List<String> getStringList(String key,
       {List<String> defValue: const []}) {
     if (_prefs == null) return defValue;
     return _prefs.getStringList(key) ?? defValue;
   }
 
+  /// put string list.
   static Future<bool> putStringList(String key, List<String> value) {
     if (_prefs == null) return null;
     return _prefs.setStringList(key, value);
   }
 
+  /// get dynamic.
   static dynamic getDynamic(String key, {Object defValue}) {
     if (_prefs == null) return defValue;
     return _prefs.get(key) ?? defValue;
   }
 
+  /// have key.
+  static bool haveKey(String key) {
+    if (_prefs == null) return null;
+    return _prefs.getKeys().contains(key);
+  }
+
+  /// get keys.
   static Set<String> getKeys() {
     if (_prefs == null) return null;
     return _prefs.getKeys();
   }
 
+  /// remove.
   static Future<bool> remove(String key) {
     if (_prefs == null) return null;
     return _prefs.remove(key);
   }
 
+  /// clear.
   static Future<bool> clear() {
     if (_prefs == null) return null;
     return _prefs.clear();

+ 1 - 1
lib/src/widget_util.dart

@@ -3,8 +3,8 @@ import 'package:flutter/widgets.dart';
 /**
  * @Author: thl
  * @GitHub: https://github.com/Sky24n
- * @JianShu: https://www.jianshu.com/u/cbf2ad25d33a
  * @Email: 863764940@qq.com
+ * @Email: sky24no@gmail.com
  * @Description: Widget Util.
  * @Date: 2018/9/10
  */

+ 2 - 3
pubspec.yaml

@@ -12,10 +12,9 @@ dependencies:
     sdk: flutter
 
   # https://github.com/tekartik/synchronized.dart
-  synchronized: ^1.5.3
+  synchronized:
   # https://github.com/flutter/plugins/tree/master/packages/shared_preferences
-  shared_preferences: ^0.5.0
-
+  shared_preferences:
 
 # For information on the generic Dart part of this file, see the
 # following page: https://www.dartlang.org/tools/pub/pubspec