什么是在flutter中设置全屏加载/进度的最佳方法



当开始加载包含的FullScreen时,我想添加灰色背景的全屏加载。

在过去,我使用了Stack小部件,但堆栈没有覆盖我的应用程序栏。

我认为在Stack窗口小部件中添加Scaffold并不是实现的好方法

您可以尝试以下

拥有utils类

class Utils {
late BuildContext context;
Utils(this.context);
// this is where you would do your fullscreen loading
Future<void> startLoading() async {
return await showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return const SimpleDialog(
elevation: 0.0,
backgroundColor: Colors.transparent, // can change this to your prefered color
children: <Widget>[
Center(
child: CircularProgressIndicator(),
)
],
);
},
);
}
Future<void> stopLoading() async {
Navigator.of(context).pop();
}
Future<void> showError(Object? error) async {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
action: SnackBarAction(
label: 'Dismiss',
onPressed: () {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
},
),
backgroundColor: Colors.red,
content: Text(handleError(error)),
),
);
}
}

然后在需要加载的地方使用它

ElevatedButton(
onPressed: () async {

FocusScope.of(context).unfocus();
if (widget.formkey!.currentState!.validate()) {
Utils(context).startLoading();
widget.formkey!.currentState!.save();
widget.authProvider
.signInWithEmailAndPassword(
widget.textController.text.trim(),
widget.passwordController.text.trim())
.then((user) async {
// do something with user
Utils(context).stopLoading();
}).onError(
(error, stackTrace) {
Utils(context).showError(error);
Utils(context).stopLoading();
},
);
}
},
child: const Text(
AppConstants.kBtnLogin,
style: TextStyle(color: Colors.white),
),
)

添加showDialog((将确保它覆盖Scaffold内的appBar,当您希望加载程序出现时添加它:

showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return Container(
color: Colors.grey,
child: Center(
child: CircularProgressIndicator(
color: Colors.white,
),
),
);
},
);
Center(
child: CircularProgressIndicator(),
),

扩展(子项:列(mainAxisAlignment:mainAxisAlignment。center,儿童:[Center(child:CircularProgressIndicator(((,],),);

最新更新