Positioned(
left: 150.0,
top: 630.0,
right: null,
bottom: null,
width: 100.0,
height: 50.0,
child: Align(
child: RaisedButton(
child: Text(
'Confirm',
style: TextStyle(fontSize: 20.0),
),
color: Color.fromARGB(255, 227, 236, 246),
textColor: Color.fromARGB(255, 49, 69, 106),
padding: EdgeInsets.all(5.0),
onPressed: () {
for(int i=0; i<usernames.length; i ){
if(conEmail.text.isEmpty | conName.text.isEmpty | conUsername.text.isEmpty | conPassword.text.isEmpty | conConfirm.text.isEmpty ){
print("some fields are empty");
break;
}
而不是列印“某些欄位為空”,我想創建一個文本以在顯示相同訊息的注冊頁面上彈出。 螢屏布局
uj5u.com熱心網友回復:
根據您的代碼摘錄,我猜測 UI 位于有狀態小部件中。您可以顯示警報彈出視窗或在注冊頁面上顯示文本。在我看來,最好的解決方案是將條目分組到 Form 小部件中。
該檔案有示例代碼:Form widget API and example
如果這不符合您的需求并且您希望在注冊頁面本身上顯示錯誤訊息,您可以將錯誤訊息包含為可選可見的文本。這意味著您必須有一個布林值來顯示條目是否完整。
```
bool formIsOk = true;
String errorString = '';
...
Widget build(BuildContext context) {
RaisedButton(...
onPressed: () {
for(int i=0; i<usernames.length; i ){
if(conEmail.text.isEmpty | conName.text.isEmpty | conUsername.text.isEmpty | conPassword.text.isEmpty | conConfirm.text.isEmpty ){
errrorString = 'some fields are empty';
break;
}
if (!errorString.isEmpty) {
setState(() {
}
);
}
...
//other widgets
...
Visibility(visible: errorString.isEmpty,
child: Text('some fields are empty'))
```
最后,如果你只是想彈出一個警報,你可以使用showDialog使用AlertDialog 類:
```
onPressed: () => onPressed() {
for(int i=0; i<usernames.length; i ){
if(conEmail.text.isEmpty | conName.text.isEmpty | conUsername.text.isEmpty | conPassword.text.isEmpty | conConfirm.text.isEmpty ){
showDialog<String>(
context: context,
builder: (BuildContext context) => AlertDialog(
title: const Text('ValidationError'),
content: const Text('Some fields are empty'),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context, 'OK'),
child: const Text('OK'),
),
],
),
}
```
uj5u.com熱心網友回復:
而不是print("某些欄位為空"); 使用訊息呼叫showDialog()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383461.html
標籤:扑
