main.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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: AppBar(
  124. leading: Builder(builder: (BuildContext ctx) {
  125. return IconButton(
  126. icon: ClipOval(
  127. child: Image.asset(('assets/images/ali_connors.png')),
  128. ),
  129. onPressed: () {
  130. Scaffold.of(ctx).openDrawer();
  131. });
  132. }),
  133. title: const Text('Flustars Demos'),
  134. centerTitle: true,
  135. actions: <Widget>[
  136. IconButton(
  137. icon: Icon(Icons.search),
  138. onPressed: () {
  139. print("thll onPressed......");
  140. //test2();
  141. test();
  142. // Navigator.push(context,
  143. // CupertinoPageRoute<void>(builder: (ctx) => SecondPage()));
  144. },
  145. ),
  146. ],
  147. ),
  148. body: Column(
  149. crossAxisAlignment: CrossAxisAlignment.start,
  150. children: <Widget>[
  151. Container(
  152. width: 360.0,
  153. height: 50,
  154. color: Colors.grey,
  155. child: Center(
  156. child: Text(
  157. "未适配宽",
  158. style: TextStyle(fontSize: 24.0),
  159. ),
  160. ),
  161. ),
  162. Container(
  163. width: ScreenUtil.getInstance().getAdapterSize(360.0),
  164. height: 50,
  165. color: Colors.grey,
  166. child: Center(
  167. child: Text(
  168. "已适配宽",
  169. style: TextStyle(fontSize: 24.0),
  170. ),
  171. ),
  172. ),
  173. Container(
  174. width: 100,
  175. height: 100,
  176. color: Colors.grey,
  177. child: Center(
  178. child: Text(
  179. "你好你好你好",
  180. style: TextStyle(fontSize: 24.0),
  181. ),
  182. ),
  183. ),
  184. Container(
  185. margin: EdgeInsets.only(top: 10.0),
  186. width: ScreenUtil.getInstance().getAdapterSize(100.0),
  187. height: ScreenUtil.getInstance().getAdapterSize(100.0),
  188. color: Colors.grey,
  189. child: Center(
  190. child: Text(
  191. "你好你好你好",
  192. style: TextStyle(
  193. fontSize: ScreenUtil.getInstance().getAdapterSize(24.0)),
  194. ),
  195. ),
  196. ),
  197. ],
  198. ),
  199. drawer: MyDrawer(),
  200. );
  201. }
  202. }
  203. class MyDrawer extends StatelessWidget {
  204. @override
  205. Widget build(BuildContext context) {
  206. double statusBar = ScreenUtil.getInstance().statusBarHeight;
  207. double width = ScreenUtil.getInstance().screenWidth;
  208. double height = ScreenUtil.getInstance().screenHeight;
  209. print(
  210. " thll MyDrawer statusBar: $statusBar, width: $width, height: $height");
  211. return Container(
  212. color: Colors.white,
  213. width: ScreenUtil.getInstance().getWidth(240),
  214. child: ListView(
  215. padding: EdgeInsets.zero,
  216. children: <Widget>[
  217. Container(
  218. color: Colors.teal,
  219. padding:
  220. EdgeInsets.only(top: ScreenUtil.getInstance().statusBarHeight),
  221. child: Center(
  222. child: Text(
  223. "Sky24n",
  224. style: TextStyle(fontSize: 16, color: Colors.white),
  225. ),
  226. ),
  227. height: 160,
  228. )
  229. ],
  230. ),
  231. );
  232. }
  233. }
  234. class SecondPage extends StatefulWidget {
  235. @override
  236. State<StatefulWidget> createState() {
  237. return SecondPageState();
  238. }
  239. }
  240. class SecondPageState extends State<SecondPage> {
  241. @override
  242. void initState() {
  243. super.initState();
  244. _init();
  245. // _initWithCtx();
  246. }
  247. void _init() {
  248. double screenWidth = ScreenUtil.getInstance().screenWidth;
  249. double screenHeight = ScreenUtil.getInstance().screenHeight;
  250. double screenDensity = ScreenUtil.getInstance().screenDensity;
  251. double statusBarHeight = ScreenUtil.getInstance().statusBarHeight;
  252. double bottomBarHeight = ScreenUtil.getInstance().bottomBarHeight;
  253. double appBarHeight = ScreenUtil.getInstance().appBarHeight;
  254. double adapterW100 = ScreenUtil.getInstance().getWidth(100);
  255. double adapterH100 = ScreenUtil.getInstance().getHeight(100);
  256. double adapterSp100 = ScreenUtil.getInstance().getSp(100);
  257. double adapterW100px = ScreenUtil.getInstance().getWidthPx(300);
  258. double adapterH100px = ScreenUtil.getInstance().getHeightPx(300);
  259. print("thll SecondPage _init screenWidth: $screenWidth, screenHeight: $screenHeight, screenDensity: $screenDensity" +
  260. ", statusBarHeight: $statusBarHeight, bottomBarHeight: $bottomBarHeight, appBarHeight: $appBarHeight" +
  261. ", adapterW100: $adapterW100, adapterH100: $adapterH100, adapterSp100: $adapterSp100" +
  262. ", adapterW100px: $adapterW100px, adapterH100px: $adapterH100px");
  263. }
  264. void _initWithCtx() {
  265. double screenWidth = ScreenUtil.getScreenW(context);
  266. double screenHeight = ScreenUtil.getScreenH(context);
  267. double screenDensity = ScreenUtil.getScreenDensity(context);
  268. double statusBarHeight = ScreenUtil.getStatusBarH(context);
  269. double bottomBarHeight = ScreenUtil.getBottomBarH(context);
  270. double adapterW100 = ScreenUtil.getScaleW(context, 100);
  271. double adapterH100 = ScreenUtil.getScaleH(context, 100);
  272. double adapterSp100 = ScreenUtil.getScaleSp(context, 100);
  273. Orientation orientation = ScreenUtil.getOrientation(context);
  274. print("thll SecondPage _initWithCtx screenWidth: $screenWidth, screenHeight: $screenHeight, screenDensity: $screenDensity" +
  275. ", statusBarHeight: $statusBarHeight, bottomBarHeight: $bottomBarHeight" +
  276. ", adapterW100: $adapterW100, adapterH100: $adapterH100, adapterSp100: $adapterSp100");
  277. }
  278. @override
  279. Widget build(BuildContext context) {
  280. double statusBar = ScreenUtil.getInstance().statusBarHeight;
  281. double width = ScreenUtil.getInstance().screenWidth;
  282. double height = ScreenUtil.getInstance().screenHeight;
  283. print(
  284. "thll SecondPage statusBar: $statusBar, width: $width, height: $height");
  285. return Scaffold(
  286. appBar: AppBar(
  287. title: Text("Second Page"),
  288. centerTitle: true,
  289. ),
  290. body: Column(
  291. crossAxisAlignment: CrossAxisAlignment.start,
  292. children: <Widget>[
  293. Container(
  294. width: 100,
  295. height: 100,
  296. color: Colors.grey,
  297. child: Center(
  298. child: Text(
  299. "你好你好你好",
  300. style: TextStyle(fontSize: 24.0),
  301. ),
  302. ),
  303. ),
  304. Container(
  305. margin: EdgeInsets.only(top: 10.0),
  306. width: ScreenUtil.getInstance().getAdapterSize(100.0),
  307. height: ScreenUtil.getInstance().getAdapterSize(100.0),
  308. color: Colors.grey,
  309. child: Center(
  310. child: Text(
  311. "你好你好你好",
  312. style: TextStyle(
  313. fontSize: ScreenUtil.getInstance().getAdapterSize(24.0)),
  314. ),
  315. ),
  316. ),
  317. Container(
  318. margin: EdgeInsets.only(top: 10.0),
  319. width: ScreenUtil.getAdapterSizeCtx(context, 100.0),
  320. height: ScreenUtil.getAdapterSizeCtx(context, 100.0),
  321. color: Colors.grey,
  322. child: Center(
  323. child: Text(
  324. "你好你好你好",
  325. style: TextStyle(
  326. fontSize: ScreenUtil.getAdapterSizeCtx(context, 24.0)),
  327. ),
  328. ),
  329. ),
  330. ],
  331. ),
  332. );
  333. }
  334. }