my_page.dart 22 KB

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