【问题标题】:How do I implement CircularProgressIndicator dialog and '"Message sent!" Dialog in Flutter?如何实现 CircularProgressIndicator 对话框和“已发送消息!” Flutter 中的对话框?
【发布时间】:2020-10-03 08:00:15
【问题描述】:

我是 Flutter 的新手,我想获得有关如何实现 CircularProgressIndicator 对话框和“已发送消息!”的帮助按下平面按钮时的对话框。在这种情况下,我正在为用户实现一个联系表单,以通过 FirebaseFirestore.instance 发送他们的消息。我设置 bool _isLoading 并使用它来触发 CircularProgressIndicator 的初始方法只是在发送消息后将其设置为 false 时它不响应。结果,我得到了一个 CircularProgressIndicator,即使在确认消息已发送后也不会停止。谁能帮我解决这个问题?

                            FlatButton(
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(20),
                              ),
                              color: Colors.pinkAccent,
                              onPressed: () {
                                setState(() {
                                  _isLoading = true;
                                });
                                if (_isLoading) {
                                  showDialog(
                                      barrierDismissible: true,
                                      context: context,
                                      builder: (BuildContext context) {
                                        return Dialog(
                                          child: Container(
                                            height: _height * 0.09495,
                                            width: _width * 0.17644444,
                                            padding: EdgeInsets.only(
                                              top: 15,
                                            ),
                                            child: Center(
                                              child: Column(
                                                children: [
                                                  CircularProgressIndicator(),
                                                  Text('Please wait...'),
                                                ],
                                              ),
                                            ),
                                          ),
                                        );
                                      });
                                  if (_fbKey.currentState.saveAndValidate()) {
                                    FirebaseFirestore.instance
                                        .collection('message')
                                        .doc()
                                        .set({
                                      'name': _fbKey.currentState.value['name'],
                                      'email':
                                          _fbKey.currentState.value['email'],
                                      'details':
                                          _fbKey.currentState.value['details'],
                                      'category':
                                          _fbKey.currentState.value['category'],
                                      'created': FieldValue.serverTimestamp(),
                                    }).then((_) {
                                      print('Sent!');
                                    }).catchError((error) {
                                      print("Failed to send message: $error");
                                    });
                                  }
                                } else {
                                  showDialog(
                                      barrierColor: Colors.transparent,
                                      barrierDismissible: true,
                                      context: context,
                                      builder: (BuildContext context) {
                                        return Dialog(
                                          child: Container(
                                            height: _height * 0.09495,
                                            width: _width * 0.17644444,
                                            child: Center(
                                              child: Text(
                                                'Message sent2!',
                                                style: TextStyle(
                                                  color: Colors.green,
                                                  fontSize: 16,
                                                ),
                                              ),
                                            ),
                                          ),
                                        );
                                      });
                                }
                                setState(() {
                                  _isLoading = false;
                                });
                              },
                            ),
'''

【问题讨论】:

  • 那之后为什么不设置_isLoading的状态呢?
  • 我已经尝试了你的建议,但我仍然得到一个连续的 CircularProgressIndicator。代码有问题吗?
  • 为什么不试试逐行调试,看看是不是到了isLoading的值发生变化的那一行。如果不调试,那么惰性方法会像 print('i am here); 这样打印。上面的 isLoading = false;
  • 我找到了解决方案。谢谢!
  • 没问题!来帮忙!

标签: flutter dart flutter-web


【解决方案1】:

您实际上不需要布尔值来显示和弹出对话框。如果我们了解 asynchronous 函数和 await 调用,则可以轻松实现此逻辑。验证表单后,我们将显示加载对话框,然后等待 firebase 查询。执行 firebase 查询时,我们将查看它是否已捕获错误或使用 try catch 块成功执行。如果它捕获到错误,我们将弹出对话框,打印错误,然后调用 return 以终止我们的方法。如果它没有发现任何错误,它将继续正常执行。我们将弹出此对话框并显示消息发送对话框。

FlatButton(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(20),
          ),
          color: Colors.pinkAccent,
          onPressed: () async {
    
            if (_fbKey.currentState.saveAndValidate()) {
              showDialog(
                  barrierDismissible: true,
                  context: context,
                  builder: (BuildContext context) {
                    return Dialog(
                      child: Container(
                        height: _height * 0.09495,
                        width: _width * 0.17644444,
                        padding: EdgeInsets.only(
                          top: 15,
                        ),
                        child: Center(
                          child: Column(
                            children: [
                              CircularProgressIndicator(),
                              Text('Please wait...'),
                            ],
                          ),
                        ),
                      ),
                    );
                  });
    
              try{
                await FirebaseFirestore.instance
                    .collection('message')
                    .doc()
                    .set({
                  'name': _fbKey.currentState.value['name'],
                  'email':
                  _fbKey.currentState.value['email'],
                  'details':
                  _fbKey.currentState.value['details'],
                  'category':
                  _fbKey.currentState.value['category'],
                  'created': FieldValue.serverTimestamp(),
                });
              } catch (e){
                //Pop loading dialog because error has occured. We will print error and call return so our function
                //should be terminated 
                Navigator.pop(context);
                print("Exception occured");
                return;
              }
              
              //Pop loading dialog because query is executed and now we want to show message sent dialog
              Navigator.pop(context);
    
              print("Query successfully executed i.e Message Sent");
    
              showDialog(
                  barrierColor: Colors.transparent,
                  barrierDismissible: true,
                  context: context,
                  builder: (BuildContext context) {
                    return Dialog(
                      child: Container(
                        height: _height * 0.09495,
                        width: _width * 0.17644444,
                        child: Center(
                          child: Text(
                            'Message sent2!',
                            style: TextStyle(
                              color: Colors.green,
                              fontSize: 16,
                            ),
                          ),
                        ),
                      ),
                    );
                  });
            }
          },
        )

【讨论】:

  • 感谢您的解决方案。它确实有效。我不知道这种情况下不需要布尔值。我现在将专注于学习异步函数和等待调用。再次感谢您!
猜你喜欢
  • 1970-01-01
  • 2011-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-31
  • 2020-04-08
  • 2021-11-10
相关资源
最近更新 更多