我在注冊頁面上遇到問題。用戶可以輸入欄位名稱、電子郵件和密碼( [1]: https://i.stack.imgur.com/FzhuR.png ) 如果輸入正確,則正在創建一個帳戶,用戶將轉到登錄頁面。他等待的時間將由回圈行程指示符 ([1]: https://i.stack.imgur.com/9wjfC.png )指示。
問題是當用戶填寫錯誤并按下注冊按鈕時,行程指示器開始運行。注冊按鈕不見了,沒有辦法繼續前進。我的問題是當欄位之一發生錯誤時如何停止 processindicator?
任何幫助將大大appriciated。
這是代碼:
class RegisterPage extends StatefulWidget {
@override
_RegisterPageState createState() => _RegisterPageState();
}
class _RegisterPageState extends State<RegisterPage> {
final _registerFormKey = GlobalKey<FormState>();
final _nameTextController = TextEditingController();
final _emailTextController = TextEditingController();
final _passwordTextController = TextEditingController();
final _focusName = FocusNode();
final _focusEmail = FocusNode();
final _focusPassword = FocusNode();
bool _isProcessing = false;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
_focusName.unfocus();
_focusEmail.unfocus();
_focusPassword.unfocus();
},
child: Scaffold(
appBar: AppBar(
iconTheme: IconThemeData(color: Color.fromRGBO(132, 76, 130, 1)),
title: Text('Registreren',
style: TextStyle(color: Color.fromRGBO(132, 76, 130, 1))),
centerTitle: true,
backgroundColor: Color.fromRGBO(250, 202, 48, 1),
),
body: Center(
child: SingleChildScrollView(
reverse: true,
padding: EdgeInsets.all(32),
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 120.0,
width: 120.0,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/images/logo.jpg'),
fit: BoxFit.fill,
),
),
),
SizedBox(height: 24.0),
Form(
key: _registerFormKey,
child: Column(
children: <Widget>[
TextFormField(
controller: _nameTextController,
focusNode: _focusName,
validator: (value) => Validator.validateName(
name: value,
),
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color:Color.fromRGBO(132, 76, 130, 1))),
hintText: "Naam ouder",
hintStyle: TextStyle(color: Color.fromRGBO(132, 76, 130, 1)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Color.fromRGBO(250, 202, 48, 1))),
errorBorder: UnderlineInputBorder(
borderRadius: BorderRadius.circular(6.0),
borderSide: BorderSide(
color: Colors.red,
),
),
),
),
SizedBox(height: 16.0),
TextFormField(
controller: _emailTextController,
focusNode: _focusEmail,
validator: (value) => Validator.validateEmail(
email: value,
),
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color:Color.fromRGBO(132, 76, 130, 1))),
hintText: "Email adres",
hintStyle: TextStyle(color: Color.fromRGBO(132, 76, 130, 1)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Color.fromRGBO(250, 202, 48, 1))),
errorBorder: UnderlineInputBorder(
borderRadius: BorderRadius.circular(6.0),
borderSide: BorderSide(
color: Colors.red,
),
),
),
),
SizedBox(height: 16.0),
TextFormField(
controller: _passwordTextController,
focusNode: _focusPassword,
obscureText: true,
validator: (value) => Validator.validatePassword(
password: value,
),
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color:Color.fromRGBO(132, 76, 130, 1))),
hintText: "Wachtwoord",
hintStyle: TextStyle(color: Color.fromRGBO(132, 76, 130, 1)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Color.fromRGBO(250, 202, 48, 1))),
errorBorder: UnderlineInputBorder(
borderRadius: BorderRadius.circular(6.0),
borderSide: BorderSide(
color: Colors.red,
),
),
),
),
SizedBox(height: 32.0),
_isProcessing
? CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Color.fromRGBO(250, 202, 48, 1)),
)
: Row(
children: [
Expanded(
child: ElevatedButton(
onPressed: () async {
setState(() {
_isProcessing = true;
});
if (_registerFormKey.currentState!
.validate()) {
User? user = await FireAuth
.registerUsingEmailPassword(
name: _nameTextController.text,
email: _emailTextController.text,
password:
_passwordTextController.text,
);
setState(() {
_isProcessing = false;
});
if (user != null) {
Navigator.of(context)
.pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) =>
ProfilePage(user: user),
),
ModalRoute.withName('/'),
);
}
}
},
child: Text(
'Registreren',
style: TextStyle(color: Color.fromRGBO(132, 76, 130, 1)),
),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Color.fromRGBO(250, 202, 48, 1)),
),
),
),
],
)
],
),
)
],
),
),
),
),
),
),
);
}
}
uj5u.com熱心網友回復:
你可以試試這個
if (_registerFormKey.currentState!.validate()) {
User? user = await FireAuth.registerUsingEmailPassword(
name: _nameTextController.text,
email: _emailTextController.text,
password:_passwordTextController.text,);
setState(() {
isProcessing = false;
});
if (user != null) {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) =>
ProfilePage(user: user)),ModalRoute.withName('/'),);
}
} /// add code in this line
/// if use input wrong
/// update UI to show the button again
else {
setState(() {
isProcessing = false;
});
}
uj5u.com熱心網友回復:
它沒有按預期作業,因為您已 _isProcessing = false;在if condition.
你應該setState(() => _isProcessing = false)在外面打電話if (_registerFormKey.currentState!.validate()) {...}
uj5u.com熱心網友回復:
我添加了這段代碼,現在它可以作業了。我唯一要做的就是在有人按下注冊按鈕時添加一個小吃店,這是一個錯誤的輸入。
else {
setState(() {
isProcessing = false;
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/402062.html
標籤:扑
上一篇:串列視圖中的容器占用了更多寬度
下一篇:底部溢位1000像素
