main.dart 11 KB

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