Display SnackBar in Flutter

前端 未结 15 1519
一个人的身影
一个人的身影 2020-12-04 19:28

I want to display a simple SnackBar inside Flutter\'s stateful widget. My application creates new instance of MaterialApp with a stateful widget ca

15条回答
  •  无人及你
    2020-12-04 19:48

    There's a better and cleaner way to display a Snackbar in flutter. I found it the hard way and sharing so that maybe it's helpful for someone else.

    No need to change in your main app part

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return new MaterialApp(
        title: 'MyApp',
        theme: new ThemeData(
        primarySwatch: Colors.orange),
        home: new MainPage());
      }
    }
    

    Page State code is where things will change.

    We know Flutter provides Scaffold.of(context).showSnackBar. However, the context should be the context of a descendant of a Scaffold, and not the context that includes a Scaffold. In order to avoid error, we need to use a BuildContext for the body of the Scaffold, and store it in a variable, as below.

    class MainPageState extends State {                                                                         
    BuildContext scaffoldContext;                                                                                                                                                                                                
    
    @override                                                                                                           
    Widget build(BuildContext context) {                                                                                
    
    return new Scaffold(                                                                                              
        backgroundColor: Colors.grey,                                                                                 
        appBar: new AppBar(                                                                                           
          title: const Text(APP_TITLE),                                                                               
        ),                                                                                                             
        body: new Builder(builder: (BuildContext context) {                                                           
          scaffoldContext = context;                                                                                  
          return new Center(
               child: new Text('Hello World', style: new TextStyle(fontSize: 32.0)),
             );                                                                                
        }));                                                                                                           
    }                                                                                                                   
    
    
    void createSnackBar(String message) {                                                                               
      final snackBar = new SnackBar(content: new Text(message),                                                         
      backgroundColor: Colors.red);                                                                                      
    
      // Find the Scaffold in the Widget tree and use it to show a SnackBar!                                            
      Scaffold.of(scaffoldContext).showSnackBar(snackBar);                                                              
      }                                                                                                                   
    }     
    

    Now, you can call this function from anywhere and it will display the Snackbar. For example, I am using it to display Internet Connectivity messages.

提交回复
热议问题