How to show SnackBar in Flutter

后端 未结 3 1111
不思量自难忘°
不思量自难忘° 2021-01-25 08:23

I want to show a SnackBar Widget when the bottom tab is clicked. I am trying to show it as:

Scaffold.of(context).showSnackBar(new SnackBar(
                cont         


        
3条回答
  •  广开言路
    2021-01-25 09:22

    Change your _handleBottomNavigationBarTap method to take a BuildContext argument.

    void _handleBottomNavigationBarTap(int newValue, BuildContext context) {
      ...
    }
    

    Then change your bottomNavigationBar argument as follows:

    bottomNavigationBar: new Builder(
      builder: (BuildContext context) {
        return new BottomNavigationBar(
          labels: bottomBarLabels,
          onTap: (index) => _handleBottomNavigationBarTap(index, context),
        );
      }
    ),
    

    This ensures that you call to Scaffold.of(context) will be able to find a ScaffoldState that is an ancestor of the context.

提交回复
热议问题