main.dart 5.6 KB

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