flutter - 在 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 的新手
解决方案
推荐阅读
- c - BMP 图像,每个像素的位数问题
- ruby-on-rails - Ruby on Rails - 当用户可以是 Task.creator 或 Task.owner 时的 User.tasks
- java - 执行包含多个 SQL 语句但数据已更新的字符串时出错
- java - (com.basistech.m2e.code.quality.checkstyle.feature.feature.group 1.0.0.201705301746) 需要 'net.sf.eclipsecs.core 5.2.0' 但找不到
- r - R:序列数据的整齐聚合和阶梯函数的可视化
- python - 从有序列表中获取随机排序的输出
- angular - 此导入已列入黑名单,请改为导入子模块
- django - Django Rest Framework:如何将数据传递给嵌套的序列化程序并仅在自定义验证后创建对象
- python - python matplotlib 将字符串变量转换为轴属性
- c - 为什么 realloc 只复制部分数据?