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

Har*_*k K 0 dart flutter flutter-layout

我想在开始加载时添加带有灰色背景的全屏加载以FullScreen覆盖其中。

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

我认为添加ScaffoldStack部件并不是一个好方法

Tin*_*son 6

你可以尝试以下方法

有一个 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)),
      ),
    );
  }
      }
Run Code Online (Sandbox Code Playgroud)

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

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),
                        ),
                      )
Run Code Online (Sandbox Code Playgroud)