my_page.dart 19 KB

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