main.dart 11 KB

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