您好,我是 Flutter 的新手,目前正在构建一个聊天应用程序。
我有一个配置文件制作器屏幕,用户可以在其中上传图像来设置他们的头像。我CircularProgressIndicator()
用来显示上传屏幕。我想知道如何在上传完成后自动导航到下一个屏幕,即我的主屏幕,这样用户就不必等待任何按钮按下。
这是我尝试过的代码
progressString != '100% Completed' ? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircularProgressIndicator(
backgroundColor: Colors.blue,
),
SizedBox(
height: 20.0,
),
Text("Uploading File : $progressString",
style: TextStyle(
color: Colors.white54,
fontSize: 20.0,
fontWeight: FontWeight.w900,
),
),
],
) : Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) {
return LoginPage();
}),
),
上传代码
FormData data = FormData.fromMap({
"username": userName.toString(),
"name": naMe.toString(),
"birthday": birthDay.toString(),
"about": aboutUser.toString(),
"sender": sendUser.toString(),
"mobile": userMobile.toString(),
"avatar": _image != null
? await MultipartFile.fromFile(_image.path,
filename: avatarName.toString())
: Text('Invalid Avatar'),
});
if (_validateAndSave()) {
final token = widget.token;
try {
Dio dio = Dio();
dio.options.headers['Accept'] = "application/json";
dio.options.headers['Authorization'] = "Bearer $token";
dio.options.headers['Content-Type'] = "multipart/form-data";
dio.options.followRedirects = false;
var response = await dio.post(url,
data: data, onSendProgress: (int rec, int total) {
setState(() {
uploading = true;
progressString = ((rec / total * 100).toString());
});
});
var responseCode = response.statusCode;
print('Dio responseCode : $responseCode');
} on DioError catch (err) {
var responseCode = err.response.statusCode;
print(responseCode);
}
setState(() {
uploading = false;
progressString = "100% Completed ";
print(progressString);
});
}