我希望在用户登录设备时显示循环进度指示器。我在网上看到了各种方法,其中一种是使用 bool _isloading 像这里但在这里它用 curcularprogressindicator 替换了我的按钮并且没有登录 + 我希望指示器在中心screen 和其他是在使用我不需要的 futurebuilder 时。所以,我想了很多,找不到任何东西,所以如果你能帮助我,那就太好了。
我的代码:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:green_earth/mainpage.dart';
import 'package:http/http.dart' as http;
Future<void> attemptSignUp(String username, String password,BuildContext context) async {
final http.Response res = await http.post(
'https://green-earth.herokuapp.com/signup',
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body:jsonEncode(<String, String>{
"email":username,
"password":password
}),
);
CircularProgressIndicator();
if (res.statusCode==200){
Navigator.of(context).pushNamed('/home');
}
else{
_showMyDialog(context,res.statusCode);
}
}
// print("res status code=${res.statusCode} $username $password ${jsonDecode(res.body)}")
Future<void> _showMyDialog(BuildContext context,int statuscode) async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Error'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
statuscode==422?Text('Email ID is already taken. Please try again!'):Text('Authorisation was failed with status code $statuscode'),
],
),
),
actions: <Widget>[
TextButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
调用函数
GestureDetector(
onTap:(){
if(formkey2.currentState.validate()==true){
signupUsername=signupUsernameController.text;
signupPassword=signupPasswordController.text;
attemptSignUp(signupUsername, signupPassword,context);
}
},
child: PrimaryButton(
btnText: "Create Account",
),
),