【发布时间】: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