my_page.dart 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_screenutil/flutter_screenutil.dart';
  3. import 'my_page/revise_password_page.dart';
  4. import 'package:flutter_picker/flutter_picker.dart';
  5. import 'dart:convert';
  6. import '../util/bluetooth_utils.dart';
  7. import '../showDialog.dart';
  8. import '../login_page.dart';
  9. import '../util/api.dart';
  10. import '../util/models.dart';
  11. import '../util/session.dart';
  12. import '../util/util.dart';
  13. class MyPage extends StatefulWidget {
  14. final Widget child;
  15. MyPage({
  16. Key key,
  17. this.child
  18. }): super(key: key);
  19. _MyPageState createState() => _MyPageState();
  20. }
  21. class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
  22. bool check = false;
  23. String _siteName = "请选择站点";
  24. List < String > stationList = [];
  25. Station stationObj;
  26. String name = '物流';
  27. BluetoothUtils bluetooth = BluetoothUtils();
  28. List < BtInfoObj > btList = [];
  29. Set < BtInfoObj > btSet = Set();
  30. List printTypeList = ["全部", "配件商", "修理厂", "司机", "货物", "货物外", "不打印"];
  31. static
  32. var printTypeMap = {
  33. "全部": "1",
  34. "配件商": "2",
  35. "修理厂": "3",
  36. "司机": "4",
  37. "货物": "5",
  38. "货物外": "6",
  39. "不打印": "7",
  40. };
  41. static
  42. var printTypeMapSecond = {
  43. "1": "全部",
  44. "2": "配件商",
  45. "3": "修理厂",
  46. "4": "司机",
  47. "5": "货物",
  48. "6": "货物外",
  49. "7": "不打印",
  50. };
  51. String printType = '全部';
  52. @override
  53. bool get wantKeepAlive => true;
  54. @override
  55. void initState() {
  56. super.initState();
  57. getStationList().then((resp) {
  58. if (isNotError(context, resp)) {
  59. stationObj = Station.fromJson(resp.data);
  60. getKey('stationId').then((value) {
  61. List < String > tempList = [];
  62. for (int i = 0; i < stationObj.data.length; i++) {
  63. tempList.add(stationObj.data[i].name.toString());
  64. if (stationObj.data[i].id.toString() == value) {
  65. _siteName = stationObj.data[i].name.toString();
  66. }
  67. }
  68. getKey('nickName').then((value) {
  69. setState(() {
  70. stationList = tempList;
  71. name = value;
  72. });
  73. });
  74. getKey('printType').then((value) {
  75. printType = printTypeMapSecond[value];
  76. });
  77. });
  78. }
  79. });
  80. bluetooth.getEchannel().receiveBroadcastStream().listen(onEvent, onError: onError);
  81. getKey('check').then((value) {
  82. if (value == null) {
  83. setState(() {
  84. check = false;
  85. });
  86. } else if (value == 'false') {
  87. setState(() {
  88. check = false;
  89. });
  90. } else if (value == 'true') {
  91. setState(() {
  92. check = true;
  93. });
  94. }
  95. print(check);
  96. });
  97. }
  98. onEvent(Object obj) {
  99. if (obj.toString().indexOf('||') != -1) { //过滤开始搜索和结束搜索
  100. BtInfoObj tempObj = BtInfoObj(name: obj.toString(), showLoadStauts: false, connectStatus: false);
  101. btSet.add(tempObj);
  102. setState(() {
  103. btList = btSet.toList();
  104. });
  105. }
  106. }
  107. onError(Object obj) {
  108. print(obj.toString());
  109. }
  110. @override
  111. Widget build(BuildContext context) {
  112. return Container(
  113. child: Scaffold(
  114. appBar: AppBar(
  115. elevation: 0,
  116. title: Text('我的',
  117. style: TextStyle(
  118. fontSize: ScreenUtil.getInstance().setSp(36)
  119. ), ),
  120. centerTitle: true,
  121. backgroundColor: Color.fromRGBO(64, 98, 254, 1),
  122. actions: < Widget > [
  123. InkWell(
  124. child: Container(
  125. padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(30)),
  126. child: Center(
  127. child: Text('退出'),
  128. )
  129. ),
  130. onTap: () {
  131. showDialog < Null > (
  132. context: context, //BuildContext对象
  133. barrierDismissible: false,
  134. builder: (BuildContext context) {
  135. return new LoadingDialog( //调用对话框
  136. text: '确认退出?', childCallback: (val) {
  137. if (val) {
  138. clearKey();
  139. Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) {
  140. return LoginPage();
  141. }));
  142. } else {
  143. return;
  144. }
  145. },
  146. );
  147. });
  148. },
  149. )
  150. ],
  151. ),
  152. body: Container(
  153. color: Color.fromRGBO(246, 246, 254, 1),
  154. child: Column(
  155. children: < Widget > [
  156. _avatarWidget(),
  157. _funWrapWidget(context),
  158. ],
  159. ),
  160. ),
  161. )
  162. );
  163. }
  164. Widget _avatarWidget() {
  165. return Container(
  166. color: Color.fromRGBO(64, 98, 254, 1),
  167. padding: EdgeInsets.fromLTRB(0, ScreenUtil.getInstance().setHeight(59), 0, ScreenUtil.getInstance().setHeight(67), ),
  168. child: Column(
  169. children: < Widget > [
  170. Center(
  171. child: Padding(
  172. padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setWidth(24), ),
  173. child: ClipOval(
  174. child: Image.asset('lib/images/icon_logo.png',
  175. fit: BoxFit.cover,
  176. width: ScreenUtil.getInstance().setWidth(120),
  177. height: ScreenUtil.getInstance().setWidth(120), ),
  178. )
  179. )
  180. ),
  181. Center(child: Text(name, style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(255, 255, 255, 1)), ), )
  182. ],
  183. ),
  184. );
  185. }
  186. Widget _funWrapWidget(BuildContext context) {
  187. return Container(
  188. padding: EdgeInsets.only(top: ScreenUtil.getInstance().setHeight(20)),
  189. child: Column(
  190. children: < Widget > [
  191. _switchSite(context),
  192. _passWordWidget(),
  193. _linkPrint(),
  194. _printType(),
  195. _btList()
  196. ],
  197. ),
  198. );
  199. }
  200. Widget _switchSite(BuildContext context) {
  201. return InkWell(
  202. child: Container(
  203. height: ScreenUtil.getInstance().setHeight(90),
  204. color: Colors.white,
  205. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
  206. child: Column(
  207. children: < Widget > [
  208. Expanded(
  209. child: Center(
  210. child: Row(
  211. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  212. children: < Widget > [
  213. Expanded(
  214. child: Row(
  215. mainAxisAlignment: MainAxisAlignment.start,
  216. children: < Widget > [
  217. Padding(
  218. padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
  219. child: Image.asset('lib/images/icon_zhandianss.png',
  220. width: ScreenUtil.getInstance().setWidth(30),
  221. height: ScreenUtil.getInstance().setHeight(34),
  222. )
  223. ),
  224. Text('站点切换', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
  225. ],
  226. ),
  227. ),
  228. Expanded(
  229. child: Row(
  230. mainAxisAlignment: MainAxisAlignment.end,
  231. children: < Widget > [
  232. Text(_siteName, style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(153, 153, 153, 1)), ),
  233. Icon(Icons.keyboard_arrow_right, color: Color.fromRGBO(199, 199, 204, 1))
  234. ],
  235. ),
  236. ),
  237. ],
  238. ),
  239. ),
  240. ),
  241. Expanded(
  242. flex: 0,
  243. child: _underLine(),
  244. )
  245. ],
  246. )
  247. ),
  248. onTap: () {
  249. // showDialog < Null > (
  250. // context: context, //BuildContext对象
  251. // barrierDismissible: false,
  252. // builder: (BuildContext context) {
  253. // return SiteDialog( //调用对话框
  254. // );
  255. // });
  256. showPickerModal(context, 'station');
  257. },
  258. );
  259. }
  260. showPickerModal(BuildContext context, String type) {
  261. var list;
  262. if (type == 'station') {
  263. list = stationList;
  264. } else if (type == 'printType') {
  265. list = printTypeList;
  266. }
  267. new Picker(
  268. adapter: PickerDataAdapter < String > (pickerdata: list),
  269. height: ScreenUtil.getInstance().setHeight(450),
  270. confirmText: '确定',
  271. confirmTextStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(64, 98, 254, 1)),
  272. cancelText: '取消',
  273. cancelTextStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(153, 153, 153, 1)),
  274. headercolor: Color.fromRGBO(245, 245, 245, 1),
  275. itemExtent: ScreenUtil.getInstance().setHeight(80),
  276. textStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(42, 42, 42, 1)),
  277. changeToFirst: true,
  278. hideHeader: false,
  279. onConfirm: (Picker picker, List value) {
  280. if (type == 'station') {
  281. setState(() {
  282. _siteName = stationObj.data[value[0]].name;
  283. });
  284. setKey('stationId', stationObj.data[value[0]].id.toString());
  285. } else if (type == "printType") {
  286. setState(() {
  287. printType = printTypeList[value[0]];
  288. });
  289. setKey('printType', printTypeMap[printTypeList[value[0]]]);
  290. getKey('printType').then((value) {
  291. print(value);
  292. });
  293. }
  294. }
  295. ).showModal(this.context); //_scaffoldKey.currentState);
  296. }
  297. Widget _underLine() {
  298. return Container(
  299. height: ScreenUtil.getInstance().setHeight(1),
  300. color: Color.fromRGBO(223, 223, 223, 1),
  301. );
  302. }
  303. Widget _passWordWidget() {
  304. return
  305. GestureDetector(
  306. child: Container(
  307. height: ScreenUtil.getInstance().setHeight(90),
  308. color: Colors.white,
  309. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
  310. child: Column(
  311. children: < Widget > [
  312. Expanded(
  313. child: Center(
  314. child: Row(
  315. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  316. children: < Widget > [
  317. Expanded(
  318. child: Row(
  319. mainAxisAlignment: MainAxisAlignment.start,
  320. children: < Widget > [
  321. Padding(
  322. padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
  323. child: Image.asset('lib/images/icon_mimas.png',
  324. width: ScreenUtil.getInstance().setWidth(30),
  325. height: ScreenUtil.getInstance().setHeight(34),
  326. )
  327. ),
  328. Text('修改密码', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
  329. ],
  330. ),
  331. ),
  332. Icon(Icons.keyboard_arrow_right, color: Color.fromRGBO(199, 199, 204, 1))
  333. ],
  334. ),
  335. ),
  336. )
  337. ],
  338. )
  339. ),
  340. onTap: () {
  341. Navigator.push(context, MaterialPageRoute(builder: (context) {
  342. return RevisePasswordPage();
  343. }));
  344. },
  345. );
  346. }
  347. Widget _linkPrint() {
  348. return Container(
  349. height: ScreenUtil.getInstance().setHeight(90),
  350. color: Colors.white,
  351. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
  352. margin: EdgeInsets.only(top: ScreenUtil.getInstance().setHeight(20)),
  353. child: Column(
  354. children: < Widget > [
  355. Expanded(
  356. child: Center(
  357. child: Row(
  358. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  359. children: < Widget > [
  360. Expanded(
  361. child: Row(
  362. mainAxisAlignment: MainAxisAlignment.start,
  363. children: < Widget > [
  364. Padding(
  365. padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
  366. child: Image.asset('lib/images/icon_dayinji.png',
  367. width: ScreenUtil.getInstance().setWidth(30),
  368. height: ScreenUtil.getInstance().setHeight(34),
  369. )
  370. ),
  371. Text('打印机连接', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
  372. ],
  373. ),
  374. ),
  375. Switch(
  376. value: check,
  377. activeColor: Colors.blue,
  378. onChanged: (bool val) {
  379. if (val) {
  380. bluetooth.isBTDevice().then((resp) {
  381. if (resp) {
  382. bluetooth.isEnableBluetooth().then((bool resp) { //打开蓝牙
  383. setState(() {
  384. check = true;
  385. });
  386. setKey('check', 'true');
  387. bluetooth.scanBT(); //
  388. });
  389. }
  390. });
  391. } else {
  392. bluetooth.disConnnectBT(); //关闭蓝牙
  393. setState(() {
  394. check = false;
  395. btList.clear();
  396. });
  397. setKey('check', 'false');
  398. }
  399. },
  400. )
  401. ],
  402. ),
  403. ),
  404. ),
  405. _underLine()
  406. ],
  407. )
  408. );
  409. }
  410. Widget _printType() {
  411. return InkWell(
  412. child: Container(
  413. height: ScreenUtil.getInstance().setHeight(90),
  414. color: Colors.white,
  415. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
  416. child: Column(
  417. children: < Widget > [
  418. Expanded(
  419. child: Center(
  420. child: Row(
  421. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  422. children: < Widget > [
  423. Expanded(
  424. child: Row(
  425. mainAxisAlignment: MainAxisAlignment.start,
  426. children: < Widget > [
  427. Padding(
  428. padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
  429. child: Image.asset('lib/images/icon_dayinji.png',
  430. width: ScreenUtil.getInstance().setWidth(30),
  431. height: ScreenUtil.getInstance().setHeight(34),
  432. )
  433. ),
  434. Text('打印类型', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
  435. ],
  436. ),
  437. ),
  438. Row(
  439. mainAxisAlignment: MainAxisAlignment.end,
  440. children: < Widget > [
  441. Text(printType, style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(153, 153, 153, 1)), ),
  442. Icon(Icons.keyboard_arrow_right, color: Color.fromRGBO(199, 199, 204, 1))
  443. ],
  444. )
  445. ],
  446. ),
  447. ),
  448. )
  449. ],
  450. )
  451. ),
  452. onTap: () {
  453. showPickerModal(context, 'printType');
  454. },
  455. );
  456. }
  457. Widget _btList() {
  458. return Container(
  459. height: ScreenUtil.getInstance().setHeight(350),
  460. padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(30)),
  461. child: (btList.length > 0 && check==true) ? ListView.builder(
  462. itemCount: btList.length,
  463. itemBuilder: (BuildContext context, int index) {
  464. return _btItem(btList[index], index);
  465. },
  466. ) : check==true?_searchBt():Center()
  467. );
  468. }
  469. Widget _btItem(BtInfoObj obj, int index) {
  470. print(obj.name);
  471. int tempIndex = obj.name.indexOf('||');
  472. String name = obj.name.substring(0, tempIndex);
  473. String btAddress = obj.name.substring(tempIndex + 2, obj.name.length);
  474. print(btAddress);
  475. return InkWell(
  476. child: Padding(
  477. padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(30)),
  478. child: Row(
  479. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  480. children: < Widget > [
  481. Text(name),
  482. obj.showLoadStauts ? SizedBox(
  483. width: ScreenUtil.getInstance().setWidth(35),
  484. height: ScreenUtil.getInstance().setHeight(35),
  485. child: Image.asset('lib/images/loading.gif'),
  486. ) :
  487. CircleAvatar(
  488. backgroundColor: obj.connectStatus ? Color.fromRGBO(64, 98, 254, 1) : Colors.grey,
  489. radius: 6,
  490. )
  491. ],
  492. )
  493. ),
  494. onTap: () {
  495. //bluetooth.connnectBT(btAddress);
  496. setState(() {
  497. btList[index].showLoadStauts = true;
  498. });
  499. },
  500. );
  501. }
  502. Widget _searchBt() {
  503. return Center(
  504. child: Row(
  505. mainAxisAlignment: MainAxisAlignment.center,
  506. children: < Widget > [
  507. Text('正在搜索...'),
  508. SizedBox(
  509. width: ScreenUtil.getInstance().setWidth(45),
  510. height: ScreenUtil.getInstance().setHeight(45),
  511. child: Image.asset('lib/images/loading.gif'),
  512. )
  513. ],
  514. ),
  515. );
  516. }
  517. }