Home > Software engineering >  ScaffoldMessenger showSnackBar on Navigator.push or .pop?
ScaffoldMessenger showSnackBar on Navigator.push or .pop?

Time:05-17

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)?

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

Use push or pop after the snackbar `

final snackBar = SnackBar( content: Text(response['message']), backgroundColor: colors.red, dismissDirection: DismissDirection.up, );

ScaffoldMessenger.of(context).showSnackBar(snackBar);

Navigator.of(context).pop()`

  • Related