main.dart 11 KB

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