关闭键盘时,TextFormField数据消失

时间:2019-03-16 10:32:46

标签: android dart flutter flutter-layout

我正在构建一个Flutter应用程序,并且在那里有一个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手机中提供的硬件后退按钮。

该表单是可编辑的,如果我单击另一个TextField,则在向TextField输入文本之后,该文本将保留在原处。如果我按“后退”按钮关闭键盘,则表单中的所有文本都消失了。

如何解决此问题?

1 个答案:

答案 0 :(得分:2)

请将您的小部件更改为 @Override public String toString() { return "DoublyLinkedListNode{" + "data=" + data + ", next=" + next + ", prev=" + prev + '}'; } ,这不会导致控制器重置。

希望有帮助!