Zabi Noori
Zabi Noori

Reputation: 31

flutter snackbar (with dismiss button)is not disappearing

I have created a snackbar with a button inside snacbar named 'undo' and also i have set snackbar duration property to 2 seconds but the snackbar is not hiding after to 2 seconds if i don't use the 'undo' button in snacbar action the duration property is working but with this button it is not working

I tried to hide snackbar automatically after 2 second with a button inside it called 'undo' or 'dismiss' but it is not hiding and here is the sample code : ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: const Text( 'the item was successfully deleted!'), action: SnackBarAction( label: 'undo', onPressed: () async { await SqlHelper.createItemWithID( deletedItem![0]['id'], deletedItem![0]['title'], deletedItem![0]['description'], deletedItem![0]['createdAt']); counterFordeleteditem -= 1; await RecyclebinHHHHHelper.deleteItemm( deletedItem![0]['id']);

                              refresh();
                            },
                          ),
                        ));
                      
                        Future.delayed(
                            Duration(seconds:2), () {
                         
                            ScaffoldMessenger.of(context)
                                .hideCurrentSnackBar();
                        });

Upvotes: 1

Views: 375

Answers (1)

Md. Fazle Rabbi
Md. Fazle Rabbi

Reputation: 65

snackbar.setAction("Undo", new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // Handle undo action here
    }
});
snackbar.show();

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        if (snackbar.isShown()) {
            snackbar.dismiss();
        }
    }
}, 2000);

Upvotes: -1

Related Questions