【问题标题】:Error: Undefined name 'isRememberMe' Error: Required named parameter 'onChanged' must be provided错误:未定义的名称“isRememberMe”错误:必须提供必需的命名参数“onChanged”
【发布时间】:2022-01-11 10:58:23
【问题描述】:

当我尝试定义 isRememberme 小部件时,发生了一些错误。我正在尝试使用颤振和 android studio 构建登录应用程序 UI。我对此很陌生,并且很难找到错误。我已经突出显示了给我错误的代码。

bool isRememberMe =false; //
class LoginScreen extends StatefulWidget {
  @override
  _LoginScreenState createState() => _LoginScreenState();
}

Widget buildEmail() {
  return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Text(
          'Email',
          style: TextStyle(
              color: Colors.white,
              fontSize: 16,
              fontWeight:
              FontWeight.bold
          ),
        ),
        SizedBox(height: 10),
        Container(
            alignment: Alignment.centerLeft,
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(10),
                boxShadow: [
                  BoxShadow(
                      color: Colors.black26,
                      blurRadius: 6,
                      offset: Offset(0, 2)
                  )
                ]
            ),

            height: 60,
            child: TextField(
              keyboardType: TextInputType.emailAddress,
              style: TextStyle(color: Colors.black87),
              decoration: InputDecoration(
                  border: InputBorder.none,
                  contentPadding: EdgeInsets.only(top: 14),
                  prefixIcon: Icon(
                    Icons.email,
                    color: Color(0xff5ac18e),
                  ),
                  hintText: 'Email',
                  hintStyle: TextStyle(color: Colors.black38)),
            )
        )
      ],
  );
}

///////////////////////////////

Widget buildPassword() {
  return Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: <Widget>[
      Text(
        'Password',
        style: TextStyle(
            color: Colors.white,
            fontSize: 16,
            fontWeight:
            FontWeight.bold
        ),
      ),
      SizedBox(height: 10),
      Container(
          alignment: Alignment.centerLeft,
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(10),
              boxShadow: [
                BoxShadow(
                    color: Colors.black26,
                    blurRadius: 6,
                    offset: Offset(0, 2)
                )
              ]
          ),

          height: 60,
          child: TextField(
            obscureText: true,
            style: TextStyle(color: Colors.black87),
            decoration: InputDecoration(
                border: InputBorder.none,
                contentPadding: EdgeInsets.only(top: 14),
                prefixIcon: Icon(
                  Icons.lock,
                  color: Color(0xff5ac18e),
                ),
                hintText: 'Password',
                hintStyle: TextStyle(color: Colors.black38)),
          )
      )
    ],
  );
}

Widget buildForgotPassBtn(){

  return Container(
    alignment: Alignment.centerRight,
    child: TextButton(
      onPressed: () => print("Forget Password pressed"),
     // padding: EdgeInsets.only(right:0),
      child: Text(
        'Forgot Passsword?',
        style: TextStyle(
          color: Colors.white,
          fontWeight : FontWeight.bold
        ),
      ),
    ),
  );
}

**Widget buildRememberCb(){
  return Container(
    height: 20,
    child: Row(
      children: <Widget>[
       Theme(
          data: ThemeData(unselectedWidgetColor :Colors.white),
          child: Checkbox(
            value: isRememberMe,
             ),
             )
            ],
          ),
        )
}
class _LoginScreenState extends State<LoginScreen> {
  bool isRememberMe =false;**

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: AnnotatedRegion<SystemUiOverlayStyle>(
        value: SystemUiOverlayStyle.light,
        child: GestureDetector(
          child: Stack(
            children: <Widget>[
              Container(
                height: double.infinity,
                width: double.infinity,
                decoration: BoxDecoration(
                    gradient: LinearGradient(
                        begin: Alignment.topCenter,
                        end: Alignment.bottomCenter,
                        colors: [
                          Color(0x665ac18e),
                          Color(0x995ac18e),
                          Color(0xcc5ac18e),
                          Color(0xff5ac18e),
                        ]
                    )
                ),

                child: SingleChildScrollView(
                  physics: AlwaysScrollableScrollPhysics(),
                  padding: EdgeInsets.symmetric(
                    horizontal: 25,
                    vertical: 120,

                  ),

                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Text(
                        'Sign In',
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 40,
                            fontWeight: FontWeight.bold),
                      ),
                      SizedBox(height: 50),
                      buildEmail(),
                      SizedBox(height: 20),
                      buildPassword(),
                      buildForgotPassBtn(),
                      buildRememberCb(),
                    ],
                  ),
                ),

              ),
            ],
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter widget


    【解决方案1】:

    将您的 buildRememberCb 方法移动到您的 _LoginScreenState 类中,以便您可以在 CheckBox 小部件的 on change 方法中使用 setState 方法。

