请帮帮我!我正在创建一个简单的登录页面,但我得到了一个异常,看起来像这样:
══╡小部件库捕获的异常╞════════════════[+3s]I/flutter(11275(:引发了以下NoSuchMethodError正在生成登录屏幕(脏,依赖项:[MediaQuery](:[]I/flutter(11275(:在null上调用了方法"validate"。[
]I/flutter(11275(:接收器:空[]I/flutter尝试调用:validate(([]I/flutter(11275(:[]I/flutter(11275(:导致小部件相关错误的原因是:[]I/flutter(11275(:登录屏幕
虽然我认为,我已经配置了所有需要配置的。但不知怎么的,我的代码抛出了异常。如何修复?
这是我的代码:
import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart';
import 'package:my_app/services/UserService.dart';
/*
* This is login screen view.
*
* If you want to edit form section for example email and password fields, just edit these below functions.
* - _formEmailWidget()
* - _formPasswordWidget()
* If you want to edit social login section, just edit _socialLoginWidget() function.
* If you want to edit or change logo, just edit _logoWidget() function.
*/
class LoginScreen extends StatelessWidget {
static final Pattern _pattern =
r'^(([^<>()[]\.,;:s@"]+(.[^<>()[]\.,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$';
final GlobalKey<FormState> _key = GlobalKey<FormState>();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
final UserAuth _auth = UserAuth.instance();
final Status status;
LoginScreen({@required this.status});
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
body: SafeArea(child: this._bodyWidgets(context)));
}
_logoWidget() {
return Padding(
padding: const EdgeInsets.only(top: 15.0, bottom: 50.0),
child: Image(image: AssetImage('assets/images/logo.png')));
}
_formEmailWidget() {
return Container(
decoration: BoxDecoration(
border:
Border(bottom: BorderSide(color: Colors.white, width: 1.0))),
child: TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
validator: (String value) {
RegExp regex = new RegExp(_pattern);
if (regex.hasMatch(value)) {
return null;
}
return "Please, Enter valid e-mail";
},
decoration: const InputDecoration(
icon: Icon(
FontAwesome.user,
color: Colors.white,
),
fillColor: Colors.white,
hintStyle: TextStyle(
color: Colors.white,
fontFamily: 'ProximaThin',
),
hintText: 'Your e-mail'),
));
}
_formPasswordWidget() {
return Container(
decoration: BoxDecoration(
border:
Border(bottom: BorderSide(color: Colors.white, width: 1.0))),
child: TextFormField(
controller: _passwordController,
validator: (String value) {
if (value.isEmpty || value.length < 6) {
return "Password must be more than 6 characters";
}
return null;
},
decoration: const InputDecoration(
icon: Icon(
FontAwesome.key,
color: Colors.white,
),
fillColor: Colors.white,
hintStyle: TextStyle(
color: Colors.white,
fontFamily: 'ProximaThin',
),
hintText: 'Your password'),
obscureText: true,
));
}
_submitRowWidget() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
InkWell(
onTap: null,
child: Text(
'forget password?',
style: TextStyle(color: Colors.white),
),
),
OutlineButton(
textColor: Colors.white,
highlightedBorderColor: Colors.white,
borderSide: BorderSide(
color: Colors.white, width: 0.8, style: BorderStyle.solid),
shape: OutlineInputBorder(borderRadius: BorderRadius.circular(10.0)),
onPressed: _loginAction(),
child: Text(
'Login',
style: TextStyle(color: Colors.white),
),
)
],
);
}
_loginAction() {
if (_key.currentState.validate()) {
_auth.signIn(_emailController.text, _passwordController.text);
} else {
AlertDialog(
content: Text(
"Your e-mail or password doesn't match, Would you like create an account?"),
elevation: 24.0,
shape: OutlineInputBorder(borderRadius: BorderRadius.circular(10)),
actions: <Widget>[
FlatButton(
onPressed: null,
child: Text("Yes"),
)
],
);
}
}
_socialLoginWidget() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(10)),
),
child: FlatButton(
padding: const EdgeInsets.only(
left: 10.0, top: 0.0, bottom: 0.0, right: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Icon(FontAwesome.facebook_f, color: Colors.black),
SizedBox(
width: 10,
),
Text('Facebook',
style: TextStyle(
fontFamily: 'Proxima',
fontSize: 11,
color: Colors.black))
],
),
onPressed: null,
),
),
Container(
width: 100,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(10)),
),
child: FlatButton(
padding: const EdgeInsets.only(
left: 10.0, top: 0.0, bottom: 0.0, right: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Icon(
FontAwesome.google,
color: Colors.black,
),
SizedBox(
width: 10,
),
Text(
'Google',
style: TextStyle(
fontFamily: 'Proxima', fontSize: 11, color: Colors.black),
)
],
),
onPressed: null,
),
)
],
);
}
_bodyWidgets(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_logoWidget(),
Expanded(
child: Center(
child: Container(
width: MediaQuery.of(context).size.width - 20,
decoration: BoxDecoration(
color: Colors.black,
boxShadow: [
BoxShadow(
color: Color.fromRGBO(0, 0, 0, 0.35),
blurRadius: 15.0,
offset: Offset(0, -5))
],
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30))),
child: Padding(
padding:
const EdgeInsets.only(top: 45.0, left: 45.0, right: 45.0),
child: Column(children: <Widget>[
Form(
key: this._key,
child: Column(
children: <Widget>[
_formEmailWidget(),
_formPasswordWidget(),
SizedBox(
height: 15.0,
),
_submitRowWidget()
],
),
),
SizedBox(
height: 45,
),
Text('or connect using',
style: TextStyle(
fontFamily: 'ProximaThin', color: Colors.white)),
SizedBox(
height: 15,
),
_socialLoginWidget(),
SizedBox(
height: 15,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Don't have an account?',
style: TextStyle(
fontFamily: 'ProximaThin', color: Colors.white),
),
SizedBox(
width: 5,
),
InkWell(
onTap: null,
child: Text('Sign up',
style: TextStyle(
fontFamily: 'Proxima', color: Colors.white)),
)
],
)
]),
),
),
))
]);
}
}
这里有两个选项:
-
将以下方法
OnPressed: _loginAction(),
更改为OnPressed: _loginAction,
-
或将同一行更改为
OnPressed: () { _loginAction(); },
以下固定代码:
_submitRowWidget() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
InkWell(
onTap: null,
child: Text(
'forget password?',
style: TextStyle(color: Colors.white),
),
),
OutlineButton(
textColor: Colors.white,
highlightedBorderColor: Colors.white,
borderSide: BorderSide(
color: Colors.white, width: 0.8, style: BorderStyle.solid),
shape: OutlineInputBorder(borderRadius: BorderRadius.circular(10.0)),
onPressed: _loginAction,
child: Text(
'Login',
style: TextStyle(color: Colors.white),
),
)
],
);
}