flutter - Flutter:如何在 ListView 中有条件地重复 showDialog
问题描述
我正在使用 flutter_reactive_ble_example 通过修改文件 device_list.dart 连接到我的蓝牙模块。
我想知道如果密码错误,我该如何重新提示用户。
我对颤振相当陌生,如果需要,请询问更多详细信息。
这是我目前拥有的代码片段:
Flexible(
child: ListView(
children: widget.scannerState.discoveredDevices
.map(
(device) => ListTile(
title: Text(tile.name),
subtitle: Text("${tile.name}\n: ${tile.sub}"),
leading: const ConnectIcon(),
onTap: () async {
//stop the scan
widget.stopScan();
//connect to the device
await widget.deviceConn.connect(device.id);
//prompt user for password
final inputData = await showDialog(
context: context,
barrierDismissible:
false, // prevent user from closing the dialog by pressing outside the dialog
builder: (_) {
String userData = "";
return AlertDialog(
title: new Text("Enter Password"),
content: new TextField(
onChanged: (value) {
userData = value;
},
),
actions: <Widget>[
ElevatedButton(
child: Text('Ok'),
onPressed: () async {
//on press subscribe and send the password
response = await ble.subscribeToCharacteristic(characteristic);
//if data failure check, how do I reshow this showDialog??
response.listen((event) {
if(event == 1){
//if return 1, password correct
Navigator.of(context).pop(userData);
}else{
//if not reshow Dialog
//howw?
}
}
//send password
ble.writeCharacteristicWithoutResponse(characteristic, value: userData);
},
)
],
);
},
);
Navigator.of(context).pop(
inputData); // pass data back to the previous page
},
),
)
.toList(),
),
),
解决方案
你可以使用我认为的递归,这里是一个例子
Future _showPasswordDialog(){
return showDialog(
context: context,
barrierDismissible:
false, // prevent user from closing the dialog by pressing outside the dialog
builder: (_) {
String userData = "";
return AlertDialog(
title: new Text("Enter Password"),
content: new TextField(
onChanged: (value) {
userData = value;
},
),
actions: <Widget>[
ElevatedButton(
child: Text('Ok'),
onPressed: () async {
//on press subscribe and send the password
response = await ble.subscribeToCharacteristic(characteristic);
//if data failure check, how do I reshow this showDialog??
response.listen((event) {
if(event == 1){
//if return 1, password correct
Navigator.of(context).pop(userData);
}else{
//if not reshow Dialog
//howw?
Navigator.of(context).pop();
_showPasswordDialog();
}
}
//send password
ble.writeCharacteristicWithoutResponse(characteristic, value: userData);
},
)
],
);
},
);
}
推荐阅读
- javascript - MongoDB驱动程序findOneAndUpdate方法,如何返回两个值
- elasticsearch - 如何安装 stackdriver elasticsearch 插件来监控在 GKE 上的 k8s v1.11 内运行的 ES 实例?
- scala - 如何在没有重复的情况下找到元组之间的所有可能组合
- python-3.x - Marvel API 调用中的哈希、时间戳和密钥组合无效
- php - Laravel SQLSTATE[HY000] [2002] 没有这样的文件或目录
- vue.js - Vue-Routes 重定向不起作用,beforeEnter 再次渲染 App 组件
- javascript - 在 javascript 中的元素中添加/删除类名之间切换
- java - 如何在 Android 上将 Java XPath 与 KML 文件和命名空间一起使用
- spring - HV000030:找不到约束的验证器(休眠验证器)
- typescript - 如何使用具有元组类型的通用休息元素?