当我键入文本并单击“确定”或单击“后退”按钮以隐藏键盘时,输入值消失了

添加一个TextEditingController


class ChatCepBottomSheet extends StatelessWidget {

  final TextEditingController _cepController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        TextFormField(
          controller: _cepController,
        ),
        SizedBox(height: 32),
        Button(
          buttonText: 'Search',
          tapHandler: () {},
        )
      ],
    );
  }
}


我希望键入的文本在文本 Controller 中稳定

最佳答案

“按钮”的声明是错误的。检查Docs以获取更多信息。另外,您无法将状态保留在StatelessWidget中。

这将有助于:

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {

  final TextEditingController _cepController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: Text('Demo')),
      body: Column(
        children: <Widget>[
          TextFormField(
            controller: _cepController,
          ),
          SizedBox(height: 32),
          FlatButton(
            child: Text('Search'),
            onPressed: () {},
          )
        ],
      )
    );
  }
}

10-05 23:58