首页 > 解决方案 > showBottomSheet 看不到 currentState

问题描述

我正在尝试添加 showBottomSheet 并在其中尝试调用 currentState 以更改密码,但出现错误:

E/flutter(3094):[错误:flutter/lib/ui/ui_dart_state.cc(186)]未处理的异常:NoSuchMethodError:getter'currentState'被调用为null。E/flutter(3094):接收者:null E/flutter(3094):尝试调用:currentState

class SecurityCard extends StatefulWidget {
  final UserModel currentUser;

  SecurityCard({this.currentUser});
  @override
  _SecurityCardState createState() => _SecurityCardState();
}

class _SecurityCardState extends State<SecurityCard> {
  var _passwordController = TextEditingController();
  var _newPasswordController = TextEditingController();
  var _repeatPasswordController = TextEditingController();

  var userController = locator.get<UserController>();

  bool checkCurrentPasswordValid = true;

  var _formKeyNewPassword;

  @override
  void dispose() {
    _passwordController.dispose();
    _newPasswordController.dispose();
    _repeatPasswordController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          ListTile(
            title: Text('Сменить пароль'),
            leading: Icon(Icons.https),
            onTap: () {
              Scaffold.of(context).showBottomSheet<void>((BuildContext context) {
                return Container(
                  padding: EdgeInsets.symmetric(vertical: getProportionateScreenWidth(5)),
                  horizontal: getProportionateScreenWidth(5)),
                  decoration: BoxDecoration(
                    gradient: LinearGradient(begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [Color.fromRGBO(248, 219, 221, 1.0), Colors.orange[100]]),
                  ),
                  height: 350.0,
                  child: Form(
                    key: _formKeyNewPassword,
                    child: Column(
                      children: [
                        TextFormField(
                          decoration: InputDecoration(hintText: 'Пароль', errorText: checkCurrentPasswordValid ? null : 'Неверный пароль'),
                          controller: _passwordController,
                        ),
                        TextFormField(
                          decoration: InputDecoration(hintText: 'Новый пароль'),
                          // obscureText: true,
                          controller: _newPasswordController,
                        ),
                        TextFormField(
                          decoration: InputDecoration(hintText: 'Повторите новый пароль'),
                          // obscureText: true,
                          controller: _repeatPasswordController,
                          validator: (value) {
                            return _newPasswordController.text == value ? null : 'Пароли не совпадают';
                          },
                        ),
                        ElevatedButton(
                          onPressed: () async {
                            checkCurrentPasswordValid = await userController.validateCurrentPassword(_passwordController.text);
                            if (_formKeyNewPassword.currentState.validate() && checkCurrentPasswordValid) {
                              userController.updateUserPassword(_newPasswordController.text);
                              setState(() {});
                            }
                          },
                          child: Text("Сохранить"),
                        )
                      ],
                    ),
                  ),
                );
              });
            },
          ),
        ],
      ),
    );
  }
}

标签: flutter

解决方案


推荐阅读