如何解决ScaffoldMessenger 不显示 SnackBar,而不是调试控制台中的问题
我正在尝试使用 Firebase 实现密码重置功能,如果发生 PlatformException 错误,则 SnackBar 会显示错误是什么。但是,尽管存在 PlatformException 错误,但 SnackBar 并未显示。但是,控制台没有显示 SnackBar 实现的任何问题。任何见解将不胜感激!
class ResetPasswordScreen extends StatefulWidget {
@override
_ResetPasswordState createState() => _ResetPasswordState();
}
class _ResetPasswordState extends State<ResetPasswordScreen> {
final _auth = auth.FirebaseAuth.instance;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
String _email = '';
@override
Widget build(BuildContext context) {
return Center(
child: FractionallySizedBox(
widthFactor: 1,heightFactor: .5,child: Padding(
padding: const EdgeInsets.only(left: 10,right: 10),child: Card(
color: Colors.white,shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),),child: Padding(
padding: const EdgeInsets.all(10),child: Scaffold(
resizetoAvoidBottomInset: false,backgroundColor: Colors.white,appBar: PreferredSize(
preferredSize: Size.fromHeight(40),child: AppBar(
backgroundColor: Colors.white,elevation: 0,leading: IconButton(
icon: Icon(Icons.arrow_back,color: Theme.of(context).primaryColor),onpressed: () => Navigator.of(context).pop(),title: Text(
'Password Reset',style: TextStyle(
color: Theme.of(context).primaryColor,fontSize: 28,body: Padding(
padding: EdgeInsets.all(20),child: Form(
key: _formKey,child: Column(
children: <Widget>[
resetEmail(context),SizedBox(height: 20),Align(
alignment: Alignment.bottomright,child: resetButton(),],);
}
TextFormField resetEmail(BuildContext context) {
return TextFormField(
keyboardType: TextInputType.emailAddress,cursorColor: Theme.of(context).accentColor,decoration: Inputdecoration(labelText: 'Email'),validator: (value) {
if (value.isEmpty || !value.contains('@')) {
return 'Please enter a valid email';
}
return null;
},onSaved: (value) {
_email = value.trim();
},);
}
TextButton resetButton() {
return TextButton(
child: Text('Send request'),style: TextButton.styleFrom(
primary: Theme.of(context).primaryColor,textStyle: TextStyle(
fontSize: 20,onpressed: () => _submitRequest(),);
}
void _submitRequest() async {
if (!_formKey.currentState.validate()) {
return;
}
_formKey.currentState.save();
FocusScope.of(context).unfocus();
try {
await _auth.sendPasswordResetEmail(email: _email);
Navigator.of(context).pop();
} on PlatformException catch (err) {
var message = 'Please check your email';
if (err.message != null) {
message = err.message;
}
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),backgroundColor: Theme.of(context).errorColor,);
} catch (err) {
print(err);
}
}
}
解决方法
该问题与 Snackbar 代码无关(我对其进行了测试,并且运行正常)。问题可能是 showSnackBar 函数永远不会被调用。
,事实证明这是一个上下文问题。我需要为 Scaffold 提供一个构建器函数,以便为小吃店提供适当的上下文。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。