Flutter在TextFormField验证器中将循环进度指示器设置为false



我有一个登录屏幕,屏幕上有两个字段,一个用于电子邮件,另一个用于密码。这两个文本字段都有一个验证器,验证是文本字段为null还是空。下面我有一个提交按钮,我的问题是,如果文本字段没有验证,我该如何使loading=false?在这种情况下,如果文本域为null,或者其中一个为null。

global variable >>> bool loading = false;

TextFormField loginEmailTextField() {
return TextFormField(
enableInteractiveSelection: false,
keyboardType: TextInputType.number,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your phone number';
}
return null;
},
controller: emailController,
onChanged: (value) {
setState(() {});
},
decoration: InputDecoration(
prefixIcon: const Icon(Icons.phone),
suffixIcon: emailController.text.isEmpty
? const Text('')
: GestureDetector(
onTap: () {
emailController.clear();
},
child: const Icon(Icons.close),
),
labelText: 'Phone',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(color: Colors.red, width: 1),
),
),
);
}
TextFormField loginPasswordTextField() {
return TextFormField(
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
return null;
},
obscureText: isVisible,
controller: passwordController,
onChanged: (value) {
print(value);
},
decoration: InputDecoration(
prefixIcon: const Icon(Icons.lock),
suffixIcon: GestureDetector(
onTap: () {
isVisible = !isVisible;
setState(() {});
},
child: Icon(isVisible ? Icons.visibility : Icons.visibility_off),
),
labelText: 'Password',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(color: Colors.red, width: 1),
),
),
);
}
Column loginSubmitButton(double width, double height, BuildContext context) {
return Column(
children: <Widget>[
Container(
width: width / 2,
height: height / 12,
child: ElevatedButton(
onPressed: () async {
if (_formKey.currentState!.validate()) {
Future<Response> futureResponse = fetchWorkingLocationData();
futureResponse
.then((response) => {
if (response.statusCode == 200)
{
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MenuPage()),
)
}
else
{
setState(() {
loading = false;
}),
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
backgroundColor: Colors.blue,
content: Text(
"Incorrect phone number or password",
style: TextStyle(fontSize: 18),
),
duration: Duration(seconds: 4),
),
),
},
})
.catchError((error, stackTrace) => print('shush'));
}
if (loading) return;
setState(() {
loading = true;
});
},
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
child: loading
? Loading()
: Text(
'Submit',
style: TextStyle(fontSize: 22, color: Colors.white),
),
),
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
),
),
),
),
),
],
);
}
}

首先按下按钮,您将检查凭据是否为空/Null,如果不是,则前进下一步,然后加载变量就可以为true,并执行api进程,在响应后加载变量false。

像这样

Future<String> signIn() async {
if (IDforlogin.text != '') {
if (password.text != '') {
setState(() {
_isloading = true;
});
Map data = {
'email': IDforlogin.text.trim(),
'password': password.text.trim(),
'fcm_token': fcm_token.toString()
};
print(data);
var response = await http.post(API_URL + 'login', body: data);
try {
print(response.body);
var decodedData = json.decode(response.body);
print(decodedData['code']);
if (decodedData['code'] == 200) {
setState(() {
_isloading = false;
});
Navigator.push(
context,
MaterialPageRoute(builder: (context) => dashbord()),
);
} else {
setState(() {
_isloading = false;
});
Toast.show(decodedData['message'], context,
duration: Toast.LENGTH_LONG, gravity: Toast.CENTER);
}
} catch (error) {
setState(() {
_isloading = false;
});
Toast.show(errormessage, context,
duration: Toast.LENGTH_LONG,
gravity: Toast
.CENTER); // executed for errors of all types other than Exception
}
} else {
Toast.show('Please enter password', context,
duration: Toast.LENGTH_LONG, gravity: Toast.CENTER);
}
} else {
Toast.show('Please enter ID', context,
duration: Toast.LENGTH_LONG, gravity: Toast.CENTER);
}}

我找到了一个适合我个人需要的答案

TextFormField loginEmailTextField() {
return TextFormField(
enableInteractiveSelection: false,
keyboardType: TextInputType.number,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your phone number';
}
return null;
},
controller: emailController,
onChanged: (value) {
setState(() {});
},
decoration: InputDecoration(
prefixIcon: const Icon(Icons.phone),
suffixIcon: emailController.text.isEmpty
? const Text('')
: GestureDetector(
onTap: () {
emailController.clear();
},
child: const Icon(Icons.close),
),
labelText: 'Phone',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(color: Colors.red, width: 1),
),
),
);
}
TextFormField loginPasswordTextField() {
return TextFormField(
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
return null;
},
obscureText: isVisible,
controller: passwordController,
onChanged: (value) {
print(value);
},
decoration: InputDecoration(
prefixIcon: const Icon(Icons.lock),
suffixIcon: GestureDetector(
onTap: () {
isVisible = !isVisible;
setState(() {});
},
child: Icon(isVisible ? Icons.visibility : Icons.visibility_off),
),
labelText: 'Password',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(color: Colors.red, width: 1),
),
),
);
}
Column loginSubmitButton(double width, double height, BuildContext context) {
return Column(
children: <Widget>[
Container(
width: width / 2,
height: height / 12,
child: ElevatedButton(
onPressed: () async {
if (_formKey.currentState!.validate()) {
setState(() {
loading = true;
});
Future<Response> futureResponse = fetchWorkingLocationData();
futureResponse
.then((response) => {
if (response.statusCode == 200)
{
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MenuPage()),
)
}
else
{
setState(() {
try {
loading = false;
} on Exception catch (e, s) {
loading = true;
}
}),
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
backgroundColor: Colors.blue,
content: Text(
"Incorrect phone number or password",
style: TextStyle(fontSize: 18),
),
duration: Duration(seconds: 4),
),
),
},
})
.catchError((error, stackTrace) => print('shush'));
}
},
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
child: loading
? Loading()
: Text(
'Submit',

最新更新