Dart 无法在颤振中显示snackbar

Dart 无法在颤振中显示snackbar,dart,flutter,flutter-animation,Dart,Flutter,Flutter Animation,我是个新手,登录失败时会显示一个SnackBar。请检查下面的代码,让我知道如何解决这个问题。我不知道我到底需要在哪里安装builder。您可以向我建议以简单快捷的方式显示SnackBar的最佳解决方案。非常感谢所有愿意帮忙的人 class LoginPage extends StatefulWidget { static String tag = 'login-page'; @override _LoginPageState createState() => new _Log

我是个新手,登录失败时会显示一个
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,
          ],
        ),
      ),
    );
  }
class LoginPage扩展StatefulWidget{
静态字符串标记='登录页面';
@凌驾
_LoginPagentate createState()=>新建;
}
类_loginpagentate扩展状态{
bool passwordVisible=true;
var_scaffoldKey=new GlobalKey();
@凌驾
小部件构建(构建上下文){
最终标志=英雄(
标签:“英雄”,
孩子:圆环星(
背景颜色:颜色。透明,
半径:48.0,
子级:Image.asset('assets/splash.png'),
),
);
/*最终电子邮件=TextFormField(
键盘类型:TextInputType.emailAddress,
自动对焦:错误,
初始值:'mufeez@gmail.com',
装饰:输入装饰(
hintText:“电子邮件”,
内容填充:来自LTRB(20.0,10.0,20.0,10.0)的EdgeInsets,
边框:大纲输入边框(边框半径:borderRadius.circular(32.0)),
),
);
*/
最终密码=TextFormField(
自动对焦:错误,
ObsureText:passwordVisible,//这将动态隐藏文本
装饰:输入装饰(
hintText:“输入密钥”,
内容填充:来自LTRB(20.0,10.0,20.0,10.0)的EdgeInsets,
边框:大纲输入边框(边框半径:borderRadius.circular(32.0)),
后缀:图标按钮(
图标:图标(
//根据passwordVisible状态选择图标
密码可见
?图标。可见性关闭
:图标。可见性,
颜色:主题。背景。原色深,
),
已按下:(){
//更新状态,即切换passwordVisible变量的状态
设置状态(){
密码可见
?passwordVisible=false
:passwordVisible=true;
});
},
),
),
);
最终登录按钮=填充(
填充:边缘组。对称(垂直:16.0),
孩子:升起按钮(
形状:圆形矩形边框(
边界半径:边界半径。圆形(24),
),
onPressed:()异步{
final snackBar=snackBar(内容:Text('Yay!A snackBar!'));
脚手架.of(上下文).showSnackBar(snackBar);
},
填充:边缘设置。全部(12),
颜色:Colors.lightBlueAccent,
子项:文本('登录',样式:TextStyle(颜色:Colors.white)),
),
);
/*最终锻件标签=扁平按钮(
子:文本(
“忘记密码了吗?”,
样式:TextStyle(颜色:Colors.black54),
),
按下:(){},
);
*/
返回脚手架(
背景颜色:Colors.white,
正文:中(
子:ListView(
收缩膜:对,
填充:仅限边设置(左:24.0,右:24.0),
儿童:[
标志
尺寸箱(高度:48.0),
尺寸箱(高度:8.0),
密码,
尺寸箱(高度:24.0),
登录按钮,
],
),
),
);
}

您必须使用脚手架钥匙才能执行此操作

您必须在scaffold小部件中添加的内容

return Scaffold(
      key: _scaffoldKey,
      backgroundColor: Colors.white,
在显示snackbar时,您必须使用scaffold键。 将按下的按钮替换为以下代码

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

您还可以将脚手架主体包装在
Builder()
小部件中

return Scaffold(
  body: body: Builder(
     builder: (BuildContext ctxt) {
        return (your content here);}));
当您想显示Snackbar时,只需使用
Scaffold.of(ctxt)
onPressed

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

您收到错误消息了吗?另外,您可以帮助我了解如何获取textinputlabel字段并将其传递给post query。我只想获取textinput label的文本。如何实现它?您可以澄清您的问题吗?我无法理解它。我想获取密码字段的值。我如何做到这一点?您必须使用form key要做到这一点,请看这个例子以清楚地理解。