关闭键盘时,文本表单字段数据消失

问题描述 投票:0回答:1

我正在构建一个扑动的应用程序,我在那里有一个Register形式。我正在使用TextFormFields,下面是我的代码。

import 'package:flutter/material.dart';

class RegisterPage extends StatelessWidget {
  final borderColorGreen = const Color(0xff339966);

  TextEditingController nameTxtController =TextEditingController();
  TextEditingController emailTxtController =TextEditingController();
  TextEditingController mobileTxtController =TextEditingController();
  TextEditingController passwordTxtController =TextEditingController();
  TextEditingController confirmPasswordTxtController =TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage("assets/images/background_login_5.jpg"),
            fit: BoxFit.cover,
          ),
        ),
        child: ListView(
          children: <Widget>[
            _buildTitleSection(),
            _buildInputFields("Name",nameTxtController),
            _buildInputFields("Email",emailTxtController),
            _buildInputFields("Mobile",mobileTxtController),
            _buildInputFields("Password",passwordTxtController),
            _buildInputFields("Confirm Password",confirmPasswordTxtController),
            _buildRegisterButton(),
          ],
        ),
      ),
    );
  }

  Widget _buildTitleSection() {
    return Container(
        margin: EdgeInsets.only(top: 40),
        padding: EdgeInsets.all(32),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              "something.xyz",
              style: TextStyle(
                color: borderColorGreen,
                fontWeight: FontWeight.w900,
                fontSize: 25,
              ),
            ),
          ],
        ));
  }

  Widget _buildInputFields(String label, TextEditingController textController) {
    return Container(
        margin: EdgeInsets.only(left: 20, bottom: 20),
        child: Column(
          children: <Widget>[

            Container(
              padding: EdgeInsets.only(right: 20),
              child: Row(
                children: <Widget>[
                  Flexible(
                    child: TextFormField(
                      controller: textController,
                      style: new TextStyle(color: Colors.white),
                      decoration: InputDecoration(
                        labelText: label,
                        fillColor: Colors.white,
                        labelStyle: TextStyle(
                            color: Colors.white, fontWeight: FontWeight.w600),
                        enabledBorder: OutlineInputBorder(
                          borderSide: const BorderSide(
                              color: Colors.white30, width: 2.0),
                          borderRadius: BorderRadius.circular(25.0),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ));
  }





  Widget _buildRegisterButton() {
    return Container(
      margin: EdgeInsets.only(top: 10, right: 20, left: 20),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Flexible(
              child: SizedBox(
                  width: double.infinity, // match_parent
                  child: RaisedButton(
                    child: Text(
                      "Create Account",
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: 18,
                          fontWeight: FontWeight.normal),
                    ),
                    shape: RoundedRectangleBorder(
                        borderRadius: new BorderRadius.circular(30.0)),
                    color: borderColorGreen,
                    onPressed: () {},
                  )))
        ],
      ),
    );
  }


}

当我点击TextField时键盘打开,我可以输入文字。但是,如果我按下手机的后退按钮关闭键盘,那么我从所有字段输入的文本。我指的是Android手机中提供的硬件后退按钮。

这个表格是scrllable,如果我点击另一个TextField,在输入文本到TextField之后,文本就会保留在那里。如果我按后退按钮关闭键盘,表单中的所有文本都将消失。

我该如何解决这个问题?

android dart flutter flutter-layout
1个回答
2
投票

请将您的小部件更改为StatefulWidget,这不会导致控制器重置。

希望有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.