繁体   English   中英

Flutter - 当键盘隐藏时,TextFormField会留空

[英]Flutter - TextFormField geting blank when keyboard is hide

在Flutter(Android)

我在flutter应用程序中有两个TextFormField。 当我按下后退按钮。 这些文字字段空白。

   class FirstScreen extends StatelessWidget {
  TextEditingController charectorsController = new TextEditingController();
  TextEditingController lengthCntroller = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First Screen'),
      ),
      body: Center(
        child: Container(
            margin: EdgeInsets.all(8),
            child: SingleChildScrollView(
              child: Column(
                children: [
                  myEditText(charectorsController, "Enter  charectors Word"),
                  myEditText(lengthCntroller, "Word length"),
                  RaisedButton(
                    child: Text('Launch screen'),
                    onPressed: () {
                      // Navigate to the second screen using a named route
                      // Navigator.pushNamed(context, '/second');
                      print("Rahul");
                      readFile();
                    },
                  )
                ],
              ),
            )),
      ),
    );
  }


}

    myEditText(TextEditingController myController, String s) {
  return new Container(
    margin: EdgeInsets.all(8),
    child: new TextFormField(
      controller: myController,
      decoration: new InputDecoration(
        labelText: s,
        fillColor: Colors.white,
        border: new OutlineInputBorder(
          borderRadius: new BorderRadius.circular(25.0),
          borderSide: new BorderSide(),
        ),
        //fillColor: Colors.green
      ),
      validator: (val) {
        if (val.length == 0) {
          return "Cannot be empty";
        } else {
          return null;
        }
      },
      keyboardType: TextInputType.emailAddress,
      style: new TextStyle(
        fontFamily: "Poppins",
      ),
    ),
  );
}

路由

void main() => runApp(MaterialApp(
  title: 'Named Routes Demo',
  // Start the app with the "/" named route. In our case, the app will start
  // on the FirstScreen Widget
  initialRoute: '/',
  routes: {
    // When we navigate to the "/" route, build the FirstScreen Widget
    '/': (context) => FirstScreen(),
    // When we navigate to the "/second" route, build the SecondScreen Widget
    '/second': (context) => SecondScreen(),
  },
));

StatelessWidget转换为有Stateful ,它将完成这项工作。 问题是通过在StatelessWidget声明TextEditingController ,它将被'重置',因为小部件的构建函数被调用:'(。所以你应该这样做:

class U extends StatefullWidget
  ....

class _UState extends State<U>
  final TextEditingController controller = TextEditingController();

  Widget build(..) => TextField(controller: controller)

这应该做的工作:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM