thl 6 سال پیش
والد
کامیت
5d4a0af42d
2فایلهای تغییر یافته به همراه190 افزوده شده و 66 حذف شده
  1. 47 4
      README.md
  2. 143 62
      example/lib/main.dart

+ 47 - 4
README.md

@@ -3,7 +3,7 @@
 
 ## [flustars] Flutter常用工具类库。主要对第三方库封装,以便于使用。如果你有好的工具类欢迎PR. 
 
-## [更新说明](../docs/UPDATELOG.md)
+## [更新说明](./docs/UPDATELOG.md)
  
 ### 关于使用本开源库
 如果您是用于公司项目,请随意使用~  
@@ -31,7 +31,7 @@ dependencies:
  8、ObjectUtil   : 判断对象是否为空(String List Map),判断两个List是否相等. 
 
 ### APIs
-* #### SpUtil -> [Example]()
+* #### SpUtil -> [Example](https://github.com/Sky24n/flutter_wanandroid/blob/master/lib/ui/pages/demos/sp_util_page.dart)
 ```
 getString
 putString
@@ -50,7 +50,7 @@ clear
 isInitialized
 ```
 
-* #### ScreenUtil -> [Example]() 
+* #### ScreenUtil -> [Example](./example/lib/main.dart) 
 ```
 getWidth                  : 返回根据屏幕宽适配后尺寸.
 getHeight                 : 返回根据屏幕高适配后尺寸.
@@ -69,9 +69,52 @@ getBottomBarH(ctx)        : 当前BottomBar高度.
 getScaleW(ctx,size)       : 返回根据屏幕宽适配后尺寸.
 getScaleH(ctx,size)       : 返回根据屏幕高适配后尺寸.
 getScaleSp(ctx,size)      : 返回根据屏幕宽适配后字体尺寸.
+
+// 屏幕宽
+double screenWidth = ScreenUtil.getInstance().screenWidth;
+// 屏幕高
+double screenHeight = ScreenUtil.getInstance().screenHeight;
+// 屏幕像素密度
+double screenDensity = ScreenUtil.getInstance().screenDensity;
+// 系统状态栏高度
+double statusBarHeight = ScreenUtil.getInstance().statusBarHeight;
+// BottomBar高度 
+double bottomBarHeight = ScreenUtil.getInstance().bottomBarHeight;
+// 系统AppBar高度
+double appBarHeight = ScreenUtil.getInstance().appBarHeight;
+// 根据屏幕宽适配后尺寸
+double adapterW100 = ScreenUtil.getInstance().getWidth(100);
+// 根据屏幕高适配后尺寸
+double adapterH100 = ScreenUtil.getInstance().getHeight(100);
+// 根据屏幕宽适配后字体尺寸
+double adapterSp100 = ScreenUtil.getInstance().getSp(100);
+// 根据屏幕宽适配后尺寸(输入px)
+double adapterW100px = ScreenUtil.getInstance().getWidthPx(300);
+// 根据屏幕高适配后尺寸(输入px)
+double adapterH100px = ScreenUtil.getInstance().getHeightPx(300);
+
+// 屏幕宽
+double screenWidth = ScreenUtil.getScreenW(context);
+// 屏幕高
+double screenHeight = ScreenUtil.getScreenH(context);
+// 屏幕像素密度
+double screenDensity = ScreenUtil.getScreenDensity(context);
+// 系统状态栏高度
+double statusBarHeight = ScreenUtil.getStatusBarH(context);
+// BottomBar高度
+double bottomBarHeight = ScreenUtil.getBottomBarH(context);
+// 根据屏幕宽适配后尺寸
+double adapterW100 = ScreenUtil.getScaleW(context, 100);
+// 根据屏幕高适配后尺寸
+double adapterH100 = ScreenUtil.getScaleH(context, 100);
+// 根据屏幕宽适配后字体尺寸
+double adapterSp100 = ScreenUtil.getScaleSp(context, 100);
+// 屏幕方向
+Orientation orientation = ScreenUtil.getOrientation(context);
+
 ```
 
