main.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. Map dataStr = SpUtil.getObject("loc_city");
  49. City hisCity = dataStr == null ? null : City.fromJson(dataStr);
  50. print("thll Str: " + (hisCity == null ? "null" : hisCity.toString()));
  51. /// save object list example.
  52. /// 存储实体对象list示例。
  53. List<City> list = new List();
  54. list.add(new City(name: "成都市"));
  55. list.add(new City(name: "北京市"));
  56. SpUtil.putObjectList("loc_city_list", list);
  57. List<Map> dataList = SpUtil.getObjectList("loc_city_list");
  58. List<City> _cityList = dataList?.map((value) {
  59. return City.fromJson(value);
  60. })?.toList();
  61. print("thll List: " + (_cityList == null ? "null" : _cityList.toString()));
  62. }
  63. @override
  64. Widget build(BuildContext context) {
  65. return new MaterialApp(
  66. home: new MainPage(),
  67. );
  68. }
  69. }
  70. class MainPage extends StatefulWidget {
  71. @override
  72. State<StatefulWidget> createState() {
  73. return new MainPageState();
  74. }
  75. }
  76. /// 在MainPage使用依赖不context方法获取屏幕参数及适配,需要build方法内调用[MediaQuery.of(context)]。
  77. /// 或者使用依赖context方法获取屏幕参数及适配。
  78. /// 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.
  79. /// Or use context-dependent methods to obtain screen parameters and adaptions.
  80. class MainPageState extends State<MainPage> {
  81. @override
  82. Widget build(BuildContext context) {
  83. /// 如果使用依赖不context方法获取屏幕参数及适配,需要调用此方法。
  84. /// If you use a dependent context-free method to obtain screen parameters and adaptions, you need to call this method.
  85. MediaQuery.of(context);
  86. double statusBar = ScreenUtil.getInstance().statusBarHeight;
  87. double width = ScreenUtil.getInstance().screenWidth;
  88. double height = ScreenUtil.getInstance().screenHeight;
  89. double density = ScreenUtil.getInstance().screenDensity;
  90. double sp = ScreenUtil.getInstance().getSp(24);
  91. double spc = ScreenUtil.getScaleSp(context, 24);
  92. print(
  93. "MainPage statusBar: $statusBar, width: $width, height: $height, density: $density, sp: $sp, spc: $spc");
  94. return new Scaffold(
  95. // 一个不需要GlobalKey就可以openDrawer的AppBar
  96. appBar: new MyAppBar(
  97. leading: ClipOval(
  98. child: new Image.asset(('assets/images/ali_connors.png')),
  99. ),
  100. title: const Text('Flustars Demos'),
  101. centerTitle: true,
  102. actions: <Widget>[
  103. new IconButton(
  104. icon: new Icon(Icons.search),
  105. onPressed: () {
  106. Navigator.push(
  107. context,
  108. new CupertinoPageRoute<void>(
  109. builder: (ctx) => new SecondPage()));
  110. },
  111. ),
  112. ],
  113. ),
  114. body: new Column(
  115. crossAxisAlignment: CrossAxisAlignment.start,
  116. children: <Widget>[
  117. new Container(
  118. width: 360.0,
  119. height: 50,
  120. color: Colors.grey,
  121. child: new Center(
  122. child: new Text(
  123. "未适配宽",
  124. style: new TextStyle(fontSize: 24.0),
  125. ),
  126. ),
  127. ),
  128. new Container(
  129. width: ScreenUtil.getInstance().getWidth(360.0),
  130. height: 50,
  131. color: Colors.grey,
  132. child: new Center(
  133. child: new Text(
  134. "已适配宽",
  135. style: new TextStyle(fontSize: 24.0),
  136. ),
  137. ),
  138. ),
  139. new Container(
  140. width: 100,
  141. height: 100,
  142. color: Colors.grey,
  143. child: new Center(
  144. child: new Text(
  145. "你好你好你好",
  146. style: new TextStyle(fontSize: 24.0),
  147. ),
  148. ),
  149. ),
  150. new Container(
  151. margin: EdgeInsets.only(top: 10.0),
  152. width: ScreenUtil.getInstance().getWidth(100.0),
  153. height: ScreenUtil.getInstance().getWidth(100.0),
  154. color: Colors.grey,
  155. child: new Center(
  156. child: new Text(
  157. "你好你好你好",
  158. style: new TextStyle(
  159. fontSize: ScreenUtil.getInstance().getSp(24.0)),
  160. ),
  161. ),
  162. ),
  163. ],
  164. ),
  165. drawer: new MyDrawer(),
  166. );
  167. }
  168. }
  169. class MyDrawer extends StatelessWidget {
  170. @override
  171. Widget build(BuildContext context) {
  172. double statusBar = ScreenUtil.getInstance().statusBarHeight;
  173. double width = ScreenUtil.getInstance().screenWidth;
  174. double height = ScreenUtil.getInstance().screenHeight;
  175. print("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("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("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("SecondPage statusBar: $statusBar, width: $width, height: $height");
  249. return new Scaffold(
  250. appBar: new AppBar(
  251. title: new Text("Second Page"),
  252. centerTitle: true,
  253. ),
  254. body: new Column(
  255. crossAxisAlignment: CrossAxisAlignment.start,
  256. children: <Widget>[
  257. new Container(
  258. width: 100,
  259. height: 100,
  260. color: Colors.grey,
  261. child: new Center(
  262. child: new Text(
  263. "你好你好你好",
  264. style: new TextStyle(fontSize: 24.0),
  265. ),
  266. ),
  267. ),
  268. new Container(
  269. margin: EdgeInsets.only(top: 10.0),
  270. width: ScreenUtil.getInstance().getWidth(100.0),
  271. height: ScreenUtil.getInstance().getWidth(100.0),
  272. color: Colors.grey,
  273. child: new Center(
  274. child: new Text(
  275. "你好你好你好",
  276. style: new TextStyle(
  277. fontSize: ScreenUtil.getInstance().getSp(24.0)),
  278. ),
  279. ),
  280. ),
  281. new Container(
  282. margin: EdgeInsets.only(top: 10.0),
  283. width: ScreenUtil.getScaleW(context, 100.0),
  284. height: ScreenUtil.getScaleW(context, 100.0),
  285. color: Colors.grey,
  286. child: new Center(
  287. child: new Text(
  288. "你好你好你好",
  289. style: new TextStyle(
  290. fontSize: ScreenUtil.getScaleSp(context, 24.0)),
  291. ),
  292. ),
  293. ),
  294. ],
  295. ),
  296. );
  297. }
  298. }