未定义的名称“更改按钮”。尝试将名称更正为已定义的名称,或定义名称

Posted

技术标签:

【中文标题】未定义的名称“更改按钮”。尝试将名称更正为已定义的名称,或定义名称【英文标题】:Undefined name 'changeButton'. Try correcting the name to one that is defined, or defining the name 【发布时间】:2021-11-28 12:34:52 【问题描述】:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dev/utils/routes.dart';


 class LoginPage extends StatefulWidget 
  bool changeButton = false;

  @override
  State<LoginPage> createState() => _LoginPageState();



 class _LoginPageState extends State<LoginPage> 
  get name => null;



  @override
  Widget build(BuildContext context) 
    return Material(
        color: Colors.white,
        child: SingleChildScrollView(
          child: Column(
            children: [
              Image.asset(
                "assets/images/login_image.png",
                fit: BoxFit.cover,
              ),
              SizedBox(
                height: 20.0,
              ),
              Text(
                "Welcome",
                style: TextStyle(
                  fontSize: 28,
                  fontWeight: FontWeight.bold,
                ),
              ),

              SizedBox(
                height: 20.0,
              ),
              Padding(
                padding: const EdgeInsets.symmetric(
                    vertical: 16.0, horizontal: 32.0),
                child: Column(
                  children: [
                    TextFormField(
                      decoration: InputDecoration(
                        hintText: "Enter username",
                        labelText: "Username",
                      ),
                      onChanged: (value) 
                        satState() 
                      ,
                    ),
                    TextFormField(
                      obscureText: true,
                      decoration: InputDecoration(
                        hintText: "Enter Password",
                        labelText: "Password",
                      ),
                    ),
                    SizedBox(
                      height: 40.0,
                    ),
                    
                    InkWell(
                     onTap: ()
                       setState(() 
                         changeButton = true;
                       );
                      //  Navigator.pushNamed(context, MyRoutes.homeRoute);
                     ,
                      child: AnimatedContainer(
                        duration: Duration(seconds: 1),
                        width: 150,
                        height: 40,
                        
                        alignment: Alignment.center,
                        child: Text(
                          "login",
                          style: TextStyle(
                            color: Colors.white, 
                            fontWeight:FontWeight.bold,
                             fontSize: 18
                             ),  
                          ),
                          decoration: BoxDecoration(
                          color: Colors.deepPurple,
                          borderRadius: BorderRadius.circular(8)
                          )
                        ),
                    )

                         
                    // ElevatedButton(
                    //   child: Text("Login"),
                    //   style: TextButton.styleFrom(
                    //     minimumSize: Size(140, 40),
                    //   ),
                    //   onPressed: () 
                    //     Navigator.pushNamed(context, MyRoutes.homeRoute);
                    //   ,
                    // ),
                  ],
                ),
              )
            ],
          ),
        ));
  

错误:

未定义名称“更改按钮”。尝试将名称更正为已定义的名称,或定义名称

请解决这个问题

【问题讨论】:

请分享代码,而不是图片。 【参考方案1】:

在类声明之前或 _LoginPageState 内部定义一个新的变量 changebutton

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dev/utils/routes.dart';


 class LoginPage extends StatefulWidget 
  // bool changeButton = false; //remove this outside the class or inside _LoginPageState 

  @override
  State<LoginPage> createState() => _LoginPageState();



 class _LoginPageState extends State<LoginPage> 
  get name => null;
 bool changeButton = false; 


  @override
  Widget build(BuildContext context) 
    return Material(
        color: Colors.white,
        child: SingleChildScrollView(
          child: Column(
            children: [
              Image.asset(
                "assets/images/login_image.png",
                fit: BoxFit.cover,
              ),
              SizedBox(
                height: 20.0,
              ),
              Text(
                "Welcome",
                style: TextStyle(
                  fontSize: 28,
                  fontWeight: FontWeight.bold,
                ),
              ),

              SizedBox(
                height: 20.0,
              ),
              Padding(
                padding: const EdgeInsets.symmetric(
                    vertical: 16.0, horizontal: 32.0),
                child: Column(
                  children: [
                    TextFormField(
                      decoration: InputDecoration(
                        hintText: "Enter username",
                        labelText: "Username",
                      ),
                      onChanged: (value) 
                        satState() 
                      ,
                    ),
                    TextFormField(
                      obscureText: true,
                      decoration: InputDecoration(
                        hintText: "Enter Password",
                        labelText: "Password",
                      ),
                    ),
                    SizedBox(
                      height: 40.0,
                    ),
                    
                    InkWell(
                     onTap: ()
                       setState(() 
                         changeButton = true;
                       );
                      //  Navigator.pushNamed(context, MyRoutes.homeRoute);
                     ,
                      child: AnimatedContainer(
                        duration: Duration(seconds: 1),
                        width: 150,
                        height: 40,
                        
                        alignment: Alignment.center,
                        child: Text(
                          "login",
                          style: TextStyle(
                            color: Colors.white, 
                            fontWeight:FontWeight.bold,
                             fontSize: 18
                             ),  
                          ),
                          decoration: BoxDecoration(
                          color: Colors.deepPurple,
                          borderRadius: BorderRadius.circular(8)
                          )
                        ),
                    )

                         
                    // ElevatedButton(
                    //   child: Text("Login"),
                    //   style: TextButton.styleFrom(
                    //     minimumSize: Size(140, 40),
                    //   ),
                    //   onPressed: () 
                    //     Navigator.pushNamed(context, MyRoutes.homeRoute);
                    //   ,
                    // ),
                  ],
                ),
              )
            ],
          ),
        ));
  

【讨论】:

【参考方案2】:

changeButton_LoginPageState下声明

class _LoginPageState extends State<LoginPage> 
  get name => null;
 bool changeButton = false;

另一种方法:

widget.changeButton = true;

【讨论】:

不..不行.. 错误是什么?【参考方案3】:
class LoginPage extends StatefulWidget 

  @override
  State<LoginPage> createState() => _LoginPageState();



 class _LoginPageState extends State<LoginPage> 
  get name => null;
  bool changeButton = false;
....

使用widget like widget.changeButton = true

【讨论】:

尝试将名称更正为现有设置器的名称,或定义一个名为“changeButton”的设置器或字段。更改按钮 = 真; ^^^^^^^^^^^^^ changeButton 不能用这个代码示例抛出这样的错误 您是否尝试将变量的值传递给构造函数?

以上是关于未定义的名称“更改按钮”。尝试将名称更正为已定义的名称,或定义名称的主要内容,如果未能解决你的问题,请参考以下文章

颤振'LatLng'未定义

Flutter TextSelection.collapsed:未定义命名参数“offset”

未定义全局名称“lats”

名称“slugify”未定义

Python / Access NameError:名称''未定义

TypeError:无法读取未定义的属性(读取“名称”)