-* #### WidgetUtil -> [Example]()
+* #### WidgetUtil -> [Example](https://github.com/Sky24n/flutter_wanandroid/blob/master/lib/ui/pages/demos/widget_util_page.dart)
 ```
 asyncPrepare              : Widget渲染监听,监听widget宽高变化,callback返回宽高等参数.
 getWidgetBounds           : 获取widget 宽高.

+ 143 - 62
example/lib/main.dart

@@ -16,21 +16,9 @@ class _MyAppState extends State<MyApp> {
 
     /// 配置设计稿尺寸
     /// 如果设计稿尺寸默认配置一致,无需该设置。默认 width:360.0 / height:640.0 / density:3.0
+    /// Configuration design draft size.
+    /// If the default configuration of design draft size is the same, this setting is not required. default width:360.0 / height:640.0 / density:3.0
     setDesignWHD(360.0, 640, density: 3);
-
-    _initAsync();
-  }
-
-  void _initAsync() async {
-    print("SpUtil: " + SpUtil.isInitialized().toString());
-    await SpUtil.getInstance();
-    print("SpUtil: " + SpUtil.isInitialized().toString());
-    SpUtil.putString("username", "sky24");
-    print("username: " + SpUtil.getString("username").toString());
-    if (!mounted) return;
-    setState(() {
-      String _userName = SpUtil.getString("username");
-    });
   }
 
   @override
@@ -48,33 +36,21 @@ class MainPage extends StatefulWidget {
   }
 }
 
+/// 在MainPage使用依赖不context方法获取屏幕参数及适配,需要build方法内调用[MediaQuery.of(context)]。
+/// 或者使用依赖context方法获取屏幕参数及适配。
+/// In MainPage, the dependency-free context method is used to obtain screen parameters and adaptions, which requires a call to [MediaQuery. of (context)] within the build method.
+/// Or use context-dependent methods to obtain screen parameters and adaptions.
 class MainPageState extends State<MainPage> {
-  @override
-  void initState() {
-    super.initState();
-  }
-
   @override
   Widget build(BuildContext context) {
-    // 如果使用ScreenUtil.getInstance()
-    // 需要MainPageState build 调用MediaQuery.of(context)
-//    MediaQuery.of(context);
+    /// 如果使用依赖不context方法获取屏幕参数及适配,需要调用此方法。
+    /// If you use a dependent context-free method to obtain screen parameters and adaptions, you need to call this method.
+    MediaQuery.of(context);
 
+    double statusBar = ScreenUtil.getInstance().statusBarHeight;
     double width = ScreenUtil.getInstance().screenWidth;
     double height = ScreenUtil.getInstance().screenHeight;
-    double density = ScreenUtil.getInstance().screenDensity;
-    double tempW = ScreenUtil.getInstance().getWidth(360.0);
-    double tempH = ScreenUtil.getInstance().getHeight(360.0);
-    double textScaleFactor =
-        ScreenUtil.getInstance().mediaQueryData.textScaleFactor;
-
-    print(
-        "width: $width, height: $height, density: $density, tempW: $tempW, tempH: $tempH, textScaleFactor: $textScaleFactor");
-    double _width = width * density;
-    double _height = height * density;
-    double __tempW = ScreenUtil.getInstance().getWidthPx(90.0);
-    print(
-        "_width: $_width, height: $_height, __tempW: $__tempW, tempW: $tempW, tempH: $tempH");
+    print("MainPage statusBar: $statusBar, width: $width, height: $height");
 
     return new Scaffold(
       // 一个不需要GlobalKey就可以openDrawer的AppBar
@@ -87,7 +63,12 @@ class MainPageState extends State<MainPage> {
         actions: <Widget>[
           new IconButton(
             icon: new Icon(Icons.search),
-            onPressed: () {},
+            onPressed: () {
+              Navigator.push(
+                  context,
+                  new CupertinoPageRoute<void>(
+                      builder: (ctx) => new SecondPage()));
+            },
           ),
         ],
       ),
@@ -100,7 +81,7 @@ class MainPageState extends State<MainPage> {
             color: Colors.grey,
             child: new Center(
               child: new Text(
-                "你好你好你好",
+                "未适配宽",
                 style: new TextStyle(fontSize: 24.0),
               ),
             ),
@@ -111,7 +92,7 @@ class MainPageState extends State<MainPage> {
             color: Colors.grey,
             child: new Center(
               child: new Text(
-                "你好你好你好",
+                "已适配宽",
                 style: new TextStyle(fontSize: 24.0),
               ),
             ),
@@ -130,59 +111,159 @@ class MainPageState extends State<MainPage> {
           new Container(
             margin: EdgeInsets.only(top: 10.0),
             width: ScreenUtil.getInstance().getWidth(100.0),
-            height: ScreenUtil.getInstance().getHeight(100.0),
+            height: ScreenUtil.getInstance().getWidth(100.0),
             color: Colors.grey,
             child: new Center(
               child: new Text(
                 "你好你好你好",
-                style: new TextStyle(fontSize: 24.0),
+                style: new TextStyle(
+                    fontSize: ScreenUtil.getInstance().getSp(24.0)),
               ),
             ),
           ),
+        ],
+      ),
+      drawer: new MyDrawer(),
+    );
+  }
+}
+
+class MyDrawer extends StatelessWidget {
+  @override
+  Widget build(BuildContext context) {
+    double statusBar = ScreenUtil.getInstance().statusBarHeight;
+    double width = ScreenUtil.getInstance().screenWidth;
+    double height = ScreenUtil.getInstance().screenHeight;
+    print("SecondPage statusBar: $statusBar, width: $width, height: $height");
+
+    return new Container(
+      color: Colors.white,
+      width: ScreenUtil.getInstance().getWidth(240),
+      child: new ListView(
+        padding: EdgeInsets.zero,
+        children: <Widget>[
           new Container(
-            margin: EdgeInsets.only(top: 10.0),
-            width: ScreenUtil.getInstance().getWidth(100.0),
-            height: ScreenUtil.getInstance().getHeight(100.0),
-            color: Colors.grey,
+            color: Colors.teal,
+            padding:
+                EdgeInsets.only(top: ScreenUtil.getInstance().statusBarHeight),
             child: new Center(
               child: new Text(
-                "你好你好你好",
-                style: new TextStyle(
-                    fontSize: ScreenUtil.getInstance().getSp(24.0)),
+                "Sky24n",
+                style: new TextStyle(fontSize: 16, color: Colors.white),
               ),
             ),
-          ),
+            height: 160,
+          )
         ],
       ),
-      drawer: new Container(
-        color: Colors.white,
-        width: ScreenUtil.getInstance().getWidth(100),
-        height: double.infinity,
-        child: new SizedBox(
-          width: ScreenUtil.getInstance().getWidth(100),
-        ),
-      ),
     );
   }
 }
 
-class TestPage extends StatefulWidget {
+class SecondPage extends StatefulWidget {
   @override
   State<StatefulWidget> createState() {
-    return new TestPageState();
+    return new SecondPageState();
   }
 }
 
-class TestPageState extends State<TestPage> {
+class SecondPageState extends State<SecondPage> {
+  @override
+  void initState() {
+    super.initState();
+    _init();
+    _initWithCtx();
+  }
+
+  void _init() {
+    double screenWidth = ScreenUtil.getInstance().screenWidth;
+    double screenHeight = ScreenUtil.getInstance().screenHeight;
+    double screenDensity = ScreenUtil.getInstance().screenDensity;
+    double statusBarHeight = ScreenUtil.getInstance().statusBarHeight;
+    double bottomBarHeight = ScreenUtil.getInstance().bottomBarHeight;
+    double appBarHeight = ScreenUtil.getInstance().appBarHeight;
+    double adapterW100 = ScreenUtil.getInstance().getWidth(100);
+    double adapterH100 = ScreenUtil.getInstance().getHeight(100);
+    double adapterSp100 = ScreenUtil.getInstance().getSp(100);
+    double adapterW100px = ScreenUtil.getInstance().getWidthPx(300);
+    double adapterH100px = ScreenUtil.getInstance().getHeightPx(300);
+
+    print("SecondPage _init screenWidth: $screenWidth, screenHeight: $screenHeight, screenDensity: $screenDensity" +
+        ", statusBarHeight: $statusBarHeight, bottomBarHeight: $bottomBarHeight, appBarHeight: $appBarHeight" +
+        ", adapterW100: $adapterW100, adapterH100: $adapterH100, adapterSp100: $adapterSp100" +
+        ", adapterW100px: $adapterW100px, adapterH100px: $adapterH100px");
+  }
+
+  void _initWithCtx() {
+    double screenWidth = ScreenUtil.getScreenW(context);
+    double screenHeight = ScreenUtil.getScreenH(context);
+    double screenDensity = ScreenUtil.getScreenDensity(context);
+    double statusBarHeight = ScreenUtil.getStatusBarH(context);
+    double bottomBarHeight = ScreenUtil.getBottomBarH(context);
+    double adapterW100 = ScreenUtil.getScaleW(context, 100);
+    double adapterH100 = ScreenUtil.getScaleH(context, 100);
+    double adapterSp100 = ScreenUtil.getScaleSp(context, 100);
+    Orientation orientation = ScreenUtil.getOrientation(context);
+
+    print("SecondPage _initWithCtx screenWidth: $screenWidth, screenHeight: $screenHeight, screenDensity: $screenDensity" +
+        ", statusBarHeight: $statusBarHeight, bottomBarHeight: $bottomBarHeight" +
+        ", adapterW100: $adapterW100, adapterH100: $adapterH100, adapterSp100: $adapterSp100");
+  }
+
   @override
   Widget build(BuildContext context) {
+    double statusBar = ScreenUtil.getInstance().statusBarHeight;
     double width = ScreenUtil.getInstance().screenWidth;
     double height = ScreenUtil.getInstance().screenHeight;
-
-    print("width: $width, height: $height");
+    print("SecondPage statusBar: $statusBar, width: $width, height: $height");
 
     return new Scaffold(
-      body: new AppBar(),
+      appBar: new AppBar(
+        title: new Text("Second Page"),
+        centerTitle: true,
+      ),
+      body: new Column(
+        crossAxisAlignment: CrossAxisAlignment.start,
+        children: <Widget>[
+          new Container(
+            width: 100,
+            height: 100,
+            color: Colors.grey,
+            child: new Center(
+              child: new Text(
+                "你好你好你好",
+                style: new TextStyle(fontSize: 24.0),
+              ),
+            ),
+          ),
+          new Container(
+            margin: EdgeInsets.only(top: 10.0),
+            width: ScreenUtil.getInstance().getWidth(100.0),
+            height: ScreenUtil.getInstance().getWidth(100.0),
+            color: Colors.grey,
+            child: new Center(
+              child: new Text(
+                "你好你好你好",
+                style: new TextStyle(
+                    fontSize: ScreenUtil.getInstance().getSp(24.0)),
+              ),
+            ),
+          ),
+          new Container(
+            margin: EdgeInsets.only(top: 10.0),
+            width: ScreenUtil.getScaleW(context, 100.0),
+            height: ScreenUtil.getScaleW(context, 100.0),
+            color: Colors.grey,
+            child: new Center(
+              child: new Text(
+                "你好你好你好",
+                style: new TextStyle(
+                    fontSize: ScreenUtil.getScaleSp(context, 24.0)),
+              ),
+            ),
+          ),
+        ],
+      ),
     );
   }
 }