main.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. @override
  11. void initState() {
  12. super.initState();
  13. /// 配置设计稿尺寸
  14. /// 如果设计稿尺寸默认配置一致,无需该设置。默认 width:360.0 / height:640.0 / density:3.0
  15. setDesignWHD(360.0, 640, density: 3);
  16. _initAsync();
  17. }
  18. void _initAsync() async {
  19. print("SpUtil: " + SpUtil.isInitialized().toString());
  20. await SpUtil.getInstance();
  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. String _userName = SpUtil.getString("username");
  27. });
  28. }
  29. @override
  30. Widget build(BuildContext context) {
  31. return new MaterialApp(
  32. home: new MainPage(),
  33. );
  34. }
  35. }
  36. class MainPage extends StatefulWidget {
  37. @override
  38. State<StatefulWidget> createState() {
  39. return new MainPageState();
  40. }
  41. }
  42. class MainPageState extends State<MainPage> {
  43. @override
  44. void initState() {
  45. super.initState();
  46. }
  47. @override
  48. Widget build(BuildContext context) {
  49. // 如果使用ScreenUtil.getInstance()
  50. // 需要MainPageState build 调用MediaQuery.of(context)
  51. // MediaQuery.of(context);
  52. double width = ScreenUtil.getInstance().screenWidth;
  53. double height = ScreenUtil.getInstance().screenHeight;
  54. double density = ScreenUtil.getInstance().screenDensity;
  55. double tempW = ScreenUtil.getInstance().getWidth(360.0);
  56. double tempH = ScreenUtil.getInstance().getHeight(360.0);
  57. double textScaleFactor =
  58. ScreenUtil.getInstance().mediaQueryData.textScaleFactor;
  59. print(
  60. "width: $width, height: $height, density: $density, tempW: $tempW, tempH: $tempH, textScaleFactor: $textScaleFactor");
  61. double _width = width * density;
  62. double _height = height * density;
  63. double __tempW = ScreenUtil.getInstance().getWidthPx(90.0);
  64. print(
  65. "_width: $_width, height: $_height, __tempW: $__tempW, tempW: $tempW, tempH: $tempH");
  66. return new Scaffold(
  67. // 一个不需要GlobalKey就可以openDrawer的AppBar
  68. appBar: new MyAppBar(
  69. leading: ClipOval(
  70. child: new Image.asset(('assets/images/ali_connors.png')),
  71. ),
  72. title: const Text('Flustars Demos'),
  73. centerTitle: true,
  74. actions: <Widget>[
  75. new IconButton(
  76. icon: new Icon(Icons.search),
  77. onPressed: () {},
  78. ),
  79. ],
  80. ),
  81. body: new Column(
  82. crossAxisAlignment: CrossAxisAlignment.start,
  83. children: <Widget>[
  84. new Container(
  85. width: 360.0,
  86. height: 50,
  87. color: Colors.grey,
  88. child: new Center(
  89. child: new Text(
  90. "你好你好你好",
  91. style: new TextStyle(fontSize: 24.0),
  92. ),
  93. ),
  94. ),
  95. new Container(
  96. width: ScreenUtil.getInstance().getWidth(360.0),
  97. height: 50,
  98. color: Colors.grey,
  99. child: new Center(
  100. child: new Text(
  101. "你好你好你好",
  102. style: new TextStyle(fontSize: 24.0),
  103. ),
  104. ),
  105. ),
  106. new Container(
  107. width: 100,
  108. height: 100,
  109. color: Colors.grey,
  110. child: new Center(
  111. child: new Text(
  112. "你好你好你好",
  113. style: new TextStyle(fontSize: 24.0),
  114. ),
  115. ),
  116. ),
  117. new Container(
  118. margin: EdgeInsets.only(top: 10.0),
  119. width: ScreenUtil.getInstance().getWidth(100.0),
  120. height: ScreenUtil.getInstance().getHeight(100.0),
  121. color: Colors.grey,
  122. child: new Center(
  123. child: new Text(
  124. "你好你好你好",
  125. style: new TextStyle(fontSize: 24.0),
  126. ),
  127. ),
  128. ),
  129. new Container(
  130. margin: EdgeInsets.only(top: 10.0),
  131. width: ScreenUtil.getInstance().getWidth(100.0),
  132. height: ScreenUtil.getInstance().getHeight(100.0),
  133. color: Colors.grey,
  134. child: new Center(
  135. child: new Text(
  136. "你好你好你好",
  137. style: new TextStyle(
  138. fontSize: ScreenUtil.getInstance().getSp(24.0)),
  139. ),
  140. ),
  141. ),
  142. ],
  143. ),
  144. drawer: new Container(
  145. color: Colors.white,
  146. width: ScreenUtil.getInstance().getWidth(100),
  147. height: double.infinity,
  148. child: new SizedBox(
  149. width: ScreenUtil.getInstance().getWidth(100),
  150. ),
  151. ),
  152. );
  153. }
  154. }
  155. class TestPage extends StatefulWidget {
  156. @override
  157. State<StatefulWidget> createState() {
  158. return new TestPageState();
  159. }
  160. }
  161. class TestPageState extends State<TestPage> {
  162. @override
  163. Widget build(BuildContext context) {
  164. double width = ScreenUtil.getInstance().screenWidth;
  165. double height = ScreenUtil.getInstance().screenHeight;
  166. print("width: $width, height: $height");
  167. return new Scaffold(
  168. body: new AppBar(),
  169. );
  170. }
  171. }