my_page.dart 18 KB

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