Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show several snackbar without overlapping

Can I display multiple messages on the screen without overlapping others with snackBar?

I have a service to display messages in my application, but the problem is that when more than one event occurs that I need to display the message on the screen, the message is overwritten by the last message to be displayed.

I need a good way of not overlapping my messages and displaying one underneath the other without replacing the others.

I would like it to work the same way a Toast works, displaying one message underneath the other without overlapping.

The way I'm doing it below, only displays one message at a time on the screen.

snack-message.service.ts:

  horizontalPosition: MatSnackBarHorizontalPosition = 'center';
  verticalPosition: MatSnackBarVerticalPosition = 'top';

  constructor(
    public snackBar: MatSnackBar){}

  showMessage(message: string) {
    this.snackBar.open(message, 'Close', {
      duration: 5000,
      horizontalPosition: this.horizontalPosition,
      verticalPosition: this.verticalPosition,
    });
  }
like image 266
Luiz Ricardo Cardoso Avatar asked Mar 20 '18 19:03

Luiz Ricardo Cardoso


People also ask

How many Snackbars can be displayed at once?

Frequency. Only one snackbar may be displayed at a time.

How long should Snackbar be visible?

Snackbars appear without warning, and don't require user interaction. They automatically disappear from the screen after a minimum of four seconds, and a maximum of ten seconds.

How do I use snackbar in service?

Import the snackBarService and inject it inside the constructor of the component, in which you want to use the Snackbar. This will create an instance of the service say snackBService. Now call the openSnackBar function wherever it is required, with the help of snackBService.

What is the use of Snackbar?

Snackbar in android is a new widget introduced with the Material Design library as a replacement of a Toast. Android Snackbar is light-weight widget and they are used to show messages in the bottom of the application with swiping enabled. Snackbar android widget may contain an optional action button.


1 Answers

I think the Snack Bar is only possible to use once.

Check the documentation:

https://material.io/components/snackbars#usage

Only one snackbar may be displayed at a time.

like image 177
Rafael Ferreira Avatar answered Sep 17 '22 20:09

Rafael Ferreira