【问题标题】:How can I show a snackbar after navigator.pop(context) in Flutter?如何在 Flutter 中的 navigator.pop(context) 之后显示小吃栏?
【发布时间】:2018-12-18 00:06:06
【问题描述】:

我们需要关闭一个屏幕并打开第二个屏幕。并在第一个屏幕的第二个屏幕上显示小吃栏。

我尝试使用Navigator.push,但此屏幕已打开,出现此错误

错误状态:Stream 已被监听。

【问题讨论】:

    标签: dart flutter navigator snackbar


    【解决方案1】:

    我建议你使用 Flushbar 插件代替 Snackbar,它易于使用,它会处理所有事情,你可以在很大程度上自定义它。 Snackbar 需要一个脚手架祖先才能工作,但是 Flushbar 没有,它自己处理所有额外的东西,并提供了大量很酷的功能。

    Flushbar Plugin here

    【讨论】:

    • 谢谢!而且我找不到在用户按下此栏上的“确定”按钮后我们如何关闭刷新栏。也许你知道?
    • flushbar 有一个函数 .dismiss() 。当你按下 ok 调用那个函数中的一个函数时,这样做flushbar.dismiss();
    • 是的,我试试。但它不起作用(而且我不明白为什么。
    • flushbar = Flushbar( title: "Some", message: "text", duration: Duration(seconds: 3), mainButton: FlatButton( onPressed: (){ flushbar.dismiss(); }, child: Text( "OK" ), ), )..show(context);
    • Flushbar 已停产,现在有什么想法吗?
    【解决方案2】:

    我不确定我是否 100% 了解您的用例,但通过从屏幕返回结果并将参数传递给新路由,您基本上可以解决任何情况。

    让我们举个例子。 A 是原始屏幕,然后你推屏幕 B。现在你看到 B,在那里执行一些操作,弹出它,你回到屏幕 A,你想显示一个快餐栏。

    当您pop 时,您可以返回一个结果并从“父”屏幕处理该结果,如"Return data from a screen" 食谱中所述。

    当您收到结果时,您可以显示快餐栏通知,或者(如果有第三个屏幕),您可以pass that result to another screen as argument

    【讨论】:

    • 希望您使用库/包回答选择的解决方案
    【解决方案3】:
    showSubmitRequestSnackBar(BuildContext context) async {
    
      Flushbar(
        flushbarPosition: FlushbarPosition.BOTTOM,
        message: "Request Successfully Saved",
        icon: Icon(
          Icons.info_outline,
          size: 28.0,
          color: Colors.red,
        ),
        backgroundColor: Colors.red,
        duration: Duration(seconds: 5),
        leftBarIndicatorColor: Colors.red,
    
      )
        ..show(context).then((r)=> Navigator.push(
            context, MaterialPageRoute(builder: (context) => ListPage(""))));
    }
    

    【讨论】:

    • 这显示了 Flushbar 被关闭后(在本例中为 5 秒后),这在大多数情况下不是您想要的(如果不是所有情况)。
    • 我们无法使用 Flushbar,因为它已停产。 pub.dev/packages/flushbar
    【解决方案4】:

    屏幕 A

    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => BScreen(
          callbackFunction: (String resMessage){
            print(resMessage);
          },
        ),
      ),
    );
    

    屏幕 B

    class BScreen extends StatefulWidget {
      final Function callbackFunction;
    
      BScreen({required this.callbackFunction});
    
      @override
      _BScreenState createState() => _BScreenState();
    }
    
    
    class _BScreenState extends State<BScreen> {
      // 
      _performTask(){
        // add your task here
        // ..........
        String msg = 'Task has been added';
        widget.callbackFunction(msg);
        Navigator.of(context).pop();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: ElevatedButton(
              onPressed: () {
                _performTask();
              },
              child: Text('Click Here'),
            ),
          ),
        );
      }
    }
    

    这个解决方案对我有用。乐于帮助他人 :) 感谢您提出这个问题。

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 2019-08-28
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 2021-08-15
      • 1970-01-01
      相关资源
      最近更新 更多