main.dart 12 KB

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