flutter - 颤振:如何在登录前显示加载动画?
问题描述
我正在使用颤振。输入我的id和密码后,我想log in animation
在进入主页之前显示一个。我使用 adialog
但我觉得我的代码非常生硬并且有潜在的错误。有更好的解决方案吗?
// this part is for the input content is legal
else {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return LoadingStyle.buildWidget(); // this is a simple loading animation
});
service.createSession(context, code, id).then((response) { // log in part
if (response != null) {
this.saveUser(response); // something about saving user info
} else {
print('null respose');
}
}).catchError((e) {
if (e is GrpcError && e.code == StatusCode.unauthenticated) {
setState(() {
this.errorMessage = "grpc network error";
});
}
}).whenComplete(() {
Navigator.of(context).pop(); // pop dialog here, is this right?
MyRoutersl.goNewPage(context); // enter the new page
});
}
解决方案
我建议使用FutureBuilder。还有一些默认的加载小部件,例如CircularProgressIndicator()
可以在进行时使用。
因为登录是某种异步进程,你可以像下面这样使用 FutureBuilder:
FutureBuilder(
future: service.createSession(... // Use Async function to return the result
builder: (context, snapshot) {
if(snapshot.hasData && snapshot.connectionState == done ){
// return widget after login successfully
// result should equal to snapshot.data
} else {
// return CircularProgressIndicator();
}
}
)
如果你需要更多花哨的加载指示器,你可以查看这个包flutter_spinkit
推荐阅读
- python-3.x - 如何在夹具中导入 python 并将它们用作 Pytest 中每个测试的全局变量?
- java - 如何使用 netcat 将 Cassandra 查询的整个结果发送到套接字
- android - how can i communicate to google home mini device with android app or vise-versa
- python - Python tkinter Menu
- erlang - 主管的所有孩子都死后会发生什么?
- c++ - file not found error when trying to include id3lib header
- vue.js - 格式化后光标定位在一个奇怪的位置
- qt - 发出 dataChanged 时 QTreeView 没有刷新数据
- javascript - How to convert object to list in javascript?
- linear-programming - How I can read files in miplib