Explorar o código

version 0.0.6

tanghongliang %!s(int64=7) %!d(string=hai) anos
pai
achega
8a46f70d53
Modificáronse 9 ficheiros con 324 adicións e 61 borrados
  1. 107 6
      README.md
  2. 9 26
      example/lib/main.dart
  3. 3 3
      lib/flustars.dart
  4. 0 13
      lib/src/db/db.dart
  5. 0 5
      lib/src/net/net.dart
  6. 66 0
      lib/src/screen_util.dart
  7. 80 4
      lib/src/sp_util.dart
  8. 54 0
      lib/src/widget_util.dart
  9. 5 4
      pubspec.yaml

+ 107 - 6
README.md

@@ -1,10 +1,111 @@
-# flustars
+# flustars(Flutter常用工具类库)
+[![Pub](https://img.shields.io/pub/v/flustars.svg?style=flat-square)](https://pub.dartlang.org/packages/flustars)
 
-A new Flutter plugin.
+## [flustars] Flutter常用工具类库。主要对第三方库封装,以便更方便使用。依赖于common_utils库。如果你有好的工具类欢迎PR.  
+ 1、SpUtil       : SharedPreferences 工具类.  
+ 2、ScreenUtil   : 获取屏幕宽、高、密度,AppBar高,状态栏高度,屏幕方向.  
+ 3、WidgetUtil   : 获取Widget宽高,在屏幕上的坐标.  
 
-## Getting Started
+### common_utils
+ 1、TimelineUtil : 时间轴.(新)  
+ 2、TimerUtil    : 倒计时,定时任务.(新)  
+ 3、MoneyUtil    : 分转元,支持格式输出.(新)  
+ 4、LogUtil      : 简单封装打印日志.(新)  
+ 5、DateUtil     : 日期转换格式化输出.  
+ 6、RegexUtil    : 正则验证手机号,身份证,邮箱等等.  
+ 7、NumUtil      : 保留x位小数.  
+ 8、ObjectUtil  : 判断对象是否为空(String List Map),判断两个List是否相等. 
 
-For help getting started with Flutter, view our online
-[documentation](https://flutter.io/).
+## Demo: [flutter_demos](https://github.com/Sky24n/flutter_demos).
+## APK: [点击下载v1.0.2](https://raw.githubusercontent.com/Sky24n/LDocuments/master/AppStore/flutter_demos.apk)
+## Android扫码下载APK
+  ![](https://github.com/Sky24n/LDocuments/blob/master/AppImgs/flutter_demos/qrcode.png)
+
+### Screenshot
+<img src="https://github.com/Sky24n/LDocuments/blob/master/AppImgs/flutter_demos/Screenshot_20181003-234414.jpg" width="200">   <img src="https://github.com/Sky24n/LDocuments/blob/master/AppImgs/flutter_demos/Screenshot_20181003-211011.jpg" width="200">   <img src="https://github.com/Sky24n/LDocuments/blob/master/AppImgs/flutter_demos/Screenshot_20180930-012302.jpg" width="200">  
+<img src="https://github.com/Sky24n/LDocuments/blob/master/AppImgs/flutter_demos/Screenshot_20180930-012431.jpg" width="200">  <img src="https://github.com/Sky24n/LDocuments/blob/master/AppImgs/flutter_demos/Screenshot_20180919-231618.jpg" width="200">   <img src="https://github.com/Sky24n/LDocuments/blob/master/AppImgs/flutter_demos/Screenshot_20180926-144840.png" width="200">  
+<img src="https://github.com/Sky24n/LDocuments/blob/master/AppImgs/flutter_demos/Screenshot_20180919-224204.jpg" width="200">   <img src="https://github.com/Sky24n/LDocuments/blob/master/AppImgs/flutter_demos/Screenshot_20180919-224146.jpg" width="200">   <img src="https://github.com/Sky24n/LDocuments/blob/master/AppImgs/flutter_demos/Screenshot_20180919-224231.jpg" width="200">   
+
+### APIs
+
+```yaml
+dependencies:
+  flustars: x.x.x  #latest version
+```
+* #### ScreenUtil
+```
+getString
+putString
+getBool
+putBool
+getInt
+putInt
+getDouble
+putDouble
+getStringList
+putStringList
+getDynamic
+getKeys
+remove
+clear
+```
+
+* #### ScreenUtil
+```
+screenWidth               : 获取屏幕宽.
+screenHeight              : 获取屏幕高.
+screenDensity             : 获取屏幕密度.
+appBarHeight              : 获取系统AppBar高度.
+statusBarHeight           : 获取系统状态栏高度.
+getScreenWidth            : 获取当前屏幕宽.
+getScreenHeight           : 获取当前屏幕高.
+getOrientation            : 获取当前屏幕方向.
+```
+
+* #### WidgetUtil
+```
+asyncPrepare              : 监听widget宽高变化,callback返回宽高等参数.
+getWidgetBounds           : 获取widget 宽高.
+getWidgetLocalToGlobal    : 获取widget在屏幕上的坐标.
+```
+
+
+### Example
+
+``` dart
+// Import package
+import 'package:flustars/flustars.dart';
+
+
+//First Page init. Notice!!!
+ScreenUtil.getInstance().init(context);
+
+ScreenUtil.screenWidth
+ScreenUtil.screenHeight
+ScreenUtil.statusBarHeight
+ScreenUtil.screenDensity
+
+// Global variable,Reference example
+WidgetUtil widgetUtil = new WidgetUtil();
+
+@override
+Widget build(BuildContext context) {
+  widgetUtil.asyncPrepare(context, false, (Rect rect) {
+     double width = rect.width;
+     double height = rect.height;
+  });
+    return ;
+ }
+
+//Widgets must be rendered completely. Otherwise return Rect.zero.
+Rect rect = WidgetUtil.getWidgetBounds(context);
+double width = rect.width;
+double height = rect.height;
+
+//Widgets must be rendered completely. Otherwise return Offset.zero.
+Offset offset = WidgetUtil.getWidgetLocalToGlobal(context);
+double dx = offset.dx  
+double dx = offset.dy
+
+```
 
-For help on editing plugin code, view the [documentation](https://flutter.io/developing-packages/#edit-plugin-package).

+ 9 - 26
example/lib/main.dart

@@ -1,8 +1,6 @@
-import 'package:flutter/material.dart';
-import 'dart:async';
-
-import 'package:flutter/services.dart';
+import 'package:common_utils/common_utils.dart';
 import 'package:flustars/flustars.dart';
+import 'package:flutter/material.dart';
 
 void main() => runApp(new MyApp());
 
@@ -12,32 +10,17 @@ class MyApp extends StatefulWidget {
 }
 
 class _MyAppState extends State<MyApp> {
-  String _platformVersion = 'Unknown';
-
   @override
   void initState() {
     super.initState();
-    initPlatformState();
+    test();
   }
 
-  // Platform messages are asynchronous, so we initialize in an async method.
-  Future<void> initPlatformState() async {
-    String platformVersion;
-    // Platform messages may fail, so we use a try/catch PlatformException.
-    try {
-      platformVersion = await Flustars.platformVersion;
-    } on PlatformException {
-      platformVersion = 'Failed to get platform version.';
-    }
-
-    // If the widget was removed from the tree while the asynchronous platform
-    // message was in flight, we want to discard the reply rather than calling
-    // setState to update our non-existent appearance.
-    if (!mounted) return;
-
-    setState(() {
-      _platformVersion = platformVersion;
-    });
+  void test() async {
+    SpUtil spUtil = await SpUtil.getInstance();
+    //SpUtil.remove("username");
+    SpUtil.putString("username", "sky224");
+    LogUtil.e("username: " + SpUtil.getString("username").toString());
   }
 
   @override
@@ -48,7 +31,7 @@ class _MyAppState extends State<MyApp> {
           title: const Text('Plugin example app'),
         ),
         body: new Center(
-          child: new Text('Running on: $_platformVersion\n'),
+          child: new Text('Running on: '),
         ),
       ),
     );

+ 3 - 3
lib/flustars.dart

@@ -1,3 +1,3 @@
-export 'src/net/net.dart';
-export 'src/db/db.dart';
-export 'package:flustars/src/sp_util.dart';
+export 'src/screen_util.dart';
+export 'src/widget_util.dart';
+export 'src/sp_util.dart';

+ 0 - 13
lib/src/db/db.dart

@@ -1,13 +0,0 @@
-import 'dart:async';
-
-import 'package:flutter/services.dart';
-
-class Flustars {
-  static const MethodChannel _channel =
-  const MethodChannel('flustars');
-
-  static Future<String> get platformVersion async {
-    final String version = await _channel.invokeMethod('getPlatformVersion');
-    return version;
-  }
-}

+ 0 - 5
lib/src/net/net.dart

@@ -1,5 +0,0 @@
-class Calculator {
-  int addOne(int value) {
-    return value + 2;
-  }
-}

+ 66 - 0
lib/src/screen_util.dart

@@ -0,0 +1,66 @@
+import 'package:flutter/material.dart';
+
+/**
+ * @Author: thl
+ * @GitHub: https://github.com/Sky24n
+ * @Description: Screen Util.
+ * @Date: 2018/9/8
+ */
+
+///
+class ScreenUtil {
+  static double _screenWidth;
+  static double _screenHeight;
+  static double _screenDensity;
+  static double _statusBarHeight;
+  static double _appBarHeight;
+  static MediaQueryData _mediaQueryData;
+
+  static ScreenUtil singleton = new ScreenUtil();
+
+  static ScreenUtil getInstance() {
+    return singleton;
+  }
+
+  void init(BuildContext context) {
+    MediaQueryData mediaQuery = MediaQuery.of(context);
+    _mediaQueryData = mediaQuery;
+    _screenWidth = mediaQuery.size.width;
+    _screenHeight = mediaQuery.size.height;
+    _screenDensity = mediaQuery.devicePixelRatio;
+    _statusBarHeight = mediaQuery.padding.top;
+    _appBarHeight = kToolbarHeight;
+  }
+
+  ///screen width
+  static double get screenWidth => _screenWidth;
+
+  ///screen height
+  static double get screenHeight => _screenHeight;
+
+  ///appBar height
+  static double get appBarHeight => _appBarHeight;
+
+  ///screen density
+  static double get screenDensity => _screenDensity;
+
+  ///status bar Height
+  static double get statusBarHeight => _statusBarHeight;
+
+  static MediaQueryData get mediaQueryData => _mediaQueryData;
+
+  static double getScreenWidth(BuildContext context) {
+    MediaQueryData mediaQuery = MediaQuery.of(context);
+    return mediaQuery.size.width;
+  }
+
+  static double getScreenHeight(BuildContext context) {
+    MediaQueryData mediaQuery = MediaQuery.of(context);
+    return mediaQuery.size.height;
+  }
+
+  static Orientation getOrientation(BuildContext context) {
+    MediaQueryData mediaQuery = MediaQuery.of(context);
+    return mediaQuery.orientation;
+  }
+}

+ 80 - 4
lib/src/sp_util.dart

@@ -1,12 +1,88 @@
+import 'dart:async';
+
 import 'package:shared_preferences/shared_preferences.dart';
+import 'package:synchronized/synchronized.dart';
+
+/**
+ * @Author: thl
+ * @GitHub: https://github.com/Sky24n
+ * @Description: Sp Util.
+ * @Date: 2018/9/8
+ */
 
+///
 class SpUtil {
-  static SpUtil singleton = new SpUtil();
+  static SpUtil _singleton;
   static SharedPreferences _sharedPreferences;
+  static Lock _lock = Lock();
+
+  static Future<SpUtil> getInstance() async {
+    if (_singleton == null) {
+      await _lock.synchronized(() async {
+        _singleton = new SpUtil();
+        await init();
+      });
+    }
+    return _singleton;
+  }
+
+  static void init() async {
+    _sharedPreferences = await SharedPreferences.getInstance();
+  }
+
+  static String getString(String key) {
+    return _sharedPreferences.getString(key);
+  }
+
+  static Future<bool> putString(String key, String value) {
+    return _sharedPreferences.setString(key, value);
+  }
+
+  static bool getBool(String key) {
+    return _sharedPreferences.getBool(key);
+  }
+
+  static Future<bool> putBool(String key, bool value) {
+    return _sharedPreferences.setBool(key, value);
+  }
+
+  static int getInt(String key) {
+    return _sharedPreferences.getInt(key);
+  }
 
-  static SpUtil getInstance() {
-    return singleton;
+  static Future<bool> putInt(String key, int value) {
+    return _sharedPreferences.setInt(key, value);
   }
 
-  void init() {}
+  static double getDouble(String key) {
+    return _sharedPreferences.getDouble(key);
+  }
+
+  static Future<bool> putDouble(String key, double value) {
+    return _sharedPreferences.setDouble(key, value);
+  }
+
+  static List<String> getStringList(String key) {
+    return _sharedPreferences.getStringList(key);
+  }
+
+  static Future<bool> putStringList(String key, List<String> value) {
+    return _sharedPreferences.setStringList(key, value);
+  }
+
+  static dynamic getDynamic(String key) {
+    return _sharedPreferences.get(key);
+  }
+
+  static Set<String> getKeys() {
+    return _sharedPreferences.getKeys();
+  }
+
+  static Future<bool> remove(String key) {
+    return _sharedPreferences.remove(key);
+  }
+
+  static Future<bool> clear() {
+    return _sharedPreferences.clear();
+  }
 }

+ 54 - 0
lib/src/widget_util.dart

@@ -0,0 +1,54 @@
+import 'package:flutter/widgets.dart';
+
+/**
+ * @Author: thl
+ * @GitHub: https://github.com/Sky24n
+ * @Description: Widget Util.
+ * @Date: 2018/9/10
+ */
+
+///
+class WidgetUtil {
+  bool _hasMeasured = false;
+  double _width;
+  double _height;
+
+  /// Widget rendering listener.
+  /// Widget渲染监听
+  /// context: Widget context
+  /// isOnce: true,Continuous monitoring  false,Listen only once.
+  /// onCallBack: Widget Rect CallBack
+  void asyncPrepare(
+      BuildContext context, bool isOnce, ValueChanged<Rect> onCallBack) {
+    if (_hasMeasured) return;
+    WidgetsBinding.instance.addPostFrameCallback((Duration timeStamp) {
+      RenderBox box = context.findRenderObject();
+      if (box != null && box.semanticBounds != null) {
+        if (isOnce) _hasMeasured = true;
+        double width = box.semanticBounds.width;
+        double height = box.semanticBounds.height;
+        if (_width != width || _height != height) {
+          _width = width;
+          _height = height;
+          if (onCallBack != null) onCallBack(box.semanticBounds);
+        }
+      }
+    });
+  }
+
+  ///get Widget Bounds (width, height, left, top, right, bottom and so on).Widgets must be rendered completely.
+  ///获取widget Rect
+  static Rect getWidgetBounds(BuildContext context) {
+    RenderBox box = context.findRenderObject();
+    return (box != null && box.semanticBounds != null)
+        ? box.semanticBounds
+        : Rect.zero;
+  }
+
+  ///Get the coordinates of the widget on the screen.Widgets must be rendered completely.
+  ///获取widget在屏幕上的坐标,widget必须渲染完成
+  static Offset getWidgetLocalToGlobal(BuildContext context) {
+    RenderBox box = context.findRenderObject();
+    return box == null ? Offset.zero : box.localToGlobal(Offset.zero);
+  }
+}

+ 5 - 4
pubspec.yaml

@@ -1,6 +1,6 @@
 name: flustars
-description: flustars library.
-version: 0.0.5
+description: Flutter common utils library. SpUtil, ScreenUtil, WidgetUtil.
+version: 0.0.6
 author: thl <863764940@qq.com>
 homepage: https://github.com/Sky24n/flustars
 
@@ -11,9 +11,10 @@ dependencies:
   flutter:
     sdk: flutter
 
+  common_utils: ^1.0.4
+  synchronized: ^1.5.3
   shared_preferences: ^0.4.2
-  dio: ^1.0.6
-  common_utils: ^1.0.3
+
 
 # For information on the generic Dart part of this file, see the
 # following page: https://www.dartlang.org/tools/pub/pubspec