    Widget buildRememberCb({bool isRememberMe : false}){
        return Container(
          height: 20,
          child: Row(
            children: <Widget>[
              Theme(
                data: ThemeData(unselectedWidgetColor :Colors.white),
                child: Checkbox(
                  value: isRememberMe,
                  onChanged: (bool? value) {
                    setState((){
                      isRememberMe = value ?? false;
                    });
                  },
                ),
              )
            ],
          ),
        );
    

    【讨论】:

      【解决方案2】:

      您已在小部件范围之外定义了所有功能。由于 buildRememberCb 函数在作用域之外,所以这里不能访问 isRememberMe 变量。

      还需要 Checkbox 小部件的 onChanged 回调。你不能让它未定义。

          class LoginScreen extends StatefulWidget {
        const LoginScreen({Key? key}) : super(key: key);
      
        @override
        _LoginScreenState createState() => _LoginScreenState();
      }
      
      class _LoginScreenState extends State<LoginScreen> {
        bool isRememberMe = false;
      
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: AnnotatedRegion<SystemUiOverlayStyle>(
              value: SystemUiOverlayStyle.light,
              child: GestureDetector(
                child: Stack(
                  children: <Widget>[
                    Container(
                      height: double.infinity,
                      width: double.infinity,
                      decoration: const BoxDecoration(
                          gradient: LinearGradient(
                              begin: Alignment.topCenter,
                              end: Alignment.bottomCenter,
                              colors: [
                            Color(0x665ac18e),
                            Color(0x995ac18e),
                            Color(0xcc5ac18e),
                            Color(0xff5ac18e),
                          ])),
                      child: SingleChildScrollView(
                        physics: const AlwaysScrollableScrollPhysics(),
                        padding: const EdgeInsets.symmetric(
                          horizontal: 25,
                          vertical: 120,
                        ),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            const Text(
                              'Sign In',
                              style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 40,
                                  fontWeight: FontWeight.bold),
                            ),
                            const SizedBox(height: 50),
                            buildEmail(),
                            const SizedBox(height: 20),
                            buildPassword(),
                            buildForgotPassBtn(),
                            buildRememberCb(),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          );
        }
      
        Widget buildEmail() {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              const Text(
                'Email',
                style: TextStyle(
                    color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 10),
              Container(
                  alignment: Alignment.centerLeft,
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(10),
                      boxShadow: const [
                        BoxShadow(
                            color: Colors.black26,
                            blurRadius: 6,
                            offset: Offset(0, 2))
                      ]),
                  height: 60,
                  child: const TextField(
                    keyboardType: TextInputType.emailAddress,
                    style: TextStyle(color: Colors.black87),
                    decoration: InputDecoration(
                        border: InputBorder.none,
                        contentPadding: EdgeInsets.only(top: 14),
                        prefixIcon: Icon(
                          Icons.email,
                          color: Color(0xff5ac18e),
                        ),
                        hintText: 'Email',
                        hintStyle: TextStyle(color: Colors.black38)),
                  ))
            ],
          );
        }
      
        Widget buildPassword() {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              const Text(
                'Password',
                style: TextStyle(
                    color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 10),
              Container(
                  alignment: Alignment.centerLeft,
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(10),
                      boxShadow: const [
                        BoxShadow(
                            color: Colors.black26,
                            blurRadius: 6,
                            offset: Offset(0, 2))
                      ]),
                  height: 60,
                  child: const TextField(
                    obscureText: true,
                    style: TextStyle(color: Colors.black87),
                    decoration: InputDecoration(
                        border: InputBorder.none,
                        contentPadding: EdgeInsets.only(top: 14),
                        prefixIcon: Icon(
                          Icons.lock,
                          color: Color(0xff5ac18e),
                        ),
                        hintText: 'Password',
                        hintStyle: TextStyle(color: Colors.black38)),
                  ))
            ],
          );
        }
      
        Widget buildForgotPassBtn() {
          return Container(
            alignment: Alignment.centerRight,
            child: TextButton(
              onPressed: () => print("Forget Password pressed"),
              // padding: EdgeInsets.only(right:0),
              child: const Text(
                'Forgot Passsword?',
                style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
              ),
            ),
          );
        }
      
        Widget buildRememberCb() {
          return SizedBox(
            height: 20,
            child: Row(
              children: <Widget>[
                Theme(
                  data: ThemeData(unselectedWidgetColor: Colors.white),
                  child: Checkbox(
                    value: isRememberMe,
                    onChanged: (bool? value) {
                      setState(() {
                        isRememberMe = !isRememberMe;
                      });
                    },
                  ),
                )
              ],
            ),
          );
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-01-11
        • 2018-11-02
        • 1970-01-01
        • 2020-01-10
        • 2021-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多