my_page.dart 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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 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. });
  96. getKey('btList').then((value) {
  97. print(value);
  98. if (value != '' && check == true) {
  99. List tempList = [];
  100. for (int i = 0; i < 999; i++) {
  101. if (value.indexOf('&') != -1) {
  102. int index = value.indexOf('&');
  103. tempList.add(value.substring(0, index));
  104. value = value.substring(index + 1, value.length);
  105. } else {
  106. break;
  107. }
  108. }
  109. for (int i = 0; i < tempList.length; i++) {
  110. BtInfoObj tempObj = BtInfoObj(name: tempList[i], showLoadStauts: false, connectStatus: false);
  111. setState(() {
  112. btList.add(tempObj);
  113. });
  114. }
  115. }
  116. });
  117. }
  118. onEvent(Object obj) {
  119. print('进入onEvent回调${obj}');
  120. if (obj.toString().indexOf('||') != -1) { //过滤开始搜索和结束搜索
  121. btSet.add(obj.toString()); //去重
  122. List tempBtList = btSet.toList();
  123. List<BtInfoObj> tempBtList2 = [];
  124. for (int i = 0; i < tempBtList.length; i++) {
  125. tempBtList2.add(
  126. BtInfoObj(name: tempBtList[i], showLoadStauts: false, connectStatus: false)
  127. );
  128. }
  129. setState(() {
  130. btList =tempBtList2;
  131. });
  132. print('设置btList完成');
  133. String tempStr = '';
  134. for (int i = 0; i < btList.length; i++) {
  135. tempStr += '${btList[i].name}&';
  136. }
  137. print('设置临时字符串');
  138. print(tempStr);
  139. setKey('btList', tempStr);
  140. }
  141. }
  142. onError(Object obj) {
  143. print(obj.toString());
  144. }
  145. @override
  146. Widget build(BuildContext context) {
  147. return Container(
  148. child: Scaffold(
  149. appBar: AppBar(
  150. elevation: 0,
  151. title: Text('我的',
  152. style: TextStyle(
  153. fontSize: ScreenUtil.getInstance().setSp(36)
  154. ), ),
  155. centerTitle: true,
  156. backgroundColor: Color.fromRGBO(64, 98, 254, 1),
  157. actions: < Widget > [
  158. InkWell(
  159. child: Container(
  160. padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(30)),
  161. child: Center(
  162. child: Text('退出'),
  163. )
  164. ),
  165. onTap: () {
  166. showDialog < Null > (
  167. context: context, //BuildContext对象
  168. barrierDismissible: false,
  169. builder: (BuildContext context) {
  170. return new LoadingDialog( //调用对话框
  171. text: '确认退出?', childCallback: (val) {
  172. if (val) {
  173. clearKey();
  174. Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) {
  175. return LoginPage();
  176. }));
  177. } else {
  178. return;
  179. }
  180. },
  181. );
  182. });
  183. },
  184. )
  185. ],
  186. ),
  187. body: Container(
  188. color: Color.fromRGBO(246, 246, 254, 1),
  189. child: Column(
  190. children: < Widget > [
  191. _avatarWidget(),
  192. _funWrapWidget(context),
  193. ],
  194. ),
  195. ),
  196. )
  197. );
  198. }
  199. Widget _avatarWidget() {
  200. return Container(
  201. color: Color.fromRGBO(64, 98, 254, 1),
  202. padding: EdgeInsets.fromLTRB(0, ScreenUtil.getInstance().setHeight(59), 0, ScreenUtil.getInstance().setHeight(67), ),
  203. child: Column(
  204. children: < Widget > [
  205. Center(
  206. child: Padding(
  207. padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setWidth(24), ),
  208. child: ClipOval(
  209. child: Image.asset('lib/images/icon_logo.png',
  210. fit: BoxFit.cover,
  211. width: ScreenUtil.getInstance().setWidth(120),
  212. height: ScreenUtil.getInstance().setWidth(120), ),
  213. )
  214. )
  215. ),
  216. Center(child: Text(name, style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(255, 255, 255, 1)), ), )
  217. ],
  218. ),
  219. );
  220. }
  221. Widget _funWrapWidget(BuildContext context) {
  222. return Container(
  223. padding: EdgeInsets.only(top: ScreenUtil.getInstance().setHeight(20)),
  224. child: Column(
  225. children: < Widget > [
  226. _switchSite(context),
  227. _passWordWidget(),
  228. _linkPrint(),
  229. _printType(),
  230. _btList()
  231. ],
  232. ),
  233. );
  234. }
  235. Widget _switchSite(BuildContext context) {
  236. return InkWell(
  237. child: Container(
  238. height: ScreenUtil.getInstance().setHeight(90),
  239. color: Colors.white,
  240. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
  241. child: Column(
  242. children: < Widget > [
  243. Expanded(
  244. child: Center(
  245. child: Row(
  246. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  247. children: < Widget > [
  248. Expanded(
  249. child: Row(
  250. mainAxisAlignment: MainAxisAlignment.start,
  251. children: < Widget > [
  252. Padding(
  253. padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
  254. child: Image.asset('lib/images/icon_zhandianss.png',
  255. width: ScreenUtil.getInstance().setWidth(30),
  256. height: ScreenUtil.getInstance().setHeight(34),
  257. )
  258. ),
  259. Text('站点切换', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
  260. ],
  261. ),
  262. ),
  263. Expanded(
  264. child: Row(
  265. mainAxisAlignment: MainAxisAlignment.end,
  266. children: < Widget > [
  267. Text(_siteName, style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(153, 153, 153, 1)), ),
  268. Icon(Icons.keyboard_arrow_right, color: Color.fromRGBO(199, 199, 204, 1))
  269. ],
  270. ),
  271. ),
  272. ],
  273. ),
  274. ),
  275. ),
  276. Expanded(
  277. flex: 0,
  278. child: _underLine(),
  279. )
  280. ],
  281. )
  282. ),
  283. onTap: () {
  284. // showDialog < Null > (
  285. // context: context, //BuildContext对象
  286. // barrierDismissible: false,
  287. // builder: (BuildContext context) {
  288. // return SiteDialog( //调用对话框
  289. // );
  290. // });
  291. showPickerModal(context, 'station');
  292. },
  293. );
  294. }
  295. showPickerModal(BuildContext context, String type) {
  296. var list;
  297. if (type == 'station') {
  298. list = stationList;
  299. } else if (type == 'printType') {
  300. list = printTypeList;
  301. }
  302. new Picker(
  303. adapter: PickerDataAdapter < String > (pickerdata: list),
  304. height: ScreenUtil.getInstance().setHeight(450),
  305. confirmText: '确定',
  306. confirmTextStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(64, 98, 254, 1)),
  307. cancelText: '取消',
  308. cancelTextStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(153, 153, 153, 1)),
  309. headercolor: Color.fromRGBO(245, 245, 245, 1),
  310. itemExtent: ScreenUtil.getInstance().setHeight(80),
  311. textStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(42, 42, 42, 1)),
  312. changeToFirst: true,
  313. hideHeader: false,
  314. onConfirm: (Picker picker, List value) {
  315. if (type == 'station') {
  316. setState(() {
  317. _siteName = stationObj.data[value[0]].name;
  318. });
  319. setKey('stationId', stationObj.data[value[0]].id.toString());
  320. } else if (type == "printType") {
  321. setState(() {
  322. printType = printTypeList[value[0]];
  323. });
  324. setKey('printType', printTypeMap[printTypeList[value[0]]]);
  325. getKey('printType').then((value) {
  326. print(value);
  327. });
  328. }
  329. }
  330. ).showModal(this.context); //_scaffoldKey.currentState);
  331. }
  332. Widget _underLine() {
  333. return Container(
  334. height: ScreenUtil.getInstance().setHeight(1),
  335. color: Color.fromRGBO(223, 223, 223, 1),
  336. );
  337. }
  338. Widget _passWordWidget() {
  339. return
  340. GestureDetector(
  341. child: Container(
  342. height: ScreenUtil.getInstance().setHeight(90),
  343. color: Colors.white,
  344. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
  345. child: Column(
  346. children: < Widget > [
  347. Expanded(
  348. child: Center(
  349. child: Row(
  350. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  351. children: < Widget > [
  352. Expanded(
  353. child: Row(
  354. mainAxisAlignment: MainAxisAlignment.start,
  355. children: < Widget > [
  356. Padding(
  357. padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
  358. child: Image.asset('lib/images/icon_mimas.png',
  359. width: ScreenUtil.getInstance().setWidth(30),
  360. height: ScreenUtil.getInstance().setHeight(34),
  361. )
  362. ),
  363. Text('修改密码', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
  364. ],
  365. ),
  366. ),
  367. Icon(Icons.keyboard_arrow_right, color: Color.fromRGBO(199, 199, 204, 1))
  368. ],
  369. ),
  370. ),
  371. )
  372. ],
  373. )
  374. ),
  375. onTap: () {
  376. Navigator.push(context, MaterialPageRoute(builder: (context) {
  377. return RevisePasswordPage();
  378. }));
  379. },
  380. );
  381. }
  382. Widget _linkPrint() {
  383. return Container(
  384. height: ScreenUtil.getInstance().setHeight(90),
  385. color: Colors.white,
  386. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
  387. margin: EdgeInsets.only(top: ScreenUtil.getInstance().setHeight(20)),
  388. child: Column(
  389. children: < Widget > [
  390. Expanded(
  391. child: Center(
  392. child: Row(
  393. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  394. children: < Widget > [
  395. Expanded(
  396. child: Row(
  397. mainAxisAlignment: MainAxisAlignment.start,
  398. children: < Widget > [
  399. Padding(
  400. padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
  401. child: Image.asset('lib/images/icon_dayinji.png',
  402. width: ScreenUtil.getInstance().setWidth(30),
  403. height: ScreenUtil.getInstance().setHeight(34),
  404. )
  405. ),
  406. Text('打印机连接', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
  407. ],
  408. ),
  409. ),
  410. Switch(
  411. value: check,
  412. activeColor: Colors.blue,
  413. onChanged: (bool val) {
  414. if (val) {
  415. bluetooth.isBTDevice().then((resp) {
  416. if (resp) {
  417. bluetooth.isEnableBluetooth().then((bool resp) { //打开蓝牙
  418. setState(() {
  419. check = true;
  420. });
  421. setKey('check', 'true');
  422. bluetooth.scanBT(); //
  423. });
  424. }
  425. });
  426. } else {
  427. bluetooth.disConnnectBT(); //关闭蓝牙
  428. setState(() {
  429. check = false;
  430. btList.clear();
  431. });
  432. setKey('check', 'false');
  433. }
  434. },
  435. )
  436. ],
  437. ),
  438. ),
  439. ),
  440. _underLine()
  441. ],
  442. )
  443. );
  444. }
  445. Widget _printType() {
  446. return InkWell(
  447. child: Container(
  448. height: ScreenUtil.getInstance().setHeight(90),
  449. color: Colors.white,
  450. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
  451. child: Column(
  452. children: < Widget > [
  453. Expanded(
  454. child: Center(
  455. child: Row(
  456. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  457. children: < Widget > [
  458. Expanded(
  459. child: Row(
  460. mainAxisAlignment: MainAxisAlignment.start,
  461. children: < Widget > [
  462. Padding(
  463. padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
  464. child: Image.asset('lib/images/icon_dayinji.png',
  465. width: ScreenUtil.getInstance().setWidth(30),
  466. height: ScreenUtil.getInstance().setHeight(34),
  467. )
  468. ),
  469. Text('打印类型', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
  470. ],
  471. ),
  472. ),
  473. Row(
  474. mainAxisAlignment: MainAxisAlignment.end,
  475. children: < Widget > [
  476. Text(printType, style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(153, 153, 153, 1)), ),
  477. Icon(Icons.keyboard_arrow_right, color: Color.fromRGBO(199, 199, 204, 1))
  478. ],
  479. )
  480. ],
  481. ),
  482. ),
  483. )
  484. ],
  485. )
  486. ),
  487. onTap: () {
  488. showPickerModal(context, 'printType');
  489. },
  490. );
  491. }
  492. Widget _btList() {
  493. return Container(
  494. height: ScreenUtil.getInstance().setHeight(350),
  495. padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(30)),
  496. child: (btList.length > 0 && check == true) ? ListView.builder(
  497. itemCount: btList.length,
  498. itemBuilder: (BuildContext context, int index) {
  499. return _btItem(btList[index], index);
  500. },
  501. ) : check == true ? _searchBt() : Center()
  502. );
  503. }
  504. Widget _btItem(BtInfoObj obj, int index) {
  505. // print(obj.name);
  506. int tempIndex = obj.name.indexOf('||');
  507. String name = obj.name.substring(0, tempIndex);
  508. String btAddress = obj.name.substring(tempIndex + 2, obj.name.length);
  509. // print(btAddress);
  510. return InkWell(
  511. child:SafeArea(
  512. child:Padding(
  513. padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(30)),
  514. child: Row(
  515. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  516. children: < Widget > [
  517. Text(name),
  518. obj.showLoadStauts ? SizedBox(
  519. width: ScreenUtil.getInstance().setWidth(35),
  520. height: ScreenUtil.getInstance().setHeight(35),
  521. child: Image.asset('lib/images/loading.gif'),
  522. ) :
  523. CircleAvatar(
  524. backgroundColor: obj.connectStatus ? Color.fromRGBO(64, 98, 254, 1) : Colors.grey,
  525. radius: 6,
  526. )
  527. ],
  528. )
  529. ),
  530. ),
  531. onTap: () {
  532. //bluetooth.connnectBT(btAddress);
  533. setState(() {
  534. btList[index].showLoadStauts = true;
  535. });
  536. },
  537. );
  538. }
  539. Widget _searchBt() {
  540. return Center(
  541. child: Row(
  542. mainAxisAlignment: MainAxisAlignment.center,
  543. children: < Widget > [
  544. Text('正在搜索...'),
  545. SizedBox(
  546. width: ScreenUtil.getInstance().setWidth(45),
  547. height: ScreenUtil.getInstance().setHeight(45),
  548. child: Image.asset('lib/images/loading.gif'),
  549. )
  550. ],
  551. ),
  552. );
  553. }
  554. }