main.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import 'package:flustars/flustars.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. void main() => runApp(new MyApp());
  5. class MyApp extends StatefulWidget {
  6. @override
  7. _MyAppState createState() => new _MyAppState();
  8. }
  9. class _MyAppState extends State<MyApp> {
  10. String _userName = '';
  11. @override
  12. void initState() {
  13. super.initState();
  14. test();
  15. }
  16. void test() async {
  17. print("SpUtil: " + SpUtil.isInitialized().toString());
  18. SpUtil spUtil = await SpUtil.getInstance();
  19. //SpUtil.remove("username");
  20. print("SpUtil: " + SpUtil.isInitialized().toString());
  21. SpUtil.putString("username", "sky24");
  22. print("username: " + SpUtil.getString("username").toString());
  23. if (!mounted) return;
  24. setState(() {
  25. _userName = SpUtil.getString("username");
  26. });
  27. }
  28. @override
  29. Widget build(BuildContext context) {
  30. return new MaterialApp(
  31. home: new MainPage(),
  32. );
  33. // return new MaterialApp(
  34. // home: new Scaffold(
  35. // appBar: new AppBar(
  36. // title: const Text('Plugin example app'),
  37. // ),
  38. // body: new Center(
  39. // child: new Text('username: $_userName'),
  40. // ),
  41. // floatingActionButton: new FloatingActionButton(onPressed: () {
  42. // Navigator.push(
  43. // context, new CupertinoPageRoute(builder: (ctx) => TestPage()));
  44. // }),
  45. // ),
  46. // );
  47. }
  48. }
  49. class MainPage extends StatefulWidget {
  50. @override
  51. State<StatefulWidget> createState() {
  52. return new MainPageState();
  53. }
  54. }
  55. class MainPageState extends State<MainPage> {
  56. WidgetUtil widgetUtil = new WidgetUtil();
  57. @override
  58. void initState() {
  59. super.initState();
  60. widgetUtil.asyncPrepares(true, (_) {
  61. print("Widget 渲染完成...");
  62. });
  63. }
  64. @override
  65. Widget build(BuildContext context) {
  66. // 如果使用ScreenUtil.getInstance()
  67. // 需要MainPageState build 调用MediaQuery.of(context)
  68. MediaQuery.of(context);
  69. double width = ScreenUtil.getInstance().screenWidth;
  70. double height = ScreenUtil.getInstance().screenHeight;
  71. double density = ScreenUtil.getInstance().screenDensity;
  72. double tempW = ScreenUtil.getInstance().getWidth(360.0);
  73. double tempH = ScreenUtil.getInstance().getHeight(360.0);
  74. double textScaleFactor =
  75. ScreenUtil.getInstance().mediaQueryData.textScaleFactor;
  76. print(
  77. "width: $width, height: $height, density: $density, tempW: $tempW, tempH: $tempH, textScaleFactor: $textScaleFactor");
  78. double _width = width * density;
  79. double _height = height * density;
  80. double __tempW = ScreenUtil.getInstance().getWidthPx(90.0);
  81. print(
  82. "_width: $_width, height: $_height, __tempW: $__tempW, tempW: $tempW, tempH: $tempH");
  83. return new Scaffold(
  84. appBar: new AppBar(),
  85. body: new Column(
  86. crossAxisAlignment: CrossAxisAlignment.start,
  87. children: <Widget>[
  88. new Container(
  89. width: 360.0,
  90. height: 50,
  91. color: Colors.grey,
  92. child: new Center(
  93. child: new Text(
  94. "你好你好你好",
  95. style: new TextStyle(fontSize: 24.0),
  96. ),
  97. ),
  98. ),
  99. new Container(
  100. width: ScreenUtil.getInstance().getWidth(360.0),
  101. height: 50,
  102. color: Colors.grey,
  103. child: new Center(
  104. child: new Text(
  105. "你好你好你好",
  106. style: new TextStyle(fontSize: 24.0),
  107. ),
  108. ),
  109. ),
  110. new Container(
  111. width: 100,
  112. height: 100,
  113. color: Colors.grey,
  114. child: new Center(
  115. child: new Text(
  116. "你好你好你好",
  117. style: new TextStyle(fontSize: 24.0),
  118. ),
  119. ),
  120. ),
  121. new Container(
  122. margin: EdgeInsets.only(top: 10.0),
  123. width: ScreenUtil.getInstance().getWidth(100.0),
  124. height: ScreenUtil.getInstance().getHeight(100.0),
  125. color: Colors.grey,
  126. child: new Center(
  127. child: new Text(
  128. "你好你好你好",
  129. style: new TextStyle(fontSize: 24.0),
  130. ),
  131. ),
  132. ),
  133. new Container(
  134. margin: EdgeInsets.only(top: 10.0),
  135. width: ScreenUtil.getInstance().getWidth(100.0),
  136. height: ScreenUtil.getInstance().getHeight(100.0),
  137. color: Colors.grey,
  138. child: new Center(
  139. child: new Text(
  140. "你好你好你好",
  141. style: new TextStyle(
  142. fontSize: ScreenUtil.getInstance().getSp(24.0)),
  143. ),
  144. ),
  145. ),
  146. ],
  147. ),
  148. );
  149. }
  150. }
  151. class TestPage extends StatefulWidget {
  152. @override
  153. State<StatefulWidget> createState() {
  154. return new TestPageState();
  155. }
  156. }
  157. class TestPageState extends State<TestPage> {
  158. @override
  159. Widget build(BuildContext context) {
  160. double width = ScreenUtil.getInstance().screenWidth;
  161. double height = ScreenUtil.getInstance().screenHeight;
  162. print("width: $width, height: $height");
  163. return new Scaffold(
  164. body: new AppBar(),
  165. );
  166. }
  167. }