在Flutter中使用TextField()构建UI时,我在state中定义了控制器,并尝试使用TextField()中定义的emailController和passwordController,但它显示“无效常量值"。我尝试解决该问题,但没有成功。以下是login_screen.dart的代码
import 'package:flutter/material.dart';
import 'package:rider_app/routes/routing_constants.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({Key? key}) : super(key: key);
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
final _validate = false;
// @override
// void dispose() {
// emailController.dispose();
// passwordController.dispose();
// super.dispose();
// }
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Column(
children: [
const SizedBox(
height: 40.0,
),
const Center(
child: Image(
image: AssetImage("lib/assets/images/logo.png"),
width: 200.0,
height: 200.0,
alignment: Alignment.center,
)),
const SizedBox(
height: 2.0,
),
const Text(
"Login as a Rider",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 25.0, fontFamily: "Brand Bold"),
),
Padding(
padding:
const EdgeInsets.symmetric(horizontal: 30.0, vertical: 10.0),
child: Column(
children: [
const SizedBox(
height: 10.0,
),
const TextField(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: "Email",
labelStyle: TextStyle(
fontSize: 15.0,
),
hintText: "Email address",
hintStyle: TextStyle(fontSize: 12.0, color: Colors.grey),
// focusedBorder: OutlineInputBorder(
// borderSide:
// BorderSide(width: 1.0, color: Colors.blueAccent),
// ),
// enabledBorder: OutlineInputBorder(
// borderSide: BorderSide(width: 1.0),
// ),
),
style: TextStyle(fontSize: 15.0),
controller: emailController,
),
const SizedBox(
height: 10.0,
),
const TextField(
controller: passwordController,
keyboardType: TextInputType.visiblePassword,
decoration: InputDecoration(
labelText: "Password",
labelStyle: TextStyle(fontSize: 15.0),
hintText: "Your password",
hintStyle: TextStyle(fontSize: 12.0, color: Colors.grey),
// focusedBorder: OutlineInputBorder(
// borderSide: BorderSide(width: 1.0, color: Colors.blueAccent),
),
// enabledBorder: OutlineInputBorder(
// borderSide: BorderSide(width: 1.0)
// )
// ),
obscureText: true,
style: TextStyle(fontSize: 15.0),
),
const SizedBox(
height: 45.0,
),
ElevatedButton(
onPressed: () {
debugPrint("Logged In");
Navigator.pushNamed(context, homeScreen);
},
child: const Padding(
padding: EdgeInsets.symmetric(
horizontal: 23, vertical: 11),
child: Text(
'Login',
style: TextStyle(
fontSize: 18,
fontFamily: "Brand Bold",
fontWeight: FontWeight.w500),
),
),
),
],
),
),
FlatButton(
onPressed: () {
Navigator.pushNamed(context, signUpScreen);
},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Text(
"Don't have Account?",
style: TextStyle(
fontWeight: FontWeight.w400,
fontFamily: "Brand-Regular"),
),
Text(
" Register Here.",
style: TextStyle(
fontFamily: "Brand-Regular",
fontWeight: FontWeight.w600),
)
],
))
],
),
),
);
}
}
控制器定义是正确的,但当我试图将控制器分配给TextField()
控制器时,抛出了错误。
有什么想法??让我知道,即使是建议也很感激。谢谢!
GitHub回购协议:Project Link Github
5条答案
按热度按时间djp7away1#
删除TextField前面的
const
,这样应该可以解决错误。gudnpqoy2#
删除TextField小部件开头的常量关键字。
mqxuamgl3#
尝试从
TextField
中删除const
修饰符。cyej8jka4#
尝试删除TextField前面的“const”(或小部件树中的顶部小部件之一),这样应该可以解决该错误。
3qpi33ja5#
从
const TextField
中删除const
,您就可以开始了。您的代码如下所示: