我试图向用户显示一个警告,不要让username和passweord字段为空,但我收到了以下错误:
E/flatter(6339):[错误:flatter/lib/ui/ui\u dart\u state.cc(198)]未处理的异常:类型“TextEditingController”不是类型转换中类型“String”的子类型
这是我的代码:
class _MyHomePageState extends State<MyHomePage> {
TextEditingController username = TextEditingController();
TextEditingController password = TextEditingController();
Future login(BuildContext cont) async {
if(username.text == " " || password.text == " "){
Alert(
context: context,
type: AlertType.error,
title: "ALERT",
desc: "Wrong username or password",
buttons: [
DialogButton(
child: Text(
"OK",
style: TextStyle(color: Colors.white, fontSize: 20),
),
onPressed: () => Navigator.pop(context),
width: 120,
)
],
).show();
}
else{
var url = "localhost:8080/localconnect/login.php";
var response = await http.post(url, body: {
username : username.text,
password : password.text,
});
var data = jsonDecode(response.body);
if(data=="success"){
Navigator.push(cont, MaterialPageRoute(builder: (context)=>WelcomeScreen()));
}
else{
Alert(
context: context,
type: AlertType.error,
title: "ALERT",
desc: "Wrong username or password",
buttons: [
DialogButton(
child: Text(
"OK",
style: TextStyle(color: Colors.white, fontSize: 20),
),
onPressed: () => Navigator.pop(context),
width: 120,
)
],
).show();
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Container(
constraints: BoxConstraints(
maxHeight: MediaQuery.of(context).size.height,
maxWidth: MediaQuery.of(context).size.width,
),
decoration: BoxDecoration(
gradient: LinearGradient(colors: [
Colors.blue[800]!,
Colors.blue[600]!,
],
begin: Alignment.topLeft,
end: Alignment.centerRight,
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(flex: 2,
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 36.0, horizontal: 24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Login",
style: TextStyle(
color: Colors.white,
fontSize: 46.0,
fontWeight: FontWeight.w800
),
),
SizedBox(height: 10.0,),
Text(
"Enter to system",
style: TextStyle(
color: Colors.white,
fontSize: 22.0,
fontWeight: FontWeight.w300
),
)
],
),
)
),
Expanded(flex: 5,
child: Container(
width: double.infinity,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(40),
topRight: Radius.circular(40),)
),
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
TextField(
controller: username,
keyboardType: TextInputType.name,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0,),
borderSide: BorderSide.none,
),
filled: true,
fillColor: Color(0xffe7edeb),
hintText: "Username",
prefixIcon: Icon(
Icons.person,
color: Colors.grey[600],
),
),
),
SizedBox(height: 20.0,),
TextField(
controller: password,
obscureText: true,
// keyboardType: TextInputType.visiblePassword,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0,),
borderSide: BorderSide.none,
),
filled: true,
fillColor: Color(0xffe7edeb),
hintText: "Password",
prefixIcon: Icon(
Icons.lock,
color: Colors.grey[600],
),
),
),
SizedBox(height: 50.0,),
Container(
width: double.infinity,
child: ElevatedButton(
onPressed: (){
login(context);
},
// color: Colors.blue[800],
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: Text(
"Login",
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
),
),
),
),
)
],
),
),
),
),
],
),
),
),
);
}
}