Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter custom appbar back function is not being triggered

I have a custom AppBar that has a function which should handle the OnBackPressed of the screen, but unfortunately is not being triggered and dunno why.

Basically I want that on pressing the back button, to leave the current screen.

@override
Widget build(BuildContext context) {
    return Scaffold(
      appBar: ComponentAppBar(
        showGoBackButton: true,
        onGoBackPressed: () => Get.back(),
      ),
      body: Container(),
    );
}

The custom app bar file:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import 'package:flutter/material.dart';

class ComponentAppBar extends StatefulWidget implements PreferredSizeWidget {
  final bool showGoBackButton;
  final Function onGoBackPressed;

  const ComponentAppBar({
    Key? key,
    required this.showGoBackButton,
    required this.onGoBackPressed,
  }) : super(key: key);

  @override
  _ComponentAppBarState createState() => _ComponentAppBarState();

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}

class _ComponentAppBarState extends State<ComponentAppBar> {

  @override
  Widget build(BuildContext context) {
    return AppBar(
      title: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          //           
        ],
      ),
      leading: widget.showGoBackButton
          ? IconButton(
              onPressed: () => widget.onGoBackPressed,
              icon: const Icon(
                Icons.arrow_back,
                color: Colors.black,
              ),
            )
          : Container(),
      automaticallyImplyLeading: false,
      leadingWidth: !widget.showGoBackButton ? 0 : 30,
    );
  }
}

Pretty straight forward, yet, when pressing the back button nothing happens.

What have I debugged:

  • Instead of Get.back() use Navigator.pop() – same problem
  • Add an output onGoBackPressed: () => { print('testing') } – no output
  • Change onPressed: () => widget.onGoBackPressed to onPressed: () => Get.back(), it works
  • Change onPressed: () => widget.onGoBackPressed to onPressed: () => { print('test directly in class') }, it works

Also, when using the F5 debug tools, it shows like this, which is very strange:

enter image description here

>Solution :

It’s all about callbacks.
You’re receiving a callback as argument (onGoBackPressed), and you need to execute it in order for it to work. There are two ways for doing this:

onPressed: widget.onGoBackPressed, //This should work since you're assigning directly onGoBackPressed as the callback to be executed by onPressed
onPressed: ()=>widget.onGoBackPressed.call(), //This should work since you're calling onGoBackPressed callback. In other words, you're executing it. Before, you were just referencing it, without actually executing

Tip: I’d recommend you to avoid dynamics when typing arguments. So, I recommend you to type specifically what your onGoBackPressed callback will return. This way:

  final void Function() onGoBackPressed;

Which is the same as

  final VoidCallback onGoBackPressed;

Cheers!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading