【问题标题】: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);
                    //   },
                    // ),
                  ],
                ),
              )
            ],
          ),
        ));
  }
}

错误:

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

请解决这个问题

【问题讨论】:

  • 请分享代码,而不是图片。

标签: android flutter visual-studio-code


【解决方案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 不能用这个代码示例抛出这样的错误
    • 您是否尝试将变量的值传递给构造函数?
    猜你喜欢
    • 2020-09-03
    • 2023-02-07
    • 2022-08-22
    • 2022-06-10
    • 1970-01-01
    • 2020-01-28
    • 2020-11-01
    • 2021-03-24
    • 1970-01-01
    相关资源
    最近更新 更多