HichemProgramming
HichemProgramming

Reputation: 61

Flutter: Disabling Android System Back Button using WillPopScope NOT WORKING

I want to disable the android system Back Button but I cannot do it, I used WillPopScope, but I don`t know why is not working, can you help me? I put a part of the code

Thank you

  Widget build(BuildContext context) {
    CollectionReference users = FirebaseFirestore.instance.collection("users");
    provider = Provider.of<CentralStoreScreenProvider>(context);
    return WillPopScope(
        onWillPop: () async {
          return false;
        },
        child:
        Scaffold(
        appBar: searchBar!.build(context),
    drawer: /**/
        body: GoogleMap(
          mapType: MapType.hybrid,
          initialCameraPosition: initialLocation,
          markers: Set.of((marker != null) ? [marker!] : []),
          circles: Set.of((circle != null) ? [circle!] : []),
          onMapCreated: (GoogleMapController controller) {
            _controller = controller;
          },
        ),
        floatingActionButton: FloatingActionButton(
            child: Icon(Icons.location_searching),
            onPressed: () {
              getCurrentLocation();

            }),

      )
    );



  } 

Upvotes: 3

Views: 1926

Answers (1)

Anandh Krishnan
Anandh Krishnan

Reputation: 5984

Make sure you are in statful widget, and check this code for reference. this is my code this works well

   class DashboardScreen extends StatefulWidget {
  @override
  _MainScreenState createState() => _MainScreenState();
}

class _MainScreenState extends State<DashboardScreen> {
  
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () async {
          return false;
        },
        child: Scaffold(
          floatingActionButton: FloatingActionButton(
              child: Icon(Icons.location_searching), onPressed: () {}),
        ));
  }

Upvotes: 1

Related Questions