简体   繁体   English

Flutter:带有 OnPressed 选项的 Snackbar

[英]Flutter: Snackbar with OnPressed option

I am using the below code for Snackbar.我正在为 Snackbar 使用以下代码。

void _showSnackBar(BuildContext context, String message, Color color) {
    final snackBar = SnackBar(
      duration: Duration(seconds: 3),
      backgroundColor: color,
      content: Text(message),
    );
    _scaffoldKey.currentState.showSnackBar(snackBar);
  }

This is how am i calling it wherever required.这就是我在需要的地方如何称呼它。

_showSnackBar(context,'Account created Successfully.',Colors.green);

It is working fine totally fine.它工作得很好。 I don't see any issues.我没有看到任何问题。

But now i need to add Onclick option in Snackbar.但现在我需要在 Snackbar 中添加 Onclick 选项。 Like below code.就像下面的代码。

action: SnackBarAction(
              label: 'Undo',
              onPressed: () {
                // Some code to undo the change.
              },
            ),

I am not sure how to add it and where to add it.我不知道如何添加它以及在哪里添加它。

Please suggest.请建议。

Edit: Little more explanation of what I want to achieve.编辑:关于我想要实现的目标的更多解释。

void _showSnackBar(BuildContext context, String message, Color color) {

how can i pass the parameters for OnPressed event?如何传递 OnPressed 事件的参数? That's the issue i am getting.这就是我遇到的问题。

I think this can be achived with:我认为这可以通过以下方式实现:

SnackBar(
  content: Text('Yay! A SnackBar!'),
  action: SnackBarAction(
    label: 'Undo',
    onPressed: () {
      // Some code to undo the change.
    },
  ),
)

This can be achieved with the following code:这可以通过以下代码实现:

    void _showSnackBar(BuildContext context, String message, Color color) {
        final snackBar = SnackBar(
          duration: Duration(seconds: 3),
          backgroundColor: color,
          content: Text(message),
          action: SnackBarAction(
              label: 'Undo',
              onPressed: (){}
          )
        );
        _scaffoldKey.currentState.showSnackBar(snackBar);
      }

You can find more info on SnackBar's here: https://flutter.dev/docs/cookbook/design/snackbars您可以在此处找到有关 SnackBar 的更多信息: https://flutter.dev/docs/cookbook/design/snackbars

You can pass any wanted parameters to your custom _showSnackBar method, and inside this method you can do whatever you want with these parameters.您可以将任何想要的参数传递给您的自定义_showSnackBar方法,并且在此方法中,您可以使用这些参数做任何您想做的事情。

As I see it:照我看来:

    void _showSnackBar(BuildContext context, String message, Color color) {
            final snackBar = SnackBar(
              duration: Duration(seconds: 3),
              backgroundColor: color,
              content: Text(message),
              action: SnackBarAction(
                  label: 'Undo',
                  onPressed: (){
                    print(color);
   /// do what you want with passed color or message
                   }
              )
            );
            _scaffoldKey.currentState.showSnackBar(snackBar);
          }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM