【问题标题】:Flutter - TextFormField geting blank when keyboard is hideFlutter - 键盘隐藏时TextFormField变为空白
【发布时间】:2019-08-12 06:33:42
【问题描述】:

在 Flutter (Android) 中

我在颤振应用中有两个 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(),
  },
));

【问题讨论】:

  • 你什么时候按返回键?
  • 我点击 TextFormField 并显示键盘。在那之后,我写了一些文字。之后,我按下后退按钮关闭键盘,同时 TextFormField 正在关闭键盘。
  • 试过你的代码。一切正常,TextFormFields 不要空白
  • 我想这可能是因为我正在使用路由。我正在发布我的路由代码。
  • 我正在尝试安卓模拟器

标签: android flutter flutter-layout


【解决方案1】:

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

class U extends StatefullWidget
  ....

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

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

这应该可以完成工作:)

【讨论】:

    猜你喜欢
    • 2019-05-04
    • 2020-04-28
    • 1970-01-01
    • 2020-10-15
    • 2021-02-12
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多