my_page.dart 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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)) {
  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. } else if (type == "printType") {
  374. setState(() {
  375. printType = printTypeList[value[0]];
  376. });
  377. setKey('printType', printTypeMap[printTypeList[value[0]]]);
  378. getKey('printType').then((value) {
  379. print(value);
  380. });
  381. }
  382. }
  383. ).showModal(this.context); //_scaffoldKey.currentState);
  384. }
  385. Widget _underLine() {
  386. return Container(
  387. height: ScreenUtil.getInstance().setHeight(1),
  388. color: Color.fromRGBO(223, 223, 223, 1),
  389. );
  390. }
  391. Widget _passWordWidget() {
  392. return
  393. GestureDetector(
  394. child: Container(
  395. height: ScreenUtil.getInstance().setHeight(90),
  396. color: Colors.white,
  397. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
  398. child: Column(
  399. children: < Widget > [
  400. Expanded(
  401. child: Center(
  402. child: Row(
  403. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  404. children: < Widget > [
  405. Expanded(
  406. child: Row(
  407. mainAxisAlignment: MainAxisAlignment.start,
  408. children: < Widget > [
  409. Padding(
  410. padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
  411. child: Image.asset('lib/images/icon_mimas.png',
  412. width: ScreenUtil.getInstance().setWidth(30),
  413. height: ScreenUtil.getInstance().setHeight(34),
  414. )
  415. ),
  416. Text('修改密码', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
  417. ],
  418. ),
  419. ),
  420. Icon(Icons.keyboard_arrow_right, color: Color.fromRGBO(199, 199, 204, 1))
  421. ],
  422. ),
  423. ),
  424. )
  425. ],
  426. )
  427. ),
  428. onTap: () {
  429. Navigator.push(context, MaterialPageRoute(builder: (context) {
  430. return RevisePasswordPage();
  431. }));
  432. },
  433. );
  434. }
  435. Widget _linkPrint() {
  436. return Container(
  437. height: ScreenUtil.getInstance().setHeight(90),
  438. color: Colors.white,
  439. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
  440. margin: EdgeInsets.only(top: ScreenUtil.getInstance().setHeight(20)),
  441. child: Column(
  442. children: < Widget > [
  443. Expanded(
  444. child: Center(
  445. child: Row(
  446. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  447. children: < Widget > [
  448. Expanded(
  449. child: Row(
  450. mainAxisAlignment: MainAxisAlignment.start,
  451. children: < Widget > [
  452. Padding(
  453. padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
  454. child: Image.asset('lib/images/icon_dayinji.png',
  455. width: ScreenUtil.getInstance().setWidth(30),
  456. height: ScreenUtil.getInstance().setHeight(34),
  457. )
  458. ),
  459. Text('打印机连接', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
  460. ],
  461. ),
  462. ),
  463. Switch(
  464. value: check,
  465. activeColor: Colors.blue,
  466. onChanged: (bool val) {
  467. if (val) {
  468. bluetooth.isBTDevice().then((resp) {
  469. if (resp) {
  470. bluetooth.isEnableBluetooth().then((bool resp) { //打开蓝牙
  471. bluetooth.enableBluetooth().then((resp){
  472. setState(() {
  473. check = true;
  474. btLinkStatus = false;
  475. });
  476. setKey('check', 'true');
  477. bluetooth.scanBT(); //
  478. });
  479. });
  480. }
  481. });
  482. } else {
  483. bluetooth.disConnnectBT(); //关闭蓝牙
  484. setState(() {
  485. check = false;
  486. btList.clear();
  487. btSet.clear();
  488. });
  489. setKey('check', 'false');
  490. }
  491. },
  492. )
  493. ],
  494. ),
  495. ),
  496. ),
  497. _underLine()
  498. ],
  499. )
  500. );
  501. }
  502. Widget _printType() {
  503. return InkWell(
  504. child: Container(
  505. height: ScreenUtil.getInstance().setHeight(90),
  506. color: Colors.white,
  507. padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
  508. child: Column(
  509. children: < Widget > [
  510. Expanded(
  511. child: Center(
  512. child: Row(
  513. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  514. children: < Widget > [
  515. Expanded(
  516. child: Row(
  517. mainAxisAlignment: MainAxisAlignment.start,
  518. children: < Widget > [
  519. Padding(
  520. padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
  521. child: Image.asset('lib/images/icon_dayinji.png',
  522. width: ScreenUtil.getInstance().setWidth(30),
  523. height: ScreenUtil.getInstance().setHeight(34),
  524. )
  525. ),
  526. Text('打印类型', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
  527. ],
  528. ),
  529. ),
  530. Row(
  531. mainAxisAlignment: MainAxisAlignment.end,
  532. children: < Widget > [
  533. Text(printType, style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(153, 153, 153, 1)), ),
  534. Icon(Icons.keyboard_arrow_right, color: Color.fromRGBO(199, 199, 204, 1))
  535. ],
  536. )
  537. ],
  538. ),
  539. ),
  540. )
  541. ],
  542. )
  543. ),
  544. onTap: () {
  545. showPickerModal(context, 'printType');
  546. },
  547. );
  548. }
  549. Widget _btList() {
  550. return Container(
  551. height: ScreenUtil.getInstance().setHeight(350),
  552. padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(30)),
  553. child: (btList.length > 0 && check == true) ? ListView.builder(
  554. itemCount: btList.length,
  555. itemBuilder: (BuildContext context, int index) {
  556. return _btItem(btList[index], index);
  557. },
  558. ) : check == true ? _searchBt() : Center()
  559. );
  560. }
  561. Widget _btItem(BtInfoObj obj, int index) {
  562. // print(obj.name);
  563. int tempIndex = obj.name.indexOf('||');
  564. String name = obj.name.substring(0, tempIndex);
  565. String btAddress = obj.name.substring(tempIndex + 2, obj.name.length);
  566. // print(btAddress);
  567. return InkWell(
  568. child:SafeArea(
  569. child:Padding(
  570. padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(30)),
  571. child: Row(
  572. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  573. children: < Widget > [
  574. Text(name),
  575. obj.showLoadStauts ? SizedBox(
  576. width: ScreenUtil.getInstance().setWidth(30),
  577. height: ScreenUtil.getInstance().setHeight(30),
  578. child: Image.asset('lib/images/loading.gif'),
  579. ) :
  580. CircleAvatar(
  581. backgroundColor: obj.connectStatus ? Color.fromRGBO(64, 98, 254, 1) : Colors.grey,
  582. radius: 6,
  583. )
  584. ],
  585. )
  586. ),
  587. ),
  588. onTap: () {
  589. if(btList[index].connectStatus) return;//已连接直接返回
  590. for(int i=0;i<btList.length;i++){
  591. if(btList[i].showLoadStauts==true){//任何一个蓝牙连接在加载中就直接返回
  592. return;
  593. }
  594. }
  595. bluetooth.connectBT(btAddress);//连接蓝牙
  596. bluetooth.cancelDiscoveryBT();//停止扫描
  597. setState(() {
  598. btList[index].showLoadStauts = true;
  599. });
  600. },
  601. );
  602. }
  603. Widget _searchBt() {
  604. return Center(
  605. child: Row(
  606. mainAxisAlignment: MainAxisAlignment.center,
  607. children: < Widget > [
  608. Text('正在搜索...'),
  609. SizedBox(
  610. width: ScreenUtil.getInstance().setWidth(45),
  611. height: ScreenUtil.getInstance().setHeight(45),
  612. child: Image.asset('lib/images/loading.gif'),
  613. )
  614. ],
  615. ),
  616. );
  617. }
  618. }