Display SnackBar in Flutter

前端 未结 15 1340
一个人的身影
一个人的身影 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:49

    In case someone is looking to initialise a Snackbar on initState() (in other words when the page loads here is my solution). Also, I assume you already have the _scaffoldKey in place.

    void initState() {
      super.initState();
      Future.delayed(Duration(seconds: 1)).then(
        (_) => _displaySnackbar
      );
    }
    
    // Display Snackbar
    void get _displaySnackbar {
      _scaffoldKey.currentState.showSnackBar(SnackBar(
        duration: Duration(minutes: 1),
        content: Text('Your snackbar message')
      ));
    }
    
    0 讨论(0)
  • 2020-12-04 19:54

    The best way is to create a PageWrapper that you can wrap all of your pages to get the context of the Scaffold widget and avoid experiencing these errors again.

    Here is a sample of the page wrapper:

    import 'package:flutter/material.dart';
    
    class PageWrapper extends StatelessWidget {
    
      Widget page;
    
      WPage(this.page);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.white,
          body: page
        );
      }
    }
    

    Then you can wrap all your pages in the main.dart like these:

    import 'package:flutter/material.dart';
    void main() => runApp(MaterialApp(
      initialRoute: '/login',
      routes: {
        '/login': (context) => PageWrapper(Login()),
        '/home': (context) => PageWrapper(Home())
      }
    ));
    

    Now you can call Scaffold.of(context) anywhere in any page just fine.

    Reference: https://noobieprogrammer.blogspot.com/2020/06/how-to-create-error-alert-or-popup.html

    or watch the video version (5 min. long): https://youtu.be/u9KoFtu0HEY

    0 讨论(0)
  • 2020-12-04 19:59
    Scaffold.of(context).showSnackBar(
         SnackBar(content: Text("Thanks for using snackbar",
         textAlign: TextAlign.center, style: TextStyle(fontSize: 16.0, fontWeight: 
         FontWeight.bold),), duration: Duration(seconds: 2), backgroundColor: Colors.red,)
    );
    
    0 讨论(0)
  • 2020-12-04 20:01

    You can do this in three simple steps.

    1. Make sure you have a key for scaffold. You can create that by writing the below code:

      final _scaffoldKey = GlobalKey<ScaffoldState>();
      

    2.Now you have to mention this key inside your Scaffold, by writing the below line inside scaffold:

    key:_scaffoldKey,
    
    1. Now you can show snackbar by writing:

      final snackBar=SnackBar(
                            content: Text('Your password has been changed successfully'),
                          );
                          _scaffoldKey.currentState.showSnackBar(snackBar);
      
    0 讨论(0)
  • 2020-12-04 20:02

    There's three problems. The first is that you don't have a Scaffold anywhere, and the Scaffold widget is the one that knows how to show snack bars. The second is that you have a key for getting a hold of the scaffold, but you've put it on a Padding instead (and Paddings don't have any knowledge of snack bars). The third is that you've used the key before the widget that it's associated with has had a chance to be initialised, since initState is called before build.

    The simplest solution is to change the home line in your MyApp widget to:

    home: new Scaffold(body: new MyHomePage()),

    ...and then remove all mention of _scaffoldKey and instead use Scaffold.of(context) where you currently have _scaffoldKey.currentState.

    0 讨论(0)
  • 2020-12-04 20:05

    I dit it, work for me :)

    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(
          title: new Text('Demo')
        ),
        body: new Builder(
          // Create an inner BuildContext so that the onPressed methods
          // can refer to the Scaffold with Scaffold.of().
          builder: (BuildContext context) {
            return new Center(
              child: new RaisedButton(
                child: new Text('SHOW A SNACKBAR'),
                onPressed: () {
                  Scaffold.of(context).showSnackBar(new SnackBar(
                    content: new Text('Hello!'),
                  ));
                },
              ),
            );
          },
        ),
      );
    }
    
    0 讨论(0)
提交回复
热议问题