首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Flutter -当键盘隐藏时TextFormField变为空白

Flutter -当键盘隐藏时TextFormField变为空白
EN

Stack Overflow用户
提问于 2019-03-21 19:52:04
回答 1查看 2.7K关注 0票数 8

在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(),
  },
));
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-05 03:11:40

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

class U extends StatefullWidget
  ....

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

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

这应该可以完成以下工作:)

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55279846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档