main.dart 12 KB

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