jonataslaw / getx

Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
MIT License
10.39k stars 1.63k forks source link

When using GetMaterialApp with PopScope(canPop: false), pressing the physical back button still results in being navigated back. #3206

Open ddxl123 opened 2 months ago

ddxl123 commented 2 months ago
class FlutterTest extends StatefulWidget {
  const FlutterTest({super.key});

  @override
  State<FlutterTest> createState() => _FlutterTestState();
}

class _FlutterTestState extends State<FlutterTest> {
  @override
  Widget build(BuildContext context) {
    return const GetMaterialApp(home: A());
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: TextButton(
          child: const Text('to B'),
          onPressed: () {
            Get.to(() => const B());
          },
        ),
      ),
    );
  }
}

class B extends StatefulWidget {
  const B({super.key});

  @override
  State<B> createState() => _BState();
}

class _BState extends State<B> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: const PopScope(
        canPop: false,
        child: Text("B Page"),
      ),
    );
  }
}

If you use MaterialApp with Navigator.push, you can prevent going back.