my_page.dart 22 KB

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