Search code examples
androidflutterdartback

Flutter PopScope is not working whlie using popUntil


class MainScreen extends StatelessWidget {
  const MainScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false, // false or true
      onPopInvoked: (bool didPop) {
        Navigator.popUntil(
            context, ModalRoute.withName(RouterHelper.logInScreen));
      },
      child: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          backgroundColor: Colors.white,
          elevation: 0,
        ),
        body: SafeArea(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(
                width: 312,
                child: 'Main Screeen'.toText(
                  textAlign: TextAlign.center,
                  maxLine: 2,
                  fontSize: 18,
                  fontFamily: poppinsMedium,
                  color:Colors.black,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

whlie we are using canPop false or true both have same senario that popUntil is not working in onPopInvoked.

I have tried to change canPop: true, but still get same result it will cause crash the app. and route is not being routed backword. And also I use canPop: false and write

canPop: false,
onPopInvoked: (bool didPop) {
        Navigator.pop(context);
}

in PopScope same causing the crash of the app. I can't be able to use Navigator.pop and Navigator.popUntil in PopScope


Solution

  • You can try this new version. may be it's useful for you.

    PopScope(
              canPop: false,
              onPopInvoked: (bool didPop) {
                if (didPop) {
                  return;
                }
                 Navigator.popUntil(context, ModalRoute.withName(RouterHelper.logInScreen));
              },
      ),