无法在颤动中显示小吃店

unable to display snackbar in flutter

我是 flutter 的新手,一直停留在登录失败时显示 SnackBar 的点上。请检查下面的代码,让我知道如何解决这个问题。我不知道我需要 builder 的确切位置。您可以向我建议以简单快捷的方式显示 SnackBar 的最佳解决方案。非常感谢所有愿意提供帮助的人。

class LoginPage extends StatefulWidget {
  static String tag = 'login-page';
  @override
  _LoginPageState createState() => new _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {

  bool passwordVisible = true;
  var _scaffoldKey = new GlobalKey<ScaffoldState>();


  @override
  Widget build(BuildContext context) {

    final logo = Hero(
      tag: 'hero',
      child: CircleAvatar(
        backgroundColor: Colors.transparent,
        radius: 48.0,
        child: Image.asset('assets/splash.png'),
      ),
    );

   /* final email = TextFormField(
      keyboardType: TextInputType.emailAddress,
      autofocus: false,
      initialValue: 'mufeez@gmail.com',
      decoration: InputDecoration(
        hintText: 'Email',
        contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
        border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
      ),
    );
*/
    final password = TextFormField(
      autofocus: false,
      obscureText: passwordVisible,//This will obscure text dynamically
      decoration: InputDecoration(
        hintText: 'Enter Secret Key',
        contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
        border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),

        suffixIcon: IconButton(
          icon: Icon(
            // Based on passwordVisible state choose the icon
            passwordVisible
                ? Icons.visibility_off
                : Icons.visibility,
            color: Theme.of(context).primaryColorDark,
          ),
          onPressed: () {
            // Update the state i.e. toogle the state of passwordVisible variable
            setState(() {
              passwordVisible
                  ? passwordVisible = false
                  : passwordVisible = true;
            });
          },
        ),
      ),
    );


    final loginButton = Padding(
      padding: EdgeInsets.symmetric(vertical: 16.0),
      child: RaisedButton(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(24),
        ),

        onPressed: () async {


          final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
          Scaffold.of(context).showSnackBar(snackBar);
        },
        padding: EdgeInsets.all(12),
        color: Colors.lightBlueAccent,
        child: Text('Log In', style: TextStyle(color: Colors.white)),
      ),
    );

   /* final forgotLabel = FlatButton(
      child: Text(
        'Forgot password?',
        style: TextStyle(color: Colors.black54),
      ),
      onPressed: () {},
    );
*/
    return Scaffold(
      backgroundColor: Colors.white,
    body: Center(
        child: ListView(
          shrinkWrap: true,
          padding: EdgeInsets.only(left: 24.0, right: 24.0),
          children: <Widget>[
            logo,
            SizedBox(height: 48.0),
            SizedBox(height: 8.0),
            password,
            SizedBox(height: 24.0),
            loginButton,
          ],
        ),
      ),
    );
  }

你必须使用脚手架密钥才能这样做。

您必须在脚手架小部件中添加什么。

return Scaffold(
      key: _scaffoldKey,
      backgroundColor: Colors.white,

并且在显示 snackbar 时你必须使用 scaffold 键。 用下面的代码替换你的 onPressed。

 onPressed: () async {
          final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
          _scaffoldKey.currentState.showSnackBar(snackBar);
        },

您还可以将 Scaffold 主体包裹在 Builder() 小部件中。

return Scaffold(
  body: body: Builder(
     builder: (BuildContext ctxt) {
        return (your content here);}));

并且当想要显示 Snackbar juste 时使用 Scaffold.of(ctxt) onPressed

onPressed: () {
  Scaffold.of(ctxt).showSnackBar(
    SnackBar(content:Text('My snack')));
 },