How to Disable Back Button in Flutter

In this example, we are going to show you how to disable the device back button in the Flutter app. You may need to deactivate the back button where you don't want to quit your app by pressing the back button on the device. see the example below:

WillPopScope(
    onWillPop: () async{
      return false;
    },
    child:Scaffold()
)

You need to wrap your whole widget tree with WilPopScope and pass a function that returns "false" on onWillPop attribute. If you want to override the back button and show exit confirmation then see this example: How to override Back Button and Show Exit Confirm in Flutter App

Your Widget build will be like this:

Widget build(BuildContext context) { 
    return  WillPopScope(
         onWillPop: () async{
           return false;
         },
         child:Scaffold()
       );
}

See this also: How to Make Double Press Back Button to Exit on Flutter App

import 'package:flutter/material.dart';

void main(){
  runApp(MyApp());
}

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatefulWidget{
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {  
  @override
  Widget build(BuildContext context) { 
    return  WillPopScope(
         onWillPop: () async{
           return false;
         },
         child:Scaffold(
              appBar: AppBar(
                title: Text("Disable Back Button"),
                backgroundColor: Colors.redAccent
              ),
              body: Container()
         )
       );
  }
}

In this way, you can disable the back button in the Flutter app. 

No any Comments on this Article


Please Wait...