main.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 City {
  10. String name;
  11. City({this.name});
  12. /// must.
  13. City.fromJson(Map<String, dynamic> json) : name = json['name'];
  14. /// must.
  15. Map<String, dynamic> toJson() => {
  16. 'name': name,
  17. };
  18. @override
  19. String toString() {
  20. StringBuffer sb = new StringBuffer('{');
  21. sb.write("\"name\":\"$name\"");
  22. sb.write('}');
  23. return sb.toString();
  24. }
  25. }
  26. class _MyAppState extends State<MyApp> {
  27. @override
  28. void initState() {
  29. super.initState();
  30. _initAsync();
  31. /// 配置设计稿尺寸
  32. /// 如果设计稿尺寸默认配置一致,无需该设置。默认 width:360.0 / height:640.0 / density:3.0
  33. /// Configuration design draft size.
  34. /// 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
  35. setDesignWHD(360.0, 640.0, density: 3);
  36. }
  37. /// SpUtil example.
  38. void _initAsync() async {
  39. await SpUtil.getInstance();
  40. SpUtil.putString("username", "sky24");
  41. String userName = SpUtil.getString("username", defValue: "");
  42. print("thll userName: " + userName);
  43. /// save object example.
  44. /// 存储实体对象示例。
  45. City city = new City();
  46. city.name = "成都市";
  47. SpUtil.putObject("loc_city", city);
  48. City hisCity = SpUtil.getObj("loc_city", (v) => City.fromJson(v));
  49. print("thll City: " + (hisCity == null ? "null" : hisCity.toString()));
  50. /// save object list example.
  51. /// 存储实体对象list示例。
  52. List<City> list = new List();
  53. list.add(new City(name: "成都市"));
  54. list.add(new City(name: "北京市"));
  55. SpUtil.putObjectList("loc_city_list", list);
  56. List<City> dataList =
  57. SpUtil.getObjList("loc_city_list", (v) => City.fromJson(v));
  58. print(
  59. "thll CityList: " + (dataList == null ? "null" : dataList.toString()));
  60. }
  61. @override
  62. Widget build(BuildContext context) {
  63. return new MaterialApp(
  64. home: new MainPage(),
  65. );
  66. }
  67. }
  68. class MainPage extends StatefulWidget {
  69. @override
  70. State<StatefulWidget> createState() {
  71. return new MainPageState();
  72. }
  73. }
  74. /// 在MainPage使用依赖不context方法获取屏幕参数及适配,需要build方法内调用[MediaQuery.of(context)]。
  75. /// 或者使用依赖context方法获取屏幕参数及适配。
  76. /// 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.
  77. /// Or use context-dependent methods to obtain screen parameters and adaptions.
  78. class MainPageState extends State<MainPage> {
  79. @override
  80. Widget build(BuildContext context) {
  81. /// 如果使用依赖不context方法获取屏幕参数及适配,需要调用此方法。
  82. /// If you use a dependent context-free method to obtain screen parameters and adaptions, you need to call this method.
  83. MediaQuery.of(context);
  84. double statusBar = ScreenUtil.getInstance().statusBarHeight;
  85. double width = ScreenUtil.getInstance().screenWidth;
  86. double height = ScreenUtil.getInstance().screenHeight;
  87. double density = ScreenUtil.getInstance().screenDensity;
  88. double sp = ScreenUtil.getInstance().getAdapterSize(24);
  89. double spc = ScreenUtil.getInstance().getAdapterSize(24);
  90. double adapterW = ScreenUtil.getInstance().getAdapterSize(360);
  91. print(
  92. "thll MainPage statusBar: $statusBar, width: $width, height: $height, density: $density, sp: $sp, spc: $spc, adapterW: $adapterW");
  93. return new Scaffold(
  94. // 一个不需要GlobalKey就可以openDrawer的AppBar
  95. appBar: new MyAppBar(
  96. leading: ClipOval(
  97. child: new Image.asset(('assets/images/ali_connors.png')),
  98. ),
  99. title: const Text('Flustars Demos'),
  100. centerTitle: true,
  101. actions: <Widget>[
  102. new IconButton(
  103. icon: new Icon(Icons.search),
  104. onPressed: () {
  105. Navigator.push(
  106. context,
  107. new CupertinoPageRoute<void>(
  108. builder: (ctx) => new SecondPage()));
  109. },
  110. ),
  111. ],
  112. ),
  113. body: new Column(
  114. crossAxisAlignment: CrossAxisAlignment.start,
  115. children: <Widget>[
  116. new Container(
  117. width: 360.0,
  118. height: 50,
  119. color: Colors.grey,
  120. child: new Center(
  121. child: new Text(
  122. "未适配宽",
  123. style: new TextStyle(fontSize: 24.0),
  124. ),
  125. ),
  126. ),
  127. new Container(
  128. width: ScreenUtil.getInstance().getAdapterSize(360.0),
  129. height: 50,
  130. color: Colors.grey,
  131. child: new Center(
  132. child: new Text(
  133. "已适配宽",
  134. style: new TextStyle(fontSize: 24.0),
  135. ),
  136. ),
  137. ),
  138. new Container(
  139. width: 100,
  140. height: 100,
  141. color: Colors.grey,
  142. child: new Center(
  143. child: new Text(
  144. "你好你好你好",
  145. style: new TextStyle(fontSize: 24.0),
  146. ),
  147. ),
  148. ),
  149. new Container(
  150. margin: EdgeInsets.only(top: 10.0),
  151. width: ScreenUtil.getInstance().getAdapterSize(100.0),
  152. height: ScreenUtil.getInstance().getAdapterSize(100.0),
  153. color: Colors.grey,
  154. child: new Center(
  155. child: new Text(
  156. "你好你好你好",
  157. style: new TextStyle(
  158. fontSize: ScreenUtil.getInstance().getAdapterSize(24.0)),
  159. ),
  160. ),
  161. ),
  162. ],
  163. ),
  164. drawer: new MyDrawer(),
  165. );
  166. }
  167. }
  168. class MyDrawer extends StatelessWidget {
  169. @override
  170. Widget build(BuildContext context) {
  171. double statusBar = ScreenUtil.getInstance().statusBarHeight;
  172. double width = ScreenUtil.getInstance().screenWidth;
  173. double height = ScreenUtil.getInstance().screenHeight;
  174. print(
  175. " thll SecondPage statusBar: $statusBar, width: $width, height: $height");
  176. return new Container(
  177. color: Colors.white,
  178. width: ScreenUtil.getInstance().getWidth(240),
  179. child: new ListView(
  180. padding: EdgeInsets.zero,
  181. children: <Widget>[
  182. new Container(
  183. color: Colors.teal,
  184. padding:
  185. EdgeInsets.only(top: ScreenUtil.getInstance().statusBarHeight),
  186. child: new Center(
  187. child: new Text(
  188. "Sky24n",
  189. style: new TextStyle(fontSize: 16, color: Colors.white),
  190. ),
  191. ),
  192. height: 160,
  193. )
  194. ],
  195. ),
  196. );
  197. }
  198. }
  199. class SecondPage extends StatefulWidget {
  200. @override
  201. State<StatefulWidget> createState() {
  202. return new SecondPageState();
  203. }
  204. }
  205. class SecondPageState extends State<SecondPage> {
  206. @override
  207. void initState() {
  208. super.initState();
  209. _init();
  210. _initWithCtx();
  211. }
  212. void _init() {
  213. double screenWidth = ScreenUtil.getInstance().screenWidth;
  214. double screenHeight = ScreenUtil.getInstance().screenHeight;
  215. double screenDensity = ScreenUtil.getInstance().screenDensity;
  216. double statusBarHeight = ScreenUtil.getInstance().statusBarHeight;
  217. double bottomBarHeight = ScreenUtil.getInstance().bottomBarHeight;
  218. double appBarHeight = ScreenUtil.getInstance().appBarHeight;
  219. double adapterW100 = ScreenUtil.getInstance().getWidth(100);
  220. double adapterH100 = ScreenUtil.getInstance().getHeight(100);
  221. double adapterSp100 = ScreenUtil.getInstance().getSp(100);
  222. double adapterW100px = ScreenUtil.getInstance().getWidthPx(300);
  223. double adapterH100px = ScreenUtil.getInstance().getHeightPx(300);
  224. print("thll SecondPage _init screenWidth: $screenWidth, screenHeight: $screenHeight, screenDensity: $screenDensity" +
  225. ", statusBarHeight: $statusBarHeight, bottomBarHeight: $bottomBarHeight, appBarHeight: $appBarHeight" +
  226. ", adapterW100: $adapterW100, adapterH100: $adapterH100, adapterSp100: $adapterSp100" +
  227. ", adapterW100px: $adapterW100px, adapterH100px: $adapterH100px");
  228. }
  229. void _initWithCtx() {
  230. double screenWidth = ScreenUtil.getScreenW(context);
  231. double screenHeight = ScreenUtil.getScreenH(context);
  232. double screenDensity = ScreenUtil.getScreenDensity(context);
  233. double statusBarHeight = ScreenUtil.getStatusBarH(context);
  234. double bottomBarHeight = ScreenUtil.getBottomBarH(context);
  235. double adapterW100 = ScreenUtil.getScaleW(context, 100);
  236. double adapterH100 = ScreenUtil.getScaleH(context, 100);
  237. double adapterSp100 = ScreenUtil.getScaleSp(context, 100);
  238. Orientation orientation = ScreenUtil.getOrientation(context);
  239. print("thll SecondPage _initWithCtx screenWidth: $screenWidth, screenHeight: $screenHeight, screenDensity: $screenDensity" +
  240. ", statusBarHeight: $statusBarHeight, bottomBarHeight: $bottomBarHeight" +
  241. ", adapterW100: $adapterW100, adapterH100: $adapterH100, adapterSp100: $adapterSp100");
  242. }
  243. @override
  244. Widget build(BuildContext context) {
  245. double statusBar = ScreenUtil.getInstance().statusBarHeight;
  246. double width = ScreenUtil.getInstance().screenWidth;
  247. double height = ScreenUtil.getInstance().screenHeight;
  248. print(
  249. "thll SecondPage statusBar: $statusBar, width: $width, height: $height");
  250. return new Scaffold(
  251. appBar: new AppBar(
  252. title: new Text("Second Page"),
  253. centerTitle: true,
  254. ),
  255. body: new Column(
  256. crossAxisAlignment: CrossAxisAlignment.start,
  257. children: <Widget>[
  258. new Container(
  259. width: 100,
  260. height: 100,
  261. color: Colors.grey,
  262. child: new Center(
  263. child: new Text(
  264. "你好你好你好",
  265. style: new TextStyle(fontSize: 24.0),
  266. ),
  267. ),
  268. ),
  269. new Container(
  270. margin: EdgeInsets.only(top: 10.0),
  271. width: ScreenUtil.getInstance().getAdapterSize(100.0),
  272. height: ScreenUtil.getInstance().getAdapterSize(100.0),
  273. color: Colors.grey,
  274. child: new Center(
  275. child: new Text(
  276. "你好你好你好",
  277. style: new TextStyle(
  278. fontSize: ScreenUtil.getInstance().getAdapterSize(24.0)),
  279. ),
  280. ),
  281. ),
  282. new Container(
  283. margin: EdgeInsets.only(top: 10.0),
  284. width: ScreenUtil.getAdapterSizeCtx(context, 100.0),
  285. height: ScreenUtil.getAdapterSizeCtx(context, 100.0),
  286. color: Colors.grey,
  287. child: new Center(
  288. child: new Text(
  289. "你好你好你好",
  290. style: new TextStyle(
  291. fontSize: ScreenUtil.getAdapterSizeCtx(context, 24.0)),
  292. ),
  293. ),
  294. ),
  295. ],
  296. ),
  297. );
  298. }
  299. }