Flutter 颤振:如何在ListView中有条件地重复showDialog

Flutter 颤振:如何在ListView中有条件地重复showDialog,flutter,bluetooth-lowenergy,flutter-layout,Flutter,Bluetooth Lowenergy,Flutter Layout,我正在使用flatter_reactive_ble_示例通过修改文件device_list.dart连接到我的蓝牙模块 我想知道,如果密码错误,如何重新提示用户 我对颤振还比较陌生,如果需要,请询问更多细节 以下是我目前拥有的代码片段: 我想你可以使用递归,这里是一个例子 Future _showPasswordDialog(){ return showDialog( context: context, barrierDismissibl

我正在使用flatter_reactive_ble_示例通过修改文件device_list.dart连接到我的蓝牙模块

我想知道,如果密码错误,如何重新提示用户

我对颤振还比较陌生,如果需要,请询问更多细节

以下是我目前拥有的代码片段:


我想你可以使用递归,这里是一个例子

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);
                      },
                    )
                  ],
                );
              },
            );
}

我想你可以使用递归,这里是一个例子

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);
                      },
                    )
                  ],
                );
              },
            );
}

将警报提示分离为另一个函数,如果登录成功,则返回用户详细信息,否则返回null

Future<String> promptAlert(BuildContext context){

 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{                                  
                               Navigator.of(context).pop();
                            }                                                                
                        }
                        
                        //send password
                        ble.writeCharacteristicWithoutResponse(characteristic, value: userData);
                      },
                    )
                  ],
                );
              },
            );

}
如果要显示snackbar和延迟警报

                  ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                    duration: Duration(seconds: 2),
                    content: Text('Login Failed Try again'),
                  ));
                  Future.delayed(
                      Duration(seconds: 2), () => promptAlert(context));
              

将警报提示分离为另一个函数,如果登录成功,则返回用户详细信息,否则返回null

Future<String> promptAlert(BuildContext context){

 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{                                  
                               Navigator.of(context).pop();
                            }                                                                
                        }
                        
                        //send password
                        ble.writeCharacteristicWithoutResponse(characteristic, value: userData);
                      },
                    )
                  ],
                );
              },
            );

}
如果要显示snackbar和延迟警报

                  ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                    duration: Duration(seconds: 2),
                    content: Text('Login Failed Try again'),
                  ));
                  Future.delayed(
                      Duration(seconds: 2), () => promptAlert(context));
              

您好,这是什么?我想添加一个延迟几秒的snackbar并重新启动它?您可以在提示之前使用snack,即在循环内部。我在isUnlocked=等待promptAlertcontext…之前尝试了这个showsnackbar。。。。它确实显示了,但是由于提示是一个覆盖,snackbar不在焦点上,有没有办法显示snackbar几秒钟呢,reprompt?您可以在显示提示时使用Future delayed。我已经更新了代码。在更新的代码中,您使用了Future和字符串user=…,但返回的类型是showdialog。。。可能吗?由于返回类型不是字符串,这不会引发错误吗?嗨,这是什么?我想添加一个延迟数秒的snackbar并重新启动它?您可以在提示之前使用snack,即在循环内部。我在isunlock=wait promptAlertcontext之前尝试了这个showsackbar。。。。它确实显示了,但是由于提示是一个覆盖,snackbar不在焦点上,有没有办法显示snackbar几秒钟呢,reprompt?您可以在显示提示时使用Future delayed。我已经更新了代码。在更新的代码中,您使用了Future和字符串user=…,但返回的类型是showdialog。。。可能吗?因为返回类型不是字符串,所以不会引发错误吗?