You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter开发LoginPage组件报错Undefined name 'changeButton'如何修复

问题原因
  • changeButton变量定义在StatefulWidget的子类LoginPage中,State类访问父Widget的属性必须通过widget.前缀调用,你直接写changeButton = true就会触发未定义错误
  • 额外存在一处笔误:用户名输入框的onChanged回调中拼写错误,satState()正确写法为setState()
修复方案

推荐使用第二种方案,因为控制UI状态的变量放在State类内部更符合Flutter的设计规范,不需要对外暴露。

方案1:保留变量在LoginPage类中,补全调用前缀

将InkWell的onTap回调中的changeButton = true修改为widget.changeButton = true即可,同时修正satState的拼写错误。

方案2:将状态变量移到State类内部(推荐)

  1. 删除LoginPage类中bool changeButton = false;这行代码
  2. 在_LoginPageState类中新增变量bool changeButton = false;

修复后完整代码

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

class LoginPage extends StatefulWidget {
  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  bool changeButton = false;
  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,
              ),
              const SizedBox(
                height: 20.0,
              ),
              const Text(
                "Welcome",
                style: TextStyle(
                  fontSize: 28,
                  fontWeight: FontWeight.bold,
                ),
              ),
              const SizedBox(
                height: 20.0,
              ),
              Padding(
                padding: const EdgeInsets.symmetric(
                    vertical: 16.0, horizontal: 32.0),
                child: Column(
                  children: [
                    TextFormField(
                      decoration: const InputDecoration(
                        hintText: "Enter username",
                        labelText: "Username",
                      ),
                      onChanged: (value) {
                        setState(() {});
                      },
                    ),
                    TextFormField(
                      obscureText: true,
                      decoration: const InputDecoration(
                        hintText: "Enter Password",
                        labelText: "Password",
                      ),
                    ),
                    const SizedBox(
                      height: 40.0,
                    ),
                    InkWell(
                      onTap: (){
                        setState(() {
                          changeButton = true;
                        });
                        //  Navigator.pushNamed(context, MyRoutes.homeRoute);
                      },
                      child: AnimatedContainer(
                          duration: const Duration(seconds: 1),
                          width: 150,
                          height: 40,
                          alignment: Alignment.center,
                          child: const 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);
                    //   },
                    // ),
                  ],
                ),
              )
            ],
          ),
        ));
  }
}

内容的提问来源于stack exchange,提问作者Durjoy Roy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 15:57:04