소스 검색

v0.2.6 pre2

thl 6 년 전
부모
커밋
639ab5792f
9개의 변경된 파일233개의 추가작업 그리고 50개의 파일을 삭제
  1. 4 0
      CHANGELOG.md
  2. 13 1
      README.md
  3. 26 20
      example/lib/main.dart
  4. 2 0
      lib/flustars.dart
  5. 132 0
      lib/src/directory_util.dart
  6. 43 4
      lib/src/screen_util.dart
  7. 0 20
      lib/src/utils.dart
  8. 7 3
      lib/src/widget_util.dart
  9. 6 2
      pubspec.yaml

+ 4 - 0
CHANGELOG.md

@@ -1,3 +1,7 @@
+### 0.2.6
+
+* TODO: xxx.
+
 ### 0.2.5+1
 
 * TODO: fix WidgetUtil error with flutter v1.6.3.

+ 13 - 1
README.md

@@ -1,7 +1,7 @@
 # flustars(Flutter常用工具类库)
 [![Pub](https://img.shields.io/pub/v/flustars.svg?style=flat-square)](https://pub.dartlang.org/packages/flustars)
 
-## 主要对第三方库封装,以便于使用。如果你有好的工具类欢迎PR. 
+## flustars依赖于Dart常用工具类库[common_utils](https://github.com/Sky24n/common_utils),以及对其他第三方库封装,致力于为大家提供简单易用工具类。如果你有好的工具类欢迎PR. 
 目前包含SharedPreferences Util, Screen Util, Widget Util。
 
 ### 关于使用本开源库规则
@@ -21,6 +21,9 @@ import 'package:flustars/flustars.dart';
 
 ## [更新说明](./doc/UPDATELOG.md)
 
+
+
+
 🔥🔥🔥Flutter全局屏幕适配[auto_size](https://github.com/flutterchina/auto_size),欢迎使用~  
   
 v0.2.5 (2019.03.07)  
@@ -329,6 +332,15 @@ getBottomBarH(ctx)        : 当前BottomBar高度.
 getScaleW(ctx,size)       : 返回根据屏幕宽适配后尺寸.
 getScaleH(ctx,size)       : 返回根据屏幕高适配后尺寸.
 getScaleSp(ctx,size)      : 返回根据屏幕宽适配后字体尺寸.  
+getScaleSp(ctx,size)      : 返回根据屏幕宽适配后字体尺寸.  
+
+///旧适配方法仅适用于纵屏适配。
+///推荐使用以下新适配方法。
+getAdapterSize(size)             : 返回适配后尺寸,可用于宽,高,字体尺寸.  
+getAdapterSizeCtx(ctx,size)      : 返回适配后尺寸,可用于宽,高,字体尺寸.   
+
+double adapterSize = ScreenUtil.getInstance().getAdapterSize(100);
+double adapterSize = ScreenUtil.getAdapterSizeCtx(context, 100);
   
 一、不依赖context
 // 屏幕宽

+ 26 - 20
example/lib/main.dart

@@ -50,7 +50,7 @@ class _MyAppState extends State<MyApp> {
 
     SpUtil.putString("username", "sky24");
     String userName = SpUtil.getString("username", defValue: "");
-    print("thll userName: " + userName);
+    print("thll  thll userName: " + userName);
 
     /// save object example.
     /// 存储实体对象示例。
@@ -60,7 +60,8 @@ class _MyAppState extends State<MyApp> {
 
     Map dataStr = SpUtil.getObject("loc_city");
     City hisCity = dataStr == null ? null : City.fromJson(dataStr);
-    print("thll City: " + (hisCity == null ? "null" : hisCity.toString()));
+    print(
+        "thll  thll City: " + (hisCity == null ? "null" : hisCity.toString()));
 
     /// save object list example.
     /// 存储实体对象list示例。
@@ -74,7 +75,8 @@ class _MyAppState extends State<MyApp> {
       return City.fromJson(value);
     })?.toList();
 
-    print("thll CityList: " + (_cityList == null ? "null" : _cityList.toString()));
+    print("thll  thll CityList: " +
+        (_cityList == null ? "null" : _cityList.toString()));
   }
 
   @override
@@ -107,10 +109,12 @@ class MainPageState extends State<MainPage> {
     double width = ScreenUtil.getInstance().screenWidth;
     double height = ScreenUtil.getInstance().screenHeight;
     double density = ScreenUtil.getInstance().screenDensity;
-    double sp = ScreenUtil.getInstance().getSp(24);
-    double spc = ScreenUtil.getScaleSp(context, 24);
+    double sp = ScreenUtil.getInstance().getAdapterSize(24);
+    double spc = ScreenUtil.getInstance().getAdapterSize(24);
+    double adapterW = ScreenUtil.getInstance().getAdapterSize(360);
+
     print(
-        "MainPage statusBar: $statusBar, width: $width, height: $height, density: $density, sp: $sp, spc: $spc");
+        "thll  MainPage statusBar: $statusBar, width: $width, height: $height, density: $density, sp: $sp, spc: $spc, adapterW: $adapterW");
 
     return new Scaffold(
       // 一个不需要GlobalKey就可以openDrawer的AppBar
@@ -147,7 +151,7 @@ class MainPageState extends State<MainPage> {
             ),
           ),
           new Container(
-            width: ScreenUtil.getInstance().getWidth(360.0),
+            width: ScreenUtil.getInstance().getAdapterSize(360.0),
             height: 50,
             color: Colors.grey,
             child: new Center(
@@ -170,14 +174,14 @@ class MainPageState extends State<MainPage> {
           ),
           new Container(
             margin: EdgeInsets.only(top: 10.0),
-            width: ScreenUtil.getInstance().getWidth(100.0),
-            height: ScreenUtil.getInstance().getWidth(100.0),
+            width: ScreenUtil.getInstance().getAdapterSize(100.0),
+            height: ScreenUtil.getInstance().getAdapterSize(100.0),
             color: Colors.grey,
             child: new Center(
               child: new Text(
                 "你好你好你好",
                 style: new TextStyle(
-                    fontSize: ScreenUtil.getInstance().getSp(24.0)),
+                    fontSize: ScreenUtil.getInstance().getAdapterSize(24.0)),
               ),
             ),
           ),
@@ -194,7 +198,8 @@ class MyDrawer extends StatelessWidget {
     double statusBar = ScreenUtil.getInstance().statusBarHeight;
     double width = ScreenUtil.getInstance().screenWidth;
     double height = ScreenUtil.getInstance().screenHeight;
-    print("SecondPage statusBar: $statusBar, width: $width, height: $height");
+    print(
+        " thll  SecondPage statusBar: $statusBar, width: $width, height: $height");
 
     return new Container(
       color: Colors.white,
@@ -248,7 +253,7 @@ class SecondPageState extends State<SecondPage> {
     double adapterW100px = ScreenUtil.getInstance().getWidthPx(300);
     double adapterH100px = ScreenUtil.getInstance().getHeightPx(300);
 
-    print("SecondPage _init screenWidth: $screenWidth, screenHeight: $screenHeight, screenDensity: $screenDensity" +
+    print("thll  SecondPage _init screenWidth: $screenWidth, screenHeight: $screenHeight, screenDensity: $screenDensity" +
         ", statusBarHeight: $statusBarHeight, bottomBarHeight: $bottomBarHeight, appBarHeight: $appBarHeight" +
         ", adapterW100: $adapterW100, adapterH100: $adapterH100, adapterSp100: $adapterSp100" +
         ", adapterW100px: $adapterW100px, adapterH100px: $adapterH100px");
@@ -265,7 +270,7 @@ class SecondPageState extends State<SecondPage> {
     double adapterSp100 = ScreenUtil.getScaleSp(context, 100);
     Orientation orientation = ScreenUtil.getOrientation(context);
 
-    print("SecondPage _initWithCtx screenWidth: $screenWidth, screenHeight: $screenHeight, screenDensity: $screenDensity" +
+    print("thll  SecondPage _initWithCtx screenWidth: $screenWidth, screenHeight: $screenHeight, screenDensity: $screenDensity" +
         ", statusBarHeight: $statusBarHeight, bottomBarHeight: $bottomBarHeight" +
         ", adapterW100: $adapterW100, adapterH100: $adapterH100, adapterSp100: $adapterSp100");
   }
@@ -275,7 +280,8 @@ class SecondPageState extends State<SecondPage> {
     double statusBar = ScreenUtil.getInstance().statusBarHeight;
     double width = ScreenUtil.getInstance().screenWidth;
     double height = ScreenUtil.getInstance().screenHeight;
-    print("SecondPage statusBar: $statusBar, width: $width, height: $height");
+    print(
+        "thll  SecondPage statusBar: $statusBar, width: $width, height: $height");
 
     return new Scaffold(
       appBar: new AppBar(
@@ -298,27 +304,27 @@ class SecondPageState extends State<SecondPage> {
           ),
           new Container(
             margin: EdgeInsets.only(top: 10.0),
-            width: ScreenUtil.getInstance().getWidth(100.0),
-            height: ScreenUtil.getInstance().getWidth(100.0),
+            width: ScreenUtil.getInstance().getAdapterSize(100.0),
+            height: ScreenUtil.getInstance().getAdapterSize(100.0),
             color: Colors.grey,
             child: new Center(
               child: new Text(
                 "你好你好你好",
                 style: new TextStyle(
-                    fontSize: ScreenUtil.getInstance().getSp(24.0)),
+                    fontSize: ScreenUtil.getInstance().getAdapterSize(24.0)),
               ),
             ),
           ),
           new Container(
             margin: EdgeInsets.only(top: 10.0),
-            width: ScreenUtil.getScaleW(context, 100.0),
-            height: ScreenUtil.getScaleW(context, 100.0),
+            width: ScreenUtil.getAdapterSizeCtx(context, 100.0),
+            height: ScreenUtil.getAdapterSizeCtx(context, 100.0),
             color: Colors.grey,
             child: new Center(
               child: new Text(
                 "你好你好你好",
                 style: new TextStyle(
-                    fontSize: ScreenUtil.getScaleSp(context, 24.0)),
+                    fontSize: ScreenUtil.getAdapterSizeCtx(context, 24.0)),
               ),
             ),
           ),

+ 2 - 0
lib/flustars.dart

@@ -2,6 +2,8 @@ library flustars;
 
 export 'src/ui/my_app_bar.dart';
 
+export 'package:common_utils/common_utils.dart';
+
 export 'src/screen_util.dart';
 export 'src/widget_util.dart';
 export 'src/sp_util.dart';

+ 132 - 0
lib/src/directory_util.dart

@@ -0,0 +1,132 @@
+import 'dart:async';
+import 'dart:io';
+
+import 'package:common_utils/common_utils.dart';
+import 'package:path_provider/path_provider.dart';
+import 'package:synchronized/synchronized.dart';
+
+/// getTemporaryDirectory
+/// 指向设备上临时目录的路径,该目录没有备份,适合存储下载文件的缓存。
+/// 此目录中的文件可以随时清除。这不会返回一个新的临时目录。相反,调用者负责在这个目录中创建(和清理)文件或目录。这个目录的作用域是调用应用程序。
+/// 在iOS上,它使用“NSCachesDirectory”API。
+/// 在Android上,它在上下文中使用“getCacheDir”API。
+
+/// getApplicationSupportDirectory
+/// 应用程序可以放置应用程序支持文件的目录的路径。
+/// 对不希望向用户公开的文件使用此选项。您的应用程序不应将此目录用于用户数据文件。
+/// 在iOS上,它使用“NSApplicationSupportDirectory”API。如果此目录不存在,则自动创建。
+/// 在Android上,此函数抛出一个[UnsupportedError]。
+
+/// getApplicationDocumentsDirectory
+/// 指向应用程序可以放置用户生成的数据或应用程序无法重新创建的数据的目录的路径。
+/// 在iOS上,它使用“NSDocumentDirectory”API。如果数据不是用户生成的,请考虑使用[GetApplicationSupportDirectory]。
+/// 在Android上,这在上下文中使用了“getDataDirectory”API。如果数据对用户可见,请考虑改用getExternalStorageDirectory。
+
+/// getExternalStorageDirectory
+/// 应用程序可以访问顶层存储的目录的路径。在发出这个函数调用之前,应该确定当前操作系统,因为这个功能只在Android上可用。
+/// 在iOS上,这个函数抛出一个[UnsupportedError],因为它不可能访问应用程序的沙箱之外。
+/// 在Android上,它使用“getExternalStorageDirectory”API。
+
+/**
+ * @Author: thl
+ * @GitHub: https://github.com/Sky24n
+ * @JianShu: https://www.jianshu.com/u/cbf2ad25d33a
+ * @Email: 863764940@qq.com
+ * @Description: Directory Util.
+ * @Date: 2019/05/09
+ */
+
+///
+class DirectoryUtil {
+  static DirectoryUtil _singleton;
+
+  static Lock _lock = Lock();
+
+  static Directory _tempDir;
+  static Directory _appDocDir;
+  static Directory _storageDir;
+
+  static Future<DirectoryUtil> getInstance() async {
+    if (_singleton == null) {
+      await _lock.synchronized(() async {
+        if (_singleton == null) {
+          // keep local instance till it is fully initialized.
+          // 保持本地实例直到完全初始化。
+          var singleton = DirectoryUtil._();
+          await singleton._init();
+          _singleton = singleton;
+        }
+      });
+    }
+    return _singleton;
+  }
+
+  DirectoryUtil._();
+
+  Future _init() async {
+    _tempDir = await getTemporaryDirectory();
+    _appDocDir = await getApplicationDocumentsDirectory();
+    if (Platform.isAndroid) {
+      _storageDir = await getExternalStorageDirectory();
+    }
+    if (Platform.isIOS) {
+      _storageDir = await getApplicationSupportDirectory();
+    }
+  }
+
+  static String getTempPath(
+      {String fileName,
+      String format,
+      String package = 'doc',
+      String category}) {
+    if (_tempDir == null) return null;
+    StringBuffer sb = new StringBuffer("${_tempDir.path}");
+    if (!ObjectUtil.isEmpty(package)) sb.write("/$package");
+    if (!ObjectUtil.isEmpty(category)) sb.write("/$category");
+    if (!ObjectUtil.isEmpty(fileName)) sb.write("/$fileName");
+    if (!ObjectUtil.isEmpty(format)) sb.write(".$format");
+    return sb.toString();
+  }
+
+  static String getAppDocPath(
+      {String fileName,
+      String format,
+      String package = 'doc',
+      String category}) {
+    if (_appDocDir == null) return null;
+    StringBuffer sb = new StringBuffer("${_appDocDir.path}");
+    if (!ObjectUtil.isEmpty(package)) sb.write("/$package");
+    if (!ObjectUtil.isEmpty(category)) sb.write("/$category");
+    if (!ObjectUtil.isEmpty(fileName)) sb.write("/$fileName");
+    if (!ObjectUtil.isEmpty(format)) sb.write(".$format");
+    return sb.toString();
+  }
+
+  static String getStoragePath(
+      {String fileName, String format, String package, String category}) {
+    if (_storageDir == null) return null;
+    StringBuffer sb = new StringBuffer("${_storageDir.path}");
+    if (!ObjectUtil.isEmpty(package)) sb.write("/$package");
+    if (!ObjectUtil.isEmpty(category)) sb.write("/$category");
+    if (!ObjectUtil.isEmpty(fileName)) sb.write("/$fileName");
+    if (!ObjectUtil.isEmpty(format)) sb.write(".$format");
+    return sb.toString();
+  }
+
+  static Directory createDirSync(String path) {
+    Directory dir = new Directory(path);
+    if (!dir.existsSync()) {
+      dir.createSync(recursive: true);
+    }
+    return dir;
+  }
+
+  static Future<Directory> createDir(String path) async {
+    Directory dir = new Directory(path);
+    bool exist = await dir.exists();
+    if (!exist) {
+      dir = await dir.create(recursive: true);
+    }
+    return dir;
+  }
+}

+ 43 - 4
lib/src/screen_util.dart

@@ -25,9 +25,9 @@ double _designD = 3.0;
 /// 配置设计稿尺寸 屏幕 宽,高,密度。
 /// Configuration design draft size  screen width, height, density.
 void setDesignWHD(double w, double h, {double density = 3.0}) {
-  _designW = w;
-  _designH = h;
-  _designD = density;
+  _designW = w ?? _designW;
+  _designH = h ?? _designH;
+  _designD = density ?? _designD;
 }
 
 /// Screen Util.
@@ -127,6 +127,7 @@ class ScreenUtil {
     return mediaQuery;
   }
 
+  /// 仅支持纵屏。
   /// returns the size after adaptation according to the screen width.(unit dp or pt)
   /// 返回根据屏幕宽适配后尺寸(单位 dp or pt)
   /// size 单位 dp or pt
@@ -135,6 +136,7 @@ class ScreenUtil {
     return size * getScreenW(context) / _designW;
   }
 
+  /// 仅支持纵屏。
   /// returns the size after adaptation according to the screen height.(unit dp or pt)
   /// 返回根据屏幕高适配后尺寸 (单位 dp or pt)
   /// size unit dp or pt
@@ -143,11 +145,12 @@ class ScreenUtil {
     return size * getScreenH(context) / _designH;
   }
 
+  /// 仅支持纵屏。
   /// returns the font size after adaptation according to the screen density.
   /// 返回根据屏幕宽适配后字体尺寸
   /// fontSize 字体尺寸
   static double getScaleSp(BuildContext context, double fontSize) {
-    if (context == null || getScreenDensity(context) == 0.0) return fontSize;
+    if (context == null || getScreenW(context) == 0.0) return fontSize;
     return fontSize * getScreenW(context) / _designW;
   }
 
@@ -158,6 +161,7 @@ class ScreenUtil {
     return mediaQuery.orientation;
   }
 
+  /// 仅支持纵屏。
   /// returns the size after adaptation according to the screen width.(unit dp or pt)
   /// 返回根据屏幕宽适配后尺寸(单位 dp or pt)
   /// size 单位 dp or pt
@@ -165,6 +169,7 @@ class ScreenUtil {
     return _screenWidth == 0.0 ? size : (size * _screenWidth / _designW);
   }
 
+  /// 仅支持纵屏。
   /// returns the size after adaptation according to the screen height.(unit dp or pt)
   /// 返回根据屏幕高适配后尺寸(单位 dp or pt)
   /// size unit dp or pt
@@ -172,6 +177,7 @@ class ScreenUtil {
     return _screenHeight == 0.0 ? size : (size * _screenHeight / _designH);
   }
 
+  /// 仅支持纵屏
   /// returns the size after adaptation according to the screen width.(unit dp or pt)
   /// 返回根据屏幕宽适配后尺寸(单位 dp or pt)
   /// sizePx unit px
@@ -181,6 +187,7 @@ class ScreenUtil {
         : (sizePx * _screenWidth / (_designW * _designD));
   }
 
+  /// 仅支持纵屏。
   /// returns the size after adaptation according to the screen height.(unit dp or pt)
   /// 返回根据屏幕高适配后尺寸(单位 dp or pt)
   /// sizePx unit px
@@ -190,6 +197,7 @@ class ScreenUtil {
         : (sizePx * _screenHeight / (_designH * _designD));
   }
 
+  /// 仅支持纵屏。
   /// returns the font size after adaptation according to the screen density.
   /// 返回根据屏幕宽适配后字体尺寸
   /// fontSize 字体尺寸
@@ -197,4 +205,35 @@ class ScreenUtil {
     if (_screenDensity == 0.0) return fontSize;
     return fontSize * _screenWidth / _designW;
   }
+
+  /// 兼容横/纵屏。
+  /// 获取适配后的尺寸,兼容横/纵屏切换,可用于宽,高,字体尺寸适配。
+  /// Get the appropriate size, compatible with horizontal/vertical screen switching, can be used for wide, high, font size adaptation.
+  double getAdapterSize(double dp) {
+    if (_screenWidth == 0 || _screenHeight == 0) return dp;
+    return getRatio() * dp;
+  }
+
+  /// 适配比率。
+  /// Ratio.
+  double getRatio() {
+    return (_screenWidth > _screenHeight ? _screenHeight : _screenWidth) /
+        _designW;
+  }
+
+  /// 兼容横/纵屏。
+  /// 获取适配后的尺寸,兼容横/纵屏切换,适应宽,高,字体尺寸。
+  /// Get the appropriate size, compatible with horizontal/vertical screen switching, can be used for wide, high, font size adaptation.
+  static double getAdapterSizeCtx(BuildContext context, double dp) {
+    Size size = MediaQuery.of(context).size;
+    if (size == Size.zero) return dp;
+    return getRatioCtx(context) * dp;
+  }
+
+  /// 适配比率。
+  /// Ratio.
+  static double getRatioCtx(BuildContext context) {
+    Size size = MediaQuery.of(context).size;
+    return (size.width > size.height ? size.height : size.width) / _designW;
+  }
 }

+ 0 - 20
lib/src/utils.dart

@@ -1,20 +0,0 @@
-/**
- * @Author: thl
- * @GitHub: https://github.com/Sky24n
- * @Email: 863764940@qq.com
- * @Email: sky24no@gmail.com
- * @Description: Widget Util.
- * @Date: 2019/03/07
- */
-
-/// Utils.
-class Utils {
-  /// isEmpty.
-  static bool isEmpty(Object value) {
-    if (value == null) return true;
-    if (value is String && value.isEmpty) {
-      return true;
-    }
-    return false;
-  }
-}

+ 7 - 3
lib/src/widget_util.dart

@@ -1,5 +1,5 @@
 import 'dart:async';
-import 'package:flustars/src/utils.dart';
+import 'package:common_utils/common_utils.dart';
 import 'package:flutter/widgets.dart';
 
 /**
@@ -72,7 +72,9 @@ class WidgetUtil {
   /// local url , package
   static Future<Rect> getImageWH(
       {Image image, String url, String localUrl, String package}) {
-    if (Utils.isEmpty(image) && Utils.isEmpty(url) && Utils.isEmpty(localUrl)) {
+    if (ObjectUtil.isEmpty(image) &&
+        ObjectUtil.isEmpty(url) &&
+        ObjectUtil.isEmpty(localUrl)) {
       return Future.value(Rect.zero);
     }
     Completer<Rect> completer = Completer<Rect>();
@@ -103,7 +105,9 @@ class WidgetUtil {
   /// package
   static Future<Rect> getImageWHE(
       {Image image, String url, String localUrl, String package}) {
-    if (Utils.isEmpty(image) && Utils.isEmpty(url) && Utils.isEmpty(localUrl)) {
+    if (ObjectUtil.isEmpty(image) &&
+        ObjectUtil.isEmpty(url) &&
+        ObjectUtil.isEmpty(localUrl)) {
       return Future.error("image is null.");
     }
     Completer<Rect> completer = Completer<Rect>();

+ 6 - 2
pubspec.yaml

@@ -1,6 +1,6 @@
 name: flustars
 description: Flutter common utils library. SpUtil, ScreenUtil, WidgetUtil. SharedPreferences Util. Screen info & Screen adaptation. get image size.
-version: 0.2.5+1
+version: 0.2.6
 author: thl <863764940@qq.com>
 homepage: https://github.com/Sky24n/flustars
 
@@ -10,7 +10,11 @@ environment:
 dependencies:
   flutter:
     sdk: flutter
+  # Dart 常用工具类库 https://github.com/Sky24n/common_utils
+  common_utils: '>=1.1.0 <3.0.0'
   # https://github.com/tekartik/synchronized.dart
   synchronized: '>=0.1.0 <3.0.0'
   # https://github.com/flutter/plugins/tree/master/packages/shared_preferences
-  shared_preferences: '>=0.1.1 <2.0.0'
+  shared_preferences: '>=0.1.1 <3.0.0'
+  # https://github.com/flutter/plugins/tree/master/packages/path_provider
+  path_provider: '>=0.4.0 <3.0.0'