似乎无法保存 TextField 的值以备将来使用

问题描述

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

完整代码如下:

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,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',],);
                                } 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!',content: Text(
                                          'Ce compte n\'existe pas,ou le mot de passe est incorrect.',actions: [
                                          TextButton(
                                            onpressed: () {
                                              Navigator.pushReplacementNamed(
                                                  context,StartingScreen.id);
                                            },child: Text(
                                              'OK',);
                                  }
                                }
                              }),)),);
    return scaffold;
  }
}

解决方法

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

enter image description here

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

  • 停止执行
  • 运行'flutter clean'
  • 运行“flutter pub get”
  • 执行它
,

在覆盖下面而不是在构建方法下面定义变量

,

向其中添加文本编辑控制器 以便您的问题得到解决