| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584 |
- import 'package:flutter/material.dart';
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- import 'my_page/revise_password_page.dart';
- import 'package:flutter_picker/flutter_picker.dart';
- import 'dart:convert';
- import '../util/bluetooth_utils.dart';
- import '../showDialog.dart';
- import '../login_page.dart';
- import '../util/api.dart';
- import '../util/models.dart';
- import '../util/session.dart';
- import '../util/util.dart';
- class MyPage extends StatefulWidget {
- final Widget child;
- MyPage({
- Key key,
- this.child
- }): super(key: key);
- _MyPageState createState() => _MyPageState();
- }
- class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
- bool check = false;
- String _siteName = "请选择站点";
- List < String > stationList = [];
- Station stationObj;
- String name = '物流';
- BluetoothUtils bluetooth = BluetoothUtils();
- List < BtInfoObj > btList = [];
- Set btSet = Set();
- int publicIndex;
- List printTypeList = ["全部", "配件商", "修理厂", "司机", "货物", "货物外", "不打印"];
- static
- var printTypeMap = {
- "全部": "1",
- "配件商": "2",
- "修理厂": "3",
- "司机": "4",
- "货物": "5",
- "货物外": "6",
- "不打印": "7",
- };
- static
- var printTypeMapSecond = {
- "1": "全部",
- "2": "配件商",
- "3": "修理厂",
- "4": "司机",
- "5": "货物",
- "6": "货物外",
- "7": "不打印",
- };
- String printType = '全部';
- @override
- bool get wantKeepAlive => true;
- @override
- void initState() {
- super.initState();
- getStationList().then((resp) {
- if (isNotError(context, resp)) {
- stationObj = Station.fromJson(resp.data);
- getKey('stationId').then((value) {
- List < String > tempList = [];
- for (int i = 0; i < stationObj.data.length; i++) {
- tempList.add(stationObj.data[i].name.toString());
- if (stationObj.data[i].id.toString() == value) {
- _siteName = stationObj.data[i].name.toString();
- }
- }
- getKey('nickName').then((value) {
- setState(() {
- stationList = tempList;
- name = value;
- });
- });
- getKey('printType').then((value) {
- printType = printTypeMapSecond[value];
- });
- });
- }
- });
- bluetooth.getEchannel().receiveBroadcastStream().listen(onEvent, onError: onError);
- getKey('check').then((value) {
- if (value == null) {
- setState(() {
- check = false;
- });
- } else if (value == 'false') {
- setState(() {
- check = false;
- });
- } else if (value == 'true') {
- setState(() {
- check = true;
- });
- }
- });
- getKey('btList').then((value) {
- print(value);
- if (value != '' && check == true) {
- List tempList = [];
- for (int i = 0; i < 999; i++) {
- if (value.indexOf('&') != -1) {
- int index = value.indexOf('&');
- tempList.add(value.substring(0, index));
- value = value.substring(index + 1, value.length);
- } else {
- break;
- }
- }
- for (int i = 0; i < tempList.length; i++) {
- BtInfoObj tempObj = BtInfoObj(name: tempList[i], showLoadStauts: false, connectStatus: false);
- setState(() {
- btList.add(tempObj);
- });
- }
- }
- });
- }
- onEvent(Object obj) {
- print('进入onEvent回调${obj}');
- if (obj.toString().indexOf('||') != -1) { //过滤开始搜索和结束搜索
- btSet.add(obj.toString()); //去重
- List tempBtList = btSet.toList();
- List<BtInfoObj> tempBtList2 = [];
- for (int i = 0; i < tempBtList.length; i++) {
- tempBtList2.add(
- BtInfoObj(name: tempBtList[i], showLoadStauts: false, connectStatus: false)
- );
- }
- setState(() {
- btList =tempBtList2;
- });
- print('设置btList完成');
- String tempStr = '';
- for (int i = 0; i < btList.length; i++) {
- tempStr += '${btList[i].name}&';
- }
- print('设置临时字符串');
- print(tempStr);
- setKey('btList', tempStr);
- }
- }
- onError(Object obj) {
- print(obj.toString());
- }
- @override
- Widget build(BuildContext context) {
- return Container(
- child: Scaffold(
- appBar: AppBar(
- elevation: 0,
- title: Text('我的',
- style: TextStyle(
- fontSize: ScreenUtil.getInstance().setSp(36)
- ), ),
- centerTitle: true,
- backgroundColor: Color.fromRGBO(64, 98, 254, 1),
- actions: < Widget > [
- InkWell(
- child: Container(
- padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(30)),
- child: Center(
- child: Text('退出'),
- )
- ),
- onTap: () {
- showDialog < Null > (
- context: context, //BuildContext对象
- barrierDismissible: false,
- builder: (BuildContext context) {
- return new LoadingDialog( //调用对话框
- text: '确认退出?', childCallback: (val) {
- if (val) {
- clearKey();
- Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) {
- return LoginPage();
- }));
- } else {
- return;
- }
- },
- );
- });
- },
- )
- ],
- ),
- body: Container(
- color: Color.fromRGBO(246, 246, 254, 1),
- child: Column(
- children: < Widget > [
- _avatarWidget(),
- _funWrapWidget(context),
- ],
- ),
- ),
- )
- );
- }
- Widget _avatarWidget() {
- return Container(
- color: Color.fromRGBO(64, 98, 254, 1),
- padding: EdgeInsets.fromLTRB(0, ScreenUtil.getInstance().setHeight(59), 0, ScreenUtil.getInstance().setHeight(67), ),
- child: Column(
- children: < Widget > [
- Center(
- child: Padding(
- padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setWidth(24), ),
- child: ClipOval(
- child: Image.asset('lib/images/icon_logo.png',
- fit: BoxFit.cover,
- width: ScreenUtil.getInstance().setWidth(120),
- height: ScreenUtil.getInstance().setWidth(120), ),
- )
- )
- ),
- Center(child: Text(name, style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(255, 255, 255, 1)), ), )
- ],
- ),
- );
- }
- Widget _funWrapWidget(BuildContext context) {
- return Container(
- padding: EdgeInsets.only(top: ScreenUtil.getInstance().setHeight(20)),
- child: Column(
- children: < Widget > [
- _switchSite(context),
- _passWordWidget(),
- _linkPrint(),
- _printType(),
- _btList()
- ],
- ),
- );
- }
- Widget _switchSite(BuildContext context) {
- return InkWell(
- child: Container(
- height: ScreenUtil.getInstance().setHeight(90),
- color: Colors.white,
- padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
- child: Column(
- children: < Widget > [
- Expanded(
- child: Center(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: < Widget > [
- Expanded(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.start,
- children: < Widget > [
- Padding(
- padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
- child: Image.asset('lib/images/icon_zhandianss.png',
- width: ScreenUtil.getInstance().setWidth(30),
- height: ScreenUtil.getInstance().setHeight(34),
- )
- ),
- Text('站点切换', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
- ],
- ),
- ),
- Expanded(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.end,
- children: < Widget > [
- Text(_siteName, style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(153, 153, 153, 1)), ),
- Icon(Icons.keyboard_arrow_right, color: Color.fromRGBO(199, 199, 204, 1))
- ],
- ),
- ),
- ],
- ),
- ),
- ),
- Expanded(
- flex: 0,
- child: _underLine(),
- )
- ],
- )
- ),
- onTap: () {
- // showDialog < Null > (
- // context: context, //BuildContext对象
- // barrierDismissible: false,
- // builder: (BuildContext context) {
- // return SiteDialog( //调用对话框
- // );
- // });
- showPickerModal(context, 'station');
- },
- );
- }
- showPickerModal(BuildContext context, String type) {
- var list;
- if (type == 'station') {
- list = stationList;
- } else if (type == 'printType') {
- list = printTypeList;
- }
- new Picker(
- adapter: PickerDataAdapter < String > (pickerdata: list),
- height: ScreenUtil.getInstance().setHeight(450),
- confirmText: '确定',
- confirmTextStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(64, 98, 254, 1)),
- cancelText: '取消',
- cancelTextStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(153, 153, 153, 1)),
- headercolor: Color.fromRGBO(245, 245, 245, 1),
- itemExtent: ScreenUtil.getInstance().setHeight(80),
- textStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(42, 42, 42, 1)),
- changeToFirst: true,
- hideHeader: false,
- onConfirm: (Picker picker, List value) {
- if (type == 'station') {
- setState(() {
- _siteName = stationObj.data[value[0]].name;
- });
- setKey('stationId', stationObj.data[value[0]].id.toString());
- } else if (type == "printType") {
- setState(() {
- printType = printTypeList[value[0]];
- });
- setKey('printType', printTypeMap[printTypeList[value[0]]]);
- getKey('printType').then((value) {
- print(value);
- });
- }
- }
- ).showModal(this.context); //_scaffoldKey.currentState);
- }
- Widget _underLine() {
- return Container(
- height: ScreenUtil.getInstance().setHeight(1),
- color: Color.fromRGBO(223, 223, 223, 1),
- );
- }
- Widget _passWordWidget() {
- return
- GestureDetector(
- child: Container(
- height: ScreenUtil.getInstance().setHeight(90),
- color: Colors.white,
- padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
- child: Column(
- children: < Widget > [
- Expanded(
- child: Center(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: < Widget > [
- Expanded(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.start,
- children: < Widget > [
- Padding(
- padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
- child: Image.asset('lib/images/icon_mimas.png',
- width: ScreenUtil.getInstance().setWidth(30),
- height: ScreenUtil.getInstance().setHeight(34),
- )
- ),
- Text('修改密码', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
- ],
- ),
- ),
- Icon(Icons.keyboard_arrow_right, color: Color.fromRGBO(199, 199, 204, 1))
- ],
- ),
- ),
- )
- ],
- )
- ),
- onTap: () {
- Navigator.push(context, MaterialPageRoute(builder: (context) {
- return RevisePasswordPage();
- }));
- },
- );
- }
- Widget _linkPrint() {
- return Container(
- height: ScreenUtil.getInstance().setHeight(90),
- color: Colors.white,
- padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
- margin: EdgeInsets.only(top: ScreenUtil.getInstance().setHeight(20)),
- child: Column(
- children: < Widget > [
- Expanded(
- child: Center(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: < Widget > [
- Expanded(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.start,
- children: < Widget > [
- Padding(
- padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
- child: Image.asset('lib/images/icon_dayinji.png',
- width: ScreenUtil.getInstance().setWidth(30),
- height: ScreenUtil.getInstance().setHeight(34),
- )
- ),
- Text('打印机连接', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
- ],
- ),
- ),
- Switch(
- value: check,
- activeColor: Colors.blue,
- onChanged: (bool val) {
- if (val) {
- bluetooth.isBTDevice().then((resp) {
- if (resp) {
- bluetooth.isEnableBluetooth().then((bool resp) { //打开蓝牙
- setState(() {
- check = true;
- });
- setKey('check', 'true');
- bluetooth.scanBT(); //
- });
- }
- });
- } else {
- bluetooth.disConnnectBT(); //关闭蓝牙
- setState(() {
- check = false;
- btList.clear();
- });
- setKey('check', 'false');
- }
- },
- )
- ],
- ),
- ),
- ),
- _underLine()
- ],
- )
- );
- }
- Widget _printType() {
- return InkWell(
- child: Container(
- height: ScreenUtil.getInstance().setHeight(90),
- color: Colors.white,
- padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(30), 0, 0, 0),
- child: Column(
- children: < Widget > [
- Expanded(
- child: Center(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: < Widget > [
- Expanded(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.start,
- children: < Widget > [
- Padding(
- padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(22)),
- child: Image.asset('lib/images/icon_dayinji.png',
- width: ScreenUtil.getInstance().setWidth(30),
- height: ScreenUtil.getInstance().setHeight(34),
- )
- ),
- Text('打印类型', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(74, 74, 74, 1)), ),
- ],
- ),
- ),
- Row(
- mainAxisAlignment: MainAxisAlignment.end,
- children: < Widget > [
- Text(printType, style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30), color: Color.fromRGBO(153, 153, 153, 1)), ),
- Icon(Icons.keyboard_arrow_right, color: Color.fromRGBO(199, 199, 204, 1))
- ],
- )
- ],
- ),
- ),
- )
- ],
- )
- ),
- onTap: () {
- showPickerModal(context, 'printType');
- },
- );
- }
- Widget _btList() {
- return Container(
- height: ScreenUtil.getInstance().setHeight(350),
- padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(30)),
- child: (btList.length > 0 && check == true) ? ListView.builder(
- itemCount: btList.length,
- itemBuilder: (BuildContext context, int index) {
- return _btItem(btList[index], index);
- },
- ) : check == true ? _searchBt() : Center()
- );
- }
- Widget _btItem(BtInfoObj obj, int index) {
- // print(obj.name);
- int tempIndex = obj.name.indexOf('||');
- String name = obj.name.substring(0, tempIndex);
- String btAddress = obj.name.substring(tempIndex + 2, obj.name.length);
- // print(btAddress);
- return InkWell(
- child:SafeArea(
- child:Padding(
- padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(30)),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: < Widget > [
- Text(name),
- obj.showLoadStauts ? SizedBox(
- width: ScreenUtil.getInstance().setWidth(35),
- height: ScreenUtil.getInstance().setHeight(35),
- child: Image.asset('lib/images/loading.gif'),
- ) :
- CircleAvatar(
- backgroundColor: obj.connectStatus ? Color.fromRGBO(64, 98, 254, 1) : Colors.grey,
- radius: 6,
- )
- ],
- )
- ),
- ),
- onTap: () {
- //bluetooth.connnectBT(btAddress);
- bluetooth.connectBT(btAddress);
- setState(() {
- btList[index].showLoadStauts = true;
- // publicIndex =
- });
-
- },
- );
- }
- Widget _searchBt() {
- return Center(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: < Widget > [
- Text('正在搜索...'),
- SizedBox(
- width: ScreenUtil.getInstance().setWidth(45),
- height: ScreenUtil.getInstance().setHeight(45),
- child: Image.asset('lib/images/loading.gif'),
- )
- ],
- ),
- );
- }
- }
|