我是一個學習最近顫動的本尼格人,如果電子郵件、密碼和手機無效,我需要啟用/禁用按鈕“注冊”,我為此制作了相應的代碼,但是此行不能放在任何地方,因為它給了我一個錯誤:
isButtonActive ? (){ setState(()=> isButtonActive = false); passwd.clear(); passwd2.clear();} : null,
我需要把那行放在這個 ElevatedButton 中
child: ElevatedButton(
// color: Colors.amber[100],
child: Text('Login',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.black)),
onPressed:
//
() {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MyHomePage(),
),
);
},
或者在函式“Future register() async”中。
我不能,因為我必須洗掉用于 isButtonActive 陳述句作業的 Navigator.push 并且在將來注冊會給我錯誤。
我的表格:
class _RegisterState extends State<Register> {
bool showError = false;
bool isButtonActive = true;
String message = '';
@override
void initState() {
super.initState();
correo.addListener(() {
final isButtonActive = correo.text.isNotEmpty;
setState(() => this.isButtonActive = isButtonActive);
});
celular.addListener(() {
final isButtonActive = celular.text.isNotEmpty;
setState(() => this.isButtonActive = isButtonActive);
});
passwd2.addListener(() {
setState(() {
showError = passwd2.text.isEmpty
? false
: passwd.text.trim() != passwd2.text.trim();
});
final isButtonActive = passwd2.text.isNotEmpty;
setState(() => this.isButtonActive = isButtonActive);
});
passwd.addListener(() {
setState(() {
showError = passwd.text.isEmpty
? false
: passwd.text.trim() != passwd2.text.trim();
});
final isButtonActive = passwd.text.isNotEmpty;
setState(() => this.isButtonActive = isButtonActive);
});
}
void validateEmail(String enteredEmail) {
if (EmailValidator.validate(enteredEmail)) {
setState(() {
message = '';
});
} else {
setState(() {
message = ('Please enter a valid email address!');
});
}
}
TextEditingController correo = TextEditingController();
TextEditingController celular = TextEditingController();
TextEditingController passwd = TextEditingController();
TextEditingController passwd2 = TextEditingController();
@override
void dispose() {
correo.dispose();
super.dispose();
}
Future register() async {
var url =
"http://192.168.2.131/DatabaseDB/register.php";
var response = await http.post(url, body: {
"correo": correo.text,
"celular": celular.text,
"passwd": passwd.text,
"passwd2": passwd2.text
});
var data = json.decode(response.body);
if (data == "Error") {
FlutterToast(context).showToast(
child: Text(
'User allready exit!',
style: TextStyle(fontSize: 25, color: Colors.red),
));
} else {
FlutterToast(context).showToast(
child: Text('Registration Successful',
style: TextStyle(fontSize: 25, color: Colors.green)));
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DashBoard(),
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: 900,
child: Card(
color: Colors.blue[200],
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Register',
style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
decoration: InputDecoration(
labelText: 'Correo',
prefixIcon: Icon(Icons.mail),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide(color: Colors.blue, width: 2.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide:
BorderSide(color: Colors.blue[900], width: 2.0),
)),
onChanged: (enteredEmail) => validateEmail(enteredEmail),
controller: correo,
),
),
Text(
message,
style: TextStyle(color: Colors.red),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
maxLength: 12,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly,
],
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Celular',
prefixIcon: Icon(Icons.person),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide(color: Colors.blue, width: 2.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide:
BorderSide(color: Colors.blue[900], width: 2.0),
),
),
controller: celular,
textInputAction: TextInputAction.done,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
obscureText: true,
decoration: InputDecoration(
labelText: 'Contrase?a',
prefixIcon: Icon(Icons.lock),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide(color: Colors.blue, width: 2.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide:
BorderSide(color: Colors.blue[900], width: 2.0),
),
),
controller: passwd,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
obscureText: true,
decoration: InputDecoration(
labelText: 'Repita contrase?a',
prefixIcon: Icon(Icons.lock),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide(color: Colors.blue, width: 2.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide:
BorderSide(color: Colors.blue[900], width: 2.0),
),
),
controller: passwd2,
),
),
if (showError)
const Text(
"Las contrase?as no coinciden",
style: TextStyle(color: Colors.red),
),
Row(
children: <Widget>[
Expanded(
child: ElevatedButton(
// color: Colors.pink,
style: ElevatedButton.styleFrom(
onSurface: Colors.red,
),
child: Text('Register',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white)),
onPressed: () {
register();
passwd.text = "";
setState(() {});
},
),
),
Expanded(
child: ElevatedButton(
// color: Colors.amber[100],
child: Text('Login',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.black)),
onPressed:
//
() {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MyHomePage(),
),
);
},
),
),
],
)
],
),
),
),
);
}
}
在 void initState 中,我將所有控制器.addListener(() {與最終isButtonActive = correo.text.isNotEmpty; setState(() => this.isButtonActive = isButtonActive);
請幫助我,如果按鈕的 onPressed 被占用,我不明白把我的 isButtonActive 陳述句放在哪里。
謝謝
uj5u.com熱心網友回復:
@jeremynac 看看這個:
Expanded(
child: ElevatedButton(
// color: Colors.pink,
style: ElevatedButton.styleFrom(
onSurface: Colors.blue,
),
child: Text('Register',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white)),
onPressed: () => [
if (isButtonActive)
{
setState(() => isButtonActive = false),
}
else
{null},
register(),
// passwd.text = "",
setState(() {})
],
),
),
這是部分問題的解決方案,使用您的傳統 if 句子和()=>[]內部 onpressed,但如果 textformfields passwd、passwd2、correo 或 celular 為(至少一個)為空,請不要禁用我的按鈕。
uj5u.com熱心網友回復:
isButtonActive ? (){ setState(()=> isButtonActive = false); passwd.clear(); passwd2.clear();} : null,
我不確定你為什么這樣做,但在我看來這可能是錯誤的根源。
你為什么不做一個簡單的如果:
if (isButtonActive) {
setState(()=> isButtonActive = false);
passwd.clear();
passwd2.clear();
}
如果您需要將此作為回呼傳遞給 onPressed(或其他):
() {
if (isButtonActive) {
// … as above
}
}
您可能還需要將 ElevatedButton 的 ? enabled ? 屬性設定為 isButtonActive,因此當 isButtonActive 為 false 時,您的按鈕將被停用,如下所示:
ElevatedButton(
//…
enabled: isButtonActive
//…
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/399071.html
上一篇:顫振這種用法有什么意義
下一篇:無法將字串推入陣列
