【问题标题】:Flutter TextFormField onSaved no called when not visible on screenFlutter TextFormField onSaved 在屏幕上不可见时不调用
【发布时间】:2018-11-24 10:40:32
【问题描述】:

我在 ListView 中有一个 TextFormField,我发现了一个问题,即当 TextFormField 不在屏幕上时,onsaved 回调从未调用过。我计划使用 TextEditingController 手动保存该值,但这是一个好方法吗?

这里有一些sn-p代码:

class _MyHomePageState extends State<MyHomePage> {
  final _formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Title"),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.save),
            onPressed: () {
              _formKey.currentState.save();
            },
          )
        ],
      ),
      body: Form(
        key: _formKey,
        child: ListView(
          children: List<int>.generate(30, (index) => index).map((v) {
            return TextFormField(
              decoration: InputDecoration(labelText: "Input $v"),
              onSaved: (v1) => print("On save $v called"),
            );
          }).toList(),
        ),
      ),
    );
  }
}

“On save $v called”并非针对所有字段打印。

【问题讨论】:

  • 你有代码要分享吗?

标签: flutter


【解决方案1】:

我找到了解决方案,所以我没有使用 ListView,而是使用 SingleChildScrollView。

【讨论】:

    猜你喜欢
    • 2019-09-12
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多