skip to Main Content

When we use below code after flutter version 3.12.0 we are getting deprecated message, what to use insted of this now?

WillPopScope(
          onWillPop: () {
            setStatusBarColor(statusBarColorPrimary,statusBarIconBrightness: Brightness.light);
            finish(context);
            return Future.value(true);
          },)

2

Answers


  1. Chosen as BEST ANSWER

    After flutter version 3.12.0 pre WillPopScope is deprecated.

    Now you can use PopScope as below

     return PopScope(
    canPop:true,//When false, blocks the current route from being popped.
    onPopInvoked: (didPop) {
                //do your logic here
                setStatusBarColor(statusBarColorPrimary,statusBarIconBrightness: Brightness.light);
               // do your logic ends
               return;
            },
              child: 
             // your other ui or logic
             Scaffold(//other child and ui etc)
    )
    

  2. Any ideas how it use with this logic?

    WillPopScope(
      onWillPop: () => _onWillPop(context, model),
      child: anyChildWidget,
    ),
    
    Future<bool> _onWillPop(
      BuildContext context,
      MyViewModel model,
    ) async {
        if (model.oneFieldIsFlled) {
          await MyUiKit.showCupertinoDialog(
            context: context,
            title: "Any title",
            barrierDismissible: false,
            description: "Any description",
            actions: [
              CustomCupertinoDialogAction(
                text: "Cancel",
                isDefaultAction: true,
                onPressed: () {
                  model.shouldClose = false;
                  Navigator.pop(context);
                },
              ),
              CustomCupertinoDialogAction(
                text: "Close",
                onPressed: () {
                  model.shouldClose = true;
                  Navigator.pop(context);
                },
              ),
            ],
          );
          return Future.value(model.shouldClose);
        } else {
          return Future.value(true);
        }
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search