的值以备将来使用

的值以备将来使用

本文介绍了似乎无法保存 TextField 的值以备将来使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的登录屏幕上:要求用户输入登录名和密码.我将这些值保存在两个变量中:电子邮件和密码.然后用户必须点击一个按钮才能实际登录.问题是,由于某种原因(我真的无法弄清楚......)当用户点击按钮时,电子邮件和密码总是为空......

On my login screen : user is asked to type in login and password. I save these value in two variables : email and password. Then the user must tap a button to actually log in.The problem is that for some reason (that I really can't figure out...) email and password are always empty when user hits the button....

完整代码如下:

class SeConnecterScreen extends StatelessWidget {
  static const String id = 'se_connecter_screen';

  @override
  Widget build(BuildContext context) {
    var uD = Provider.of<UdProvider>(context);
    String email = '';
    String passWord = '';
    final Connectivity _connectivity = Connectivity();
    var scaffold = Scaffold(
      extendBody: true,
      extendBodyBehindAppBar: true,
      backgroundColor: Colors.indigo[900],
      body: Column(
        children: [
          Expanded(
            flex: 4,
            child: Container(
              margin: const EdgeInsets.only(bottom: 12),
              decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage('images/start2.jpg'), fit: BoxFit.cover),
                borderRadius: BorderRadius.only(
                  bottomLeft: Radius.circular(50),
                ),
              ),
              height: MediaQuery.of(context).size.height / 4,
            ),
          ),
          Expanded(
              flex: 6,
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Container(
                  height: MediaQuery.of(context).size.height -
                      (MediaQuery.of(context).size.height / 4),
                  padding: EdgeInsets.all(8.0),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    border: Border.all(
                      width: 1.0,
                      color: Colors.white,
                    ),
                    borderRadius: BorderRadius.circular(10.0),
                  ),
                  child: SingleChildScrollView(
                    child: Padding(
                      padding: const EdgeInsets.all(16.0),
                      child: Column(
                        children: [
                          Text(
                            'WORD CHAMPIONS',
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                color: Colors.indigo[900],
                                fontSize: 28.0,
                                fontWeight: FontWeight.bold),
                          ),
                          SizedBox(
                            height: 50,
                          ),
                          TextField(
                            keyboardType: TextInputType.emailAddress,
                            decoration: InputDecoration(
                                focusColor: Colors.white,
                                border: OutlineInputBorder(),
                                labelText: 'Tape ton adresse email'),
                            onChanged: (value) {
                              email = value;
                              print(email);
                            },
                          ),
                          SizedBox(
                            height: 25.0,
                          ),
                          TextField(
                            obscureText: true,
                            keyboardType: TextInputType.name,
                            decoration: InputDecoration(
                                focusColor: Colors.white,
                                border: OutlineInputBorder(),
                                labelText: 'Tape ton mot de passe'),
                            onChanged: (value) {
                              passWord = value;
                              print(passWord);
                            },
                          ),
                          SizedBox(
                            height: 35.0,
                          ),
                          ElevatedButton.icon(
                              label: Text('Se connecter'),
                              icon: Icon(Icons.library_add_check_rounded),
                              style: ElevatedButton.styleFrom(
                                minimumSize: Size(30, 45),
                                primary: Colors.green[800],
                                onPrimary: Colors.white,
                              ),
                              onPressed: () async {
                                print('Email : $email');
                                print('passWord : $passWord');
                                if (await _connectivity.checkConnectivity() ==
                                    ConnectivityResult.none) {
                                  showDialog(
                                    context: context,
                                    builder: (ctx) => AlertDialog(
                                      title: Text(
                                        'Oups!',
                                        style: TextStyle(fontSize: 22.0),
                                      ),
                                      content: Text(
                                        'Il faut te connecter à Internet !',
                                        style: TextStyle(fontSize: 18.0),
                                      ),
                                      actions: [
                                        TextButton(
                                          onPressed: () {
                                            Navigator.pushReplacementNamed(
                                                context, StartingScreen.id);
                                          },
                                          child: Text(
                                            'OK',
                                            style: TextStyle(fontSize: 18.0),
                                          ),
                                        ),
                                      ],
                                    ),
                                  );
                                } else {
                                  String accountVerif =
                                      await uD.checkAccountId(email, passWord);
                                  if (accountVerif == '') {
                                    DialogBuilder(context).showLoadingIndicator(
                                        text: 'Téléchargement des bases',
                                        color: Colors.black45);
                                    await uD.downLoadUserInfo(email);
                                    DialogBuilder(context).hideOpenDialog();
                                    Navigator.pushReplacementNamed(
                                        context, ProfileScreen.id);
                                  } else {
                                    showDialog(
                                      context: context,
                                      builder: (ctx) => AlertDialog(
                                        title: Text(
                                          'Oups!',
                                          style: TextStyle(fontSize: 22.0),
                                        ),
                                        content: Text(
                                          'Ce compte n\'existe pas, ou le mot de passe est incorrect.',
                                          style: TextStyle(fontSize: 18.0),
                                        ),
                                        actions: [
                                          TextButton(
                                            onPressed: () {
                                              Navigator.pushReplacementNamed(
                                                  context, StartingScreen.id);
                                            },
                                            child: Text(
                                              'OK',
                                              style: TextStyle(fontSize: 18.0),
                                            ),
                                          ),
                                        ],
                                      ),
                                    );
                                  }
                                }
                              }),
                        ],
                      ),
                    ),
                  ),
                ),
              )),
        ],
      ),
    );
    return scaffold;
  }
}

推荐答案

请尝试在构建方法之外定义密码和电子邮件变量.它可能会解决问题.

Please Try to define you password and email variables outside build method. It may solve issue.

看到它对我有用,也许你应该这样做

See it works for me, May be you should do

  • 停止执行
  • 运行'flutter clean'
  • 运行flutter pub get"
  • 执行它

这篇关于似乎无法保存 TextField 的值以备将来使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:56