فهرست منبع

蓝牙打开与关闭的页面交互

daxu0403 7 سال پیش
والد
کامیت
ecc1acad96
4فایلهای تغییر یافته به همراه125 افزوده شده و 50 حذف شده
  1. 80 49
      lib/pages/my_page.dart
  2. 1 1
      lib/pages/order_taking_page.dart
  3. 9 0
      lib/util/bluetooth_utils.dart
  4. 35 0
      lib/util/models.dart

+ 80 - 49
lib/pages/my_page.dart

@@ -2,6 +2,7 @@ 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';
@@ -29,8 +30,8 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
   Station stationObj;
   String name = '物流';
   BluetoothUtils bluetooth = BluetoothUtils();
-  List<String> btList = [];
-  Set<String> btSet = Set();
+  List < BtInfoObj > btList = [];
+  Set < BtInfoObj > btSet = Set();
   List printTypeList = ["全部", "配件商", "修理厂", "司机", "货物", "货物外", "不打印"];
   static
   var printTypeMap = {
@@ -84,17 +85,32 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
     });
 
     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;
+        });
+      }
+      print(check);
+    });
   }
 
   onEvent(Object obj) {
-    setState(() {
-      if(obj.toString().indexOf('||')!=-1){
-        int index = obj.toString().indexOf('||');
-        String temp = obj.toString().substring(0,index);
-        btSet.add(temp);
-        btList =btSet.toList();
-      }
-    });
+    if (obj.toString().indexOf('||') != -1) { //过滤开始搜索和结束搜索
+      BtInfoObj tempObj = BtInfoObj(name: obj.toString(), showLoadStauts: false, connectStatus: false);
+      btSet.add(tempObj);
+      setState(() {
+        btList = btSet.toList();
+      });
+    }
   }
 
   onError(Object obj) {
@@ -148,7 +164,7 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
           child: Column(
             children: < Widget > [
               _avatarWidget(),
-              _funWrapWidget(check, context),
+              _funWrapWidget(context),
             ],
           ),
         ),
@@ -180,14 +196,14 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
     );
   }
 
-  Widget _funWrapWidget(bool check, BuildContext context) {
+  Widget _funWrapWidget(BuildContext context) {
     return Container(
       padding: EdgeInsets.only(top: ScreenUtil.getInstance().setHeight(20)),
       child: Column(
         children: < Widget > [
           _switchSite(context),
           _passWordWidget(),
-          _linkPrint(check),
+          _linkPrint(),
           _printType(),
           _btList()
         ],
@@ -346,7 +362,7 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
     );
   }
 
-  Widget _linkPrint(bool check) {
+  Widget _linkPrint() {
     return Container(
       height: ScreenUtil.getInstance().setHeight(90),
       color: Colors.white,
@@ -375,30 +391,31 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
                     ),
                   ),
                   Switch(
-                    value: this.check,
+                    value: check,
                     activeColor: Colors.blue,
                     onChanged: (bool val) {
-                      if(val){
+                      if (val) {
                         bluetooth.isBTDevice().then((resp) {
-                        if (resp) {
-                          bluetooth.isEnableBluetooth().then((bool resp) {
-                            this.setState(() {
-                              this.check = resp;
+                          if (resp) {
+                            bluetooth.isEnableBluetooth().then((bool resp) { //打开蓝牙
+                              setState(() {
+                                check = true;
+                              });
+                              setKey('check', 'true');
+                              bluetooth.scanBT(); //
                             });
-                            bluetooth.scanBT(); //
-                          });
 
-                        }
-                      });
-                      }else{
-                        bluetooth.disConnnectBT();
+                          }
+                        });
+                      } else {
+                        bluetooth.disConnnectBT(); //关闭蓝牙
                         setState(() {
-                          this.check = val;
+                          check = false;
                           btList.clear();
-                          btSet.clear();
                         });
+                        setKey('check', 'false');
                       }
-                      
+
                     },
                   )
                 ],
@@ -463,19 +480,21 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
     return Container(
       height: ScreenUtil.getInstance().setHeight(350),
       padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(30)),
-      child: ListView.builder(
-        itemCount:btList.length,
+      child: (btList.length > 0 && check==true) ? ListView.builder(
+        itemCount: btList.length,
         itemBuilder: (BuildContext context, int index) {
-         
-          return _btItem(btList[index]);
-         
-         
+          return _btItem(btList[index], index);
         },
-      )
+      ) : check==true?_searchBt():Center()
     );
   }
 
-  Widget _btItem(String name) {
+  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: Padding(
         padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(30)),
@@ -483,29 +502,41 @@ class _MyPageState extends State < MyPage > with AutomaticKeepAliveClientMixin {
           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: Colors.grey,
+              backgroundColor: obj.connectStatus ? Color.fromRGBO(64, 98, 254, 1) : Colors.grey,
               radius: 6,
             )
           ],
         )
       ),
-      onTap: (){
-        print('链接');
+      onTap: () {
+        //bluetooth.connnectBT(btAddress);
+        setState(() {
+          btList[index].showLoadStauts = true;
+        });
       },
     );
   }
 
-  Widget _searchBt(){
-    return InkWell(
-      child: Center(
-        child: Container(
-          width: 160,
-          height: 30,
-          color: Colors.blue,
-          child: Text('扫描中'),
-        ),
+  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'),
+          )
+        ],
       ),
     );
   }
+
 }

+ 1 - 1
lib/pages/order_taking_page.dart

@@ -219,7 +219,7 @@ class _OrderTakingPageState extends State < OrderTakingPage > with AutomaticKeep
       child: Container(
         height: ScreenUtil.getInstance().setHeight(80),
         decoration: BoxDecoration(
-          border: BorderDirectional(bottom: BorderSide(color: Colors.white, width: 0.5))
+          border: BorderDirectional(bottom: BorderSide(color: Colors.white, width: 0.1))
         ),
         child: Row(
           children: < Widget > [

+ 9 - 0
lib/util/bluetooth_utils.dart

@@ -50,6 +50,14 @@ class BluetoothUtils {
     }
   }
 
+  Future cancelDiscoveryBT() async {
+    try {
+      return await mChannel.invokeMethod('cancelDiscoveryBT');
+    } on PlatformException catch (e) {
+      print("$e");
+    }
+  }
+
   Future printOrder(Map<String,String> pum,int type)async{
      try {
       Map<String,Object> map = {"pum":pum,"type": type};
@@ -61,6 +69,7 @@ class BluetoothUtils {
 
   }
 
+
    EventChannel getEchannel(){
      return eChannel;
 

+ 35 - 0
lib/util/models.dart

@@ -774,4 +774,39 @@ class CheckToken{
       data:json['data'],
     );
   }
+}
+
+//蓝牙信息缓存对象
+class BtInfoList{
+  List<BtInfoObj> btList;
+
+  BtInfoList({
+    this.btList
+  });
+
+  factory BtInfoList.fromJson(List json){
+    List tempList = json.map((i) => BtInfoObj.fromJson(i)).toList();
+    return BtInfoList(
+      btList:tempList
+    );
+  }
+}
+class BtInfoObj{
+  String name;
+  bool showLoadStauts;
+  bool connectStatus;
+  
+  BtInfoObj({
+    this.name,
+    this.showLoadStauts,
+    this.connectStatus
+  });
+
+  factory BtInfoObj.fromJson(Map<String,dynamic> json){
+    return BtInfoObj(
+      name:json['name'],
+      showLoadStauts:json['showLoadStauts'],
+      connectStatus:json['connectStatus'],
+    );
+  }
 }