ScaffoldMessenger showSnackBar on Navigator.push or .pop?

Viewed 145

As simple as it sounds, what's the current method to display a snackbar through the ScaffoldMessenger after a Navigator.pop into the previous page, where the snackbar is shown on the now current page (previous)?

3 Answers

To return data to the first screen, use the Navigator.pop() method and then show snackbar

  ElevatedButton(
            onPressed: () {
              // Close the screen and return "test!" as the result.
              Navigator.of(context).pop();
              ScaffoldMessenger.of(context)
                ..removeCurrentSnackBar()
                ..showSnackBar(const SnackBar(content: Text('test')));
            },
            child: const Text('close!'),
          )

For more read this article

first show the snackbar using ScaffoldMessenger.of(context) ..hideCurrentSnackBar() ..showSnackBar(SnackBar(content: Text(message))); and later you can use Navigator.pop();

content: Text(response['message']), backgroundColor: primaryColor, dismissDirection: DismissDirection.up, ); ScaffoldMessenger.of(context).showSnackBar(snackBar); Navigator.of(context).pop()

Related