在 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),
)
],
))
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
控制器定义没问题,但当我尝试将控制器分配给TextField()
控制器时,抛出错误。附上截图:
有什么想法吗?让我知道,即使是建议也很感激。谢谢你!
GitHub 存储库:项目链接 Github
小智 6
删除即可const
。const TextField
你的代码将是这样的:
TextField(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: "Email",
labelStyle: TextStyle(
fontSize: 15.0,
),
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11486 次 |
最近记录: |