Flutter 使用状态(ScopedModels)在单个屏幕上显示登录或注册表单

Flutter 使用状态(ScopedModels)在单个屏幕上显示登录或注册表单,flutter,Flutter,我想为注册和登录使用一个身份验证屏幕。我使用state根据用户的输入显示相应的表单。我有一个名为SignView的父窗口小部件类和一个名为auth_form_widgets.dart的子窗口小部件auth_form_widgets.dart处理表单布局和验证,而SignView根据来自模型(SignModel)的值构建UI 我有两个问题: 这是一个好方法吗?我想我没有看到任何教程建议这样做。我这样做只是为了更好地掌握颤振中的状态管理 我现在注意到的是,当我从注册切换到登录(或返回)时,输入到一

我想为注册和登录使用一个身份验证屏幕。我使用state根据用户的输入显示相应的表单。我有一个名为SignView的父窗口小部件类和一个名为auth_form_widgets.dart的子窗口小部件auth_form_widgets.dart处理表单布局和验证,而SignView根据来自模型(SignModel)的值构建UI

我有两个问题:

  • 这是一个好方法吗?我想我没有看到任何教程建议这样做。我这样做只是为了更好地掌握颤振中的状态管理

  • 我现在注意到的是,当我从注册切换到登录(或返回)时,输入到一个表单字段中的值在另一个表单上仍然可见

  • 登录视图代码:

    return ScopedModel<SignModel>(
          model: locator<SignModel>(),
          child: ScopedModelDescendant<SignModel>(
              builder: (context, child, model) => Scaffold(
                    appBar: AppBar(
                      centerTitle: true,
                      title: Text(
                        model.title,
                        style: Theme.of(context).textTheme.headline1,
                      ),
                    ),
                    body: GestureDetector(
                      onTap: () {
                        FocusScope.of(context).unfocus();
                      },
                      child: Column(
                        children: [
                          Expanded(
                              flex: 7,
                              child: Container(
                                  height: screenHeightPercentage(context,
                                      percentage: 0.8),
                                  child: model.activeForm)),
    

    Widget signIn() {
      return ScopedModelDescendant<SignModel>(
        builder: (context, child, model) => Container(
          padding: EdgeInsets.only(top: 25, bottom: 10, left: 30, right: 30),
          alignment: Alignment.topCenter,
          decoration: BoxDecoration(
            color: Colors.white,
          ),
          child: Builder(
              builder: (context) => Form(
                    key: _formKey,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: [
                        Opacity(opacity: .5, child: emailField()),
                        SizedBox(
                          height: 25,
                        ),
                        Opacity(opacity: .5, child: passwordField()),
                        SizedBox(
                          height: 25,
                        ),
                        Container(
                          child: ConstrainedBox(
                            constraints:
                                BoxConstraints.tightFor(width: 370, height: 65),
                            child: ElevatedButton(
                                style: ElevatedButton.styleFrom(
                                  primary: Colors.green,
                                  onPrimary: Colors.white,
                                  shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(10.0),
                                  ),
                                ),
                                onPressed: () {},
                                child: Text(
                                  'Sign in',
                                  style: TextStyle(fontSize: 18),
                                )),
                          ),
                        ),
                        SizedBox(
                          height: 25,
                        ),
                        Center(
                          child: Container(
                            child: RichText(
                              text: TextSpan(children: [
                                TextSpan(
                                    text: 'New here? Register.',
                                    style: Theme.of(context).textTheme.bodyText2,
                                    recognizer: TapGestureRecognizer()
                                      ..onTap = () {
                                        model.displayForm();
                                      }),
                                TextSpan(
                                  text: '     ',
                                ),
                                TextSpan(
                                    text: '  Forgot Password?',
                                    style: Theme.of(context).textTheme.bodyText2,
                                    recognizer: TapGestureRecognizer()
                                      ..onTap = () {}),
                              ]),
                            ),
                          ),
                        )
                      ],
                    ),
                  )),
        ),
      );
    }
    
      Widget displayForm() {
        if (_setUserStatus() == true) {
          notifyListeners();
          activeForm = signUp();
          title = 'Sign up';
          
        } else {
          notifyListeners();
          activeForm = signIn();
          title = 'Sign in';
       
        }
        return activeForm;
      }
    }