my_page.dart 20 KB

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