Browse Source

增加蓝牙是否启动检测

liuyuqi-dellpc 2 years ago
parent
commit
77950f9070
2 changed files with 49 additions and 2 deletions
  1. 30 0
      lib/pages/bluetooth_off_page.dart
  2. 19 2
      lib/pages/track_page.dart

+ 30 - 0
lib/pages/bluetooth_off_page.dart

@@ -0,0 +1,30 @@
+import 'package:flutter/material.dart';
+import 'package:flutter_blue/flutter_blue.dart';
+
+class BluetoothOffPage extends StatelessWidget {
+  final BluetoothState state;
+
+  BluetoothOffPage({Key key, BluetoothState this.state}) : super(key: key);
+
+  @override
+  Widget build(BuildContext context) {
+    return Container(
+      color: Colors.blue,
+      child: Center(
+        child: Column(
+          mainAxisSize: MainAxisSize.min,
+          children: <Widget>[
+            const Icon(
+              Icons.bluetooth_disabled,
+              size: 200.0,
+              color: Colors.white54,
+            ),
+            Text(
+              '蓝牙适配器: ${state != null ? state.toString().substring(15) : '不可用'}.',
+            ),
+          ],
+        ),
+      ),
+    );
+  }
+}

+ 19 - 2
lib/pages/track_page.dart

@@ -1,7 +1,11 @@
 import 'package:flutter/material.dart';
+import 'package:flutter_blue/flutter_blue.dart';
 import 'package:flutter_tracker/utils/app_util.dart';
 import 'package:flutter_tracker/views/contact_card.dart';
-/// Description: 
+
+import 'bluetooth_off_page.dart';
+
+/// Description:
 /// Time       : 2021年12月03日 Friday
 /// Author     : liuyuqi.gov@msncn
 class TrackPage extends StatefulWidget {
@@ -19,11 +23,24 @@ class _TrackPageState extends State<TrackPage> {
 
   @override
   Widget build(BuildContext context) {
+    return StreamBuilder<BluetoothState>(
+        stream: FlutterBlue.instance.state,
+        initialData: BluetoothState.unknown,
+        builder: (c, snapshot) {
+          final state = snapshot.data;
+          if (state == BluetoothState.on) {
+            return buildContent();
+          }
+          return BluetoothOffPage(state: state);
+        });
+  }
+
+  Column buildContent() {
     return Column(
       children: <Widget>[
         Expanded(
           child: Padding(
-            padding: EdgeInsets.only(
+            padding: const EdgeInsets.only(
               left: 25.0,
               right: 25.0,
               bottom: 10.0,