main.dart 11 KB

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