首页 > 解决方案 > 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(),
),
),
      

标签: flutterbluetooth-lowenergyflutter-layout

解决方案


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

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

推荐阅读