How to change the function of the back button in the flutter app bar?

How to change the function of the back button in the flutter app bar?

Problem Description:

How can I change the function of the back button in de app bar? So I want to go to the home page when you click on the back button how can I make this work?

 appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        leading: const BackButton(
          color: Color(0xFFFD879A),
        ),
      ),

Solution – 1

Add onPressed to your leading:

leading: BackButton(
  color: Color(0xFFFD879A),
  onPressed: () {
    // do your navigate here

    print("back click");
  },
),

Solution – 2

 appBar: AppBar(
    backgroundColor: Colors.transparent,
    elevation: 0,
    leading: const BackButton(
      color: Color(0xFFFD879A),
      onPressed: () {
          // execute this on pressed...
      },
    ),
  ),
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject