Browse Source

继续完成track功能

liuyuqi-dellpc 2 years ago
parent
commit
6e30759a45
3 changed files with 81 additions and 28 deletions
  1. 1 1
      lib/dio/login_dao.dart
  2. 79 26
      lib/pages/track_page.dart
  3. 1 1
      lib/views/contact_card.dart

+ 1 - 1
lib/dio/login_dao.dart

@@ -45,4 +45,4 @@ class LoginDao {
     Response response = await Dio().post(Api.getUserInfoUrl, data: params);
     return UserModel.fromJson(response.data);
   }
-}
+}

+ 79 - 26
lib/pages/track_page.dart

@@ -20,6 +20,7 @@ class _TrackPageState extends State<TrackPage> {
   List<dynamic> contactTraces = [];
   List<dynamic> contactTimes = [];
   List<dynamic> contactLocations = [];
+  FlutterBlue flutterBlue = FlutterBlue.instance;
 
   @override
   Widget build(BuildContext context) {
@@ -85,26 +86,7 @@ class _TrackPageState extends State<TrackPage> {
             ),
           ),
         ),
-        Padding(
-          padding: EdgeInsets.only(bottom: 200.0),
-          child: RaisedButton(
-            shape: RoundedRectangleBorder(
-                borderRadius: BorderRadius.circular(20.0)),
-            elevation: 5.0,
-            color: Colors.deepPurple[400],
-            onPressed: () async {
-              startTrack();
-            },
-            child: const Text(
-              '开始追踪',
-              style: TextStyle(
-                fontSize: 20.0,
-                fontWeight: FontWeight.bold,
-                color: Colors.white,
-              ),
-            ),
-          ),
-        ),
+        buildStartButton(),
         Expanded(
           flex: 2,
           child: Padding(
@@ -113,11 +95,9 @@ class _TrackPageState extends State<TrackPage> {
               itemBuilder: (context, index) {
                 return ContactCard(
                   imagePath: 'assets/images/head.jpg',
-                  email: contactTraces[index],
-                  infection: 'Not-Infected',
+                  infection: '健康',
                   contactUsername: contactTraces[index],
                   contactTime: contactTimes[index],
-                  contactLocation: contactLocations[index],
                 );
               },
               itemCount: contactTraces.length,
@@ -128,7 +108,81 @@ class _TrackPageState extends State<TrackPage> {
     );
   }
 
-  void startTrack() async {
+  StreamBuilder<bool> buildStartButton() {
+    return StreamBuilder<bool>(
+        stream: flutterBlue.isScanning,
+        initialData: false,
+        builder: (context, snapshot) {
+          if (snapshot.data) {
+            return Padding(
+              padding: EdgeInsets.only(bottom: 200.0),
+              child: RaisedButton(
+                shape: RoundedRectangleBorder(
+                    borderRadius: BorderRadius.circular(20.0)),
+                elevation: 5.0,
+                color: Colors.red,
+                onPressed: () async {
+                  startTrack(snapshot.data);
+                },
+                child: const Text(
+                  '停止追踪',
+                  style: TextStyle(
+                    fontSize: 20.0,
+                    fontWeight: FontWeight.bold,
+                    color: Colors.white,
+                  ),
+                ),
+              ),
+            );
+          } else {
+            return Padding(
+              padding: EdgeInsets.only(bottom: 200.0),
+              child: RaisedButton(
+                shape: RoundedRectangleBorder(
+                    borderRadius: BorderRadius.circular(20.0)),
+                elevation: 5.0,
+                color: Colors.deepPurple[400],
+                onPressed: () async {
+                  startTrack(snapshot.data);
+                },
+                child: const Text(
+                  '开始追踪',
+                  style: TextStyle(
+                    fontSize: 20.0,
+                    fontWeight: FontWeight.bold,
+                    color: Colors.white,
+                  ),
+                ),
+              ),
+            );
+          }
+        });
+  }
+
+  void startTrack(bool flag) async {
+    if (flag) {
+      flutterBlue.stopScan();
+    } else {
+      AppUtil.buildToast("正在搜索附近的人...");
+      flutterBlue.startScan(timeout: const Duration(seconds: 4));
+      //  扫描周围蓝牙设备
+      // List<BluetoothService> services = await device.discoverServices();
+      var subscription = flutterBlue.scanResults.listen((scanResult) {
+        for (ScanResult scan in scanResult) {
+          BluetoothDevice device = scan.device;
+          print("------------------------------" +
+              '${device.name} found! rssi: ${scan.rssi}dBm' +
+              device.id.toString());
+        }
+      });
+      // 上传周边设备到服务器
+
+      //  循环对每个设备检测安全性
+
+      //更新结果
+
+    }
+
     // try {
     //   bool a = await Nearby().startAdvertising(
     //     loggedInUser.email,
@@ -148,9 +202,8 @@ class _TrackPageState extends State<TrackPage> {
     // }
 
     discovery();
-    AppUtil.buildToast("正在搜索附近的人...");
     AppUtil.buildToast("追踪用户状态中...");
   }
 
-  void discovery() {}
+  void discovery() async {}
 }

+ 1 - 1
lib/views/contact_card.dart

@@ -42,7 +42,7 @@ class ContactCard extends StatelessWidget {
             context: context,
             builder: (builder) {
               return Padding(
-                padding: EdgeInsets.symmetric(vertical: 50.0, horizontal: 10.0),
+                padding: const EdgeInsets.symmetric(vertical: 50.0, horizontal: 10.0),
                 child: Column(
                   children: <Widget>[
                     BottomSheetText(