Show snackbar from scaffold inside onPressed callback on Floating Action Button

前端 未结 3 1101
星月不相逢
星月不相逢 2021-02-13 14:05

I am trying to call

Scaffold.of(context).showSnackBar(SnackBar(
  content: Text(\"Snack text\"),
));

inside onPressed of fl

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-13 14:22

    Add a Globalkey of the Scaffold state and use that to display snack bar as below,

    GlobalKey scaffoldState;
    
    Scaffold {
    key: scaffoldState,
    ....
    
    
    scaffoldState.currentState.showSnackBar(new SnackBar(content: new Text('Hello!')));
    

提交回复
热议问题