main.dart 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. /// Configuration design draft size.
  16. /// 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
  17. setDesignWHD(360.0, 640, density: 3);
  18. }
  19. @override
  20. Widget build(BuildContext context) {
  21. return new MaterialApp(
  22. home: new MainPage(),
  23. );
  24. }
  25. }
  26. class MainPage extends StatefulWidget {
  27. @override
  28. State<StatefulWidget> createState() {
  29. return new MainPageState();
  30. }
  31. }
  32. /// 在MainPage使用依赖不context方法获取屏幕参数及适配,需要build方法内调用[MediaQuery.of(context)]。
  33. /// 或者使用依赖context方法获取屏幕参数及适配。
  34. /// 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.
  35. /// Or use context-dependent methods to obtain screen parameters and adaptions.
  36. class MainPageState extends State<MainPage> {
  37. @override
  38. Widget build(BuildContext context) {
  39. /// 如果使用依赖不context方法获取屏幕参数及适配,需要调用此方法。
  40. /// If you use a dependent context-free method to obtain screen parameters and adaptions, you need to call this method.
  41. MediaQuery.of(context);
  42. double statusBar = ScreenUtil.getInstance().statusBarHeight;
  43. double width = ScreenUtil.getInstance().screenWidth;
  44. double height = ScreenUtil.getInstance().screenHeight;
  45. print("MainPage statusBar: $statusBar, width: $width, height: $height");
  46. return new Scaffold(
  47. // 一个不需要GlobalKey就可以openDrawer的AppBar
  48. appBar: new MyAppBar(
  49. leading: ClipOval(
  50. child: new Image.asset(('assets/images/ali_connors.png')),
  51. ),
  52. title: const Text('Flustars Demos'),
  53. centerTitle: true,
  54. actions: <Widget>[
  55. new IconButton(
  56. icon: new Icon(Icons.search),
  57. onPressed: () {
  58. Navigator.push(
  59. context,
  60. new CupertinoPageRoute<void>(
  61. builder: (ctx) => new SecondPage()));
  62. },
  63. ),
  64. ],
  65. ),
  66. body: new Column(
  67. crossAxisAlignment: CrossAxisAlignment.start,
  68. children: <Widget>[
  69. new Container(
  70. width: 360.0,
  71. height: 50,
  72. color: Colors.grey,
  73. child: new Center(
  74. child: new Text(
  75. "未适配宽",
  76. style: new TextStyle(fontSize: 24.0),
  77. ),
  78. ),
  79. ),
  80. new Container(
  81. width: ScreenUtil.getInstance().getWidth(360.0),
  82. height: 50,
  83. color: Colors.grey,
  84. child: new Center(
  85. child: new Text(
  86. "已适配宽",
  87. style: new TextStyle(fontSize: 24.0),
  88. ),
  89. ),
  90. ),
  91. new Container(
  92. width: 100,
  93. height: 100,
  94. color: Colors.grey,
  95. child: new Center(
  96. child: new Text(
  97. "你好你好你好",
  98. style: new TextStyle(fontSize: 24.0),
  99. ),
  100. ),
  101. ),
  102. new Container(
  103. margin: EdgeInsets.only(top: 10.0),
  104. width: ScreenUtil.getInstance().getWidth(100.0),
  105. height: ScreenUtil.getInstance().getWidth(100.0),
  106. color: Colors.grey,
  107. child: new Center(
  108. child: new Text(
  109. "你好你好你好",
  110. style: new TextStyle(
  111. fontSize: ScreenUtil.getInstance().getSp(24.0)),
  112. ),
  113. ),
  114. ),
  115. ],
  116. ),
  117. drawer: new MyDrawer(),
  118. );
  119. }
  120. }
  121. class MyDrawer extends StatelessWidget {
  122. @override
  123. Widget build(BuildContext context) {
  124. double statusBar = ScreenUtil.getInstance().statusBarHeight;
  125. double width = ScreenUtil.getInstance().screenWidth;
  126. double height = ScreenUtil.getInstance().screenHeight;
  127. print("SecondPage statusBar: $statusBar, width: $width, height: $height");
  128. return new Container(
  129. color: Colors.white,
  130. width: ScreenUtil.getInstance().getWidth(240),
  131. child: new ListView(
  132. padding: EdgeInsets.zero,
  133. children: <Widget>[
  134. new Container(
  135. color: Colors.teal,
  136. padding:
  137. EdgeInsets.only(top: ScreenUtil.getInstance().statusBarHeight),
  138. child: new Center(
  139. child: new Text(
  140. "Sky24n",
  141. style: new TextStyle(fontSize: 16, color: Colors.white),
  142. ),
  143. ),
  144. height: 160,
  145. )
  146. ],
  147. ),
  148. );
  149. }
  150. }
  151. class SecondPage extends StatefulWidget {
  152. @override
  153. State<StatefulWidget> createState() {
  154. return new SecondPageState();
  155. }
  156. }
  157. class SecondPageState extends State<SecondPage> {
  158. @override
  159. void initState() {
  160. super.initState();
  161. _init();
  162. _initWithCtx();
  163. }
  164. void _init() {
  165. double screenWidth = ScreenUtil.getInstance().screenWidth;
  166. double screenHeight = ScreenUtil.getInstance().screenHeight;
  167. double screenDensity = ScreenUtil.getInstance().screenDensity;
  168. double statusBarHeight = ScreenUtil.getInstance().statusBarHeight;
  169. double bottomBarHeight = ScreenUtil.getInstance().bottomBarHeight;
  170. double appBarHeight = ScreenUtil.getInstance().appBarHeight;
  171. double adapterW100 = ScreenUtil.getInstance().getWidth(100);
  172. double adapterH100 = ScreenUtil.getInstance().getHeight(100);
  173. double adapterSp100 = ScreenUtil.getInstance().getSp(100);
  174. double adapterW100px = ScreenUtil.getInstance().getWidthPx(300);
  175. double adapterH100px = ScreenUtil.getInstance().getHeightPx(300);
  176. print("SecondPage _init screenWidth: $screenWidth, screenHeight: $screenHeight, screenDensity: $screenDensity" +
  177. ", statusBarHeight: $statusBarHeight, bottomBarHeight: $bottomBarHeight, appBarHeight: $appBarHeight" +
  178. ", adapterW100: $adapterW100, adapterH100: $adapterH100, adapterSp100: $adapterSp100" +
  179. ", adapterW100px: $adapterW100px, adapterH100px: $adapterH100px");
  180. }
  181. void _initWithCtx() {
  182. double screenWidth = ScreenUtil.getScreenW(context);
  183. double screenHeight = ScreenUtil.getScreenH(context);
  184. double screenDensity = ScreenUtil.getScreenDensity(context);
  185. double statusBarHeight = ScreenUtil.getStatusBarH(context);
  186. double bottomBarHeight = ScreenUtil.getBottomBarH(context);
  187. double adapterW100 = ScreenUtil.getScaleW(context, 100);
  188. double adapterH100 = ScreenUtil.getScaleH(context, 100);
  189. double adapterSp100 = ScreenUtil.getScaleSp(context, 100);
  190. Orientation orientation = ScreenUtil.getOrientation(context);
  191. print("SecondPage _initWithCtx screenWidth: $screenWidth, screenHeight: $screenHeight, screenDensity: $screenDensity" +
  192. ", statusBarHeight: $statusBarHeight, bottomBarHeight: $bottomBarHeight" +
  193. ", adapterW100: $adapterW100, adapterH100: $adapterH100, adapterSp100: $adapterSp100");
  194. }
  195. @override
  196. Widget build(BuildContext context) {
  197. double statusBar = ScreenUtil.getInstance().statusBarHeight;
  198. double width = ScreenUtil.getInstance().screenWidth;
  199. double height = ScreenUtil.getInstance().screenHeight;
  200. print("SecondPage statusBar: $statusBar, width: $width, height: $height");
  201. return new Scaffold(
  202. appBar: new AppBar(
  203. title: new Text("Second Page"),
  204. centerTitle: true,
  205. ),
  206. body: new Column(
  207. crossAxisAlignment: CrossAxisAlignment.start,
  208. children: <Widget>[
  209. new Container(
  210. width: 100,
  211. height: 100,
  212. color: Colors.grey,
  213. child: new Center(
  214. child: new Text(
  215. "你好你好你好",
  216. style: new TextStyle(fontSize: 24.0),
  217. ),
  218. ),
  219. ),
  220. new Container(
  221. margin: EdgeInsets.only(top: 10.0),
  222. width: ScreenUtil.getInstance().getWidth(100.0),
  223. height: ScreenUtil.getInstance().getWidth(100.0),
  224. color: Colors.grey,
  225. child: new Center(
  226. child: new Text(
  227. "你好你好你好",
  228. style: new TextStyle(
  229. fontSize: ScreenUtil.getInstance().getSp(24.0)),
  230. ),
  231. ),
  232. ),
  233. new Container(
  234. margin: EdgeInsets.only(top: 10.0),
  235. width: ScreenUtil.getScaleW(context, 100.0),
  236. height: ScreenUtil.getScaleW(context, 100.0),
  237. color: Colors.grey,
  238. child: new Center(
  239. child: new Text(
  240. "你好你好你好",
  241. style: new TextStyle(
  242. fontSize: ScreenUtil.getScaleSp(context, 24.0)),
  243. ),
  244. ),
  245. ),
  246. ],
  247. ),
  248. );
  249. }
  250. }