首页 > 解决方案 > 在 Flutter 中检查 BLE 是否关闭时显示底页

问题描述

在页面上检查蓝牙时,我想显示底页。该页面还包括底部导航栏。当它检查条件时,它会打印底部工作表,这意味着它将进入底部工作表小部件,但它没有显示在屏幕上。我也想在最后显示底部表。我怎样才能做到这一点?

代码:

import 'package:epicare/BluetoothAccessDenied.dart';
import 'package:epicare/BluetoothConnectBand.dart';
import 'package:epicare/BluetoothScanningDevices.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blue/flutter_blue.dart';

class BluetoothAllowScreen extends StatefulWidget {
  @override
  _BluetoothAllowScreenState createState() => _BluetoothAllowScreenState();
}

class _BluetoothAllowScreenState extends State<BluetoothAllowScreen> {
  FlutterBlue flutterBlue = FlutterBlue.instance;
  bool isBleOn;

  //Initialisation and listening to device state
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    //checks bluetooth current state
    FlutterBlue.instance.state.listen((state) {
      if (state == BluetoothState.on) {
        print("Bluetooth is on");
        isBleOn = true;
        //Alert user to turn on bluetooth.
      } else if (state == BluetoothState.off) {
        print("Bluetooth is off");
        isBleOn = false;
      }
      print(isBleOn);
      //asking to turn on BLE by showing bottom sheet
      if (isBleOn == false) {
        print("Requesting BLE to turn on");
        bottomSheet();
      }
      //if BLE is on then it should move to scanning devices page
      else {
        Navigator.pushAndRemoveUntil(
          context,
          MaterialPageRoute(
            builder: (context) {
              return BluetoothScanningDevices();
            },
          ),
          (route) => false,
        );
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Scaffold(
      appBar: AppBar(
        backgroundColor: const Color(0xffE5E0A1),
        elevation: 0,
        centerTitle: true,
        title: Text(
          " Connect Band",
          style: TextStyle(
            fontSize: 15.0,
            color: Colors.black,
            fontFamily: 'Montserrat',
            fontWeight: FontWeight.normal,
          ),
        ),
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back,
            color: Colors.black,
          ),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) {
                  return BluetoothConnectBand();
                },
              ),
            );
          },
        ),
        actions: [
          Padding(
            padding: EdgeInsets.only(right: 5.0),
            child: FlatButton(
              onPressed: () {
                showModalBottomSheet(
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.only(
                        topRight: Radius.circular(15),
                        topLeft: Radius.circular(15),
                      ),
                    ),
                    context: context,
                    builder: (builder) => bottomSheet());
              },
              child: Text(
                "Search",
                style: TextStyle(
                  fontSize: 15.0,
                  color: Colors.black,
                  fontWeight: FontWeight.normal,
                  fontFamily: 'Montserrat',
                ),
              ),
            ),
          )
        ],
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            height: size.height * 0.4,
            width: size.width,
            color: const Color(0xffE5E0A1),
            child: Image.asset(
              'assets/images/bluetooth.png',
            ),
          ),
        ],
      ),
    );
  }

  Widget bottomSheet() {
    print("BottomSheet");
    return Container(
      padding: EdgeInsets.symmetric(vertical: 15.0),
      height: 150.0,
      child: Column(
        children: [
          Text(
            "\nEpicare wants to turn on Bluetooth",
            style: TextStyle(
              color: Colors.black,
              fontFamily: 'Montserrat',
              fontWeight: FontWeight.w400,
              fontSize: 12.0,
            ),
            textAlign: TextAlign.center,
          ),
          SizedBox(
            height: 30.0,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              ButtonTheme(
                height: 40.0,
                minWidth: 150.0,
                child: FlatButton(
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(28),
                  ),
                  color: const Color(0xffE5E0A1),
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) {
                          return BluetoothAccessDenied();
                        },
                      ),
                    );
                  },
                  child: Text(
                    "Deny",
                    style: TextStyle(
                        fontSize: 14.0,
                        fontFamily: 'Montserrat',
                        color: Colors.black,
                        fontWeight: FontWeight.w700),
                  ),
                ),
              ),
              ButtonTheme(
                height: 40.0,
                minWidth: 150.0,
                child: FlatButton(
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(28),
                  ),
                  color: Colors.black,
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context){
                          return BluetoothScanningDevices();
                        },
                      ),
                    );
                  },
                  child: Text(
                    "Allow",
                    style: TextStyle(
                      fontSize: 14.0,
                      fontFamily: 'Montserrat',
                      color: const Color(0xffd4d411),
                      fontWeight: FontWeight.w700,
                    ),
                  ),
                ),
              ),
            ],
          )
        ],
      ),
    );
  }
}

我的屏幕如下所示:

在此处输入图像描述

如果 BLE 关闭,它应该像这样显示底部表。当我单击搜索按钮但不检查条件时显示此底部表。

在此处输入图像描述

请让我知道如何解决这个问题,因为我是 Flutter 的新手

标签: flutterdart

解决方案


推荐阅读