How to show snackBar without Scaffold

前端 未结 3 1884
清歌不尽
清歌不尽 2021-01-18 05:28

I am trying to show snackbar in my app to notify User but without scaffold it shows me error. My current code is:

scaffoldKey.currentState?.showSnackBar(
           


        
相关标签:
3条回答
  • 2021-01-18 05:43

    No, It is not possible. Snackbar is part of Scaffold. It must have a Scaffold parent. Snackbar

    Inside Scaffold parent, you can do like below

    BuildContext con=context;
    final snackBar = SnackBar(content: Text(message));
    Scaffold.of(con).showSnackBar(snackBar);
    
    0 讨论(0)
  • 2021-01-18 05:48

    It can get the current context and show snackbar like this:

    void _showToast(BuildContext context) {
      final scaffold = Scaffold.of(context);
      scaffold.showSnackBar(
        SnackBar(
          content: const Text('Updating..'),
        ),
      );
    
    this._showToast(context);
    
    0 讨论(0)
  • 2021-01-18 05:56

    Use https://pub.dev/packages/get

    then you can simply call the Get.snackbar() to show the snackbar where you want it to be displayed.

    Get.snackbar('Hi', 'i am a modern snackbar');
    

    NOTE

    You can use this package when you don't have the context too.

    0 讨论(0)
提交回复
热议问题