Home > Back-end >  what is best ways to set loading/progress in full screen in flutter
what is best ways to set loading/progress in full screen in flutter

Time:04-02

I want to add full screen loading with grey background when start loading for FullScreen covered in it.

In Past I used Stack() widget for it but stack was not covered my App bar.

and I Think add Scaffold in Stack widget is not good way to do this

CodePudding user response:

you can try the following

Have a utils class

    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)),
      ),
    );
  }
      }

Then use it where you need loading

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),
                        ),
                      )

CodePudding user response:

Center(
   child: CircularProgressIndicator(),
),

CodePudding user response:

Adding a showDialog() will make sure that it covers appBar inside Scaffold, add this when you want the loader to appear:

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