import 'package:flutter/material.dart';
import 'package:flutter_app_1/utils/routes.dart';
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
String name = "";
bool changeButton = false;
final _formKey = GlobalKey<FormState>();
moveToHome(BuildContext context) async {
if (_formKey.currentState.validate()) {
setState(() {
changeButton = true;
});
await Future.delayed(Duration(seconds: 1));
await Navigator.pushNamed(context, MyRoutes.homeRoute);
setState(() {
changeButton = false;
});
}
}
@override
Widget build(BuildContext context) {
return Material(
color: Colors.white,
child: SingleChildScrollView(
child: Form(
key: _formKey,
child: Column(
children: [
Image.asset(
"assets/images/login_image.png",
fit: BoxFit.cover,
height: 500,
),
SizedBox(
height: 20.0,
),
Text(
"Welcome $name",
style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
),
SizedBox(
height: 28.0,
),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 16.0, horizontal: 32.0),
child: Column(
children: [
TextFormField(
decoration: InputDecoration(
hintText: "Enter User Name",
labelText: "Username",
),
validator: (String? value) {
if (value != null && value.isEmpty) {
return "Username can't be empty";
}
return null;
},
onChanged: (value) {
name = value;
setState(() {});
},
),
TextFormField(
obscureText: true,
decoration: InputDecoration(
hintText: "Enter password",
labelText: "Password",
),
validator: (String? value) {
if (value != null && value.isEmpty) {
return "Password can't be empty";
}
return null;
},
),
SizedBox(
height: 40.0,
),
Material(
color: Colors.deepPurple,
borderRadius:
BorderRadius.circular(changeButton ? 50 : 8),
child: InkWell(
onTap: () => moveToHome(context),
child: AnimatedContainer(
duration: Duration(seconds: 1),
width: changeButton ? 50 : 150,
height: 50,
//color: Colors.deepPurple,
alignment: Alignment.center,
child: changeButton
? Icon(
Icons.done,
color: Colors.white,
)
: Text(
"Login",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 18),
),
// decoration: BoxDecoration(
// //color: Colors.deepPurple,
// // shape: changeButton
// // ? BoxShape.circle
// // : BoxShape.rectangle,
// ),
),
),
),
// ElevatedButton(
// child: Text("Login"),
// style: TextButton.styleFrom(minimumSize: Size(150, 40)),
// onPressed: () {
// Navigator.pushNamed(context, MyRoutes.homeRoute);
// })
],
),
)
],
),
),
));
}
}
大家好,
我正在尝试将验证器添加到小部件,但收到此错误,请帮助。来解决这个错误。
错误:
The method 'validate' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!')
我尝试替换提供的代码,但仍然收到错误。请调查一下。
第二个错误:
The method 'validate' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!')
我尝试替换提供的代码,但仍然收到错误。请调查一下。