如何从另一个屏幕重置Flutter中的表单?

问题描述 投票:3回答:1

如何重置(清除)flutter app中的表单字段?一旦数据提交到服务器?

在我的应用程序中,我有2页。

第1页输入表格详细信息,如姓名,地址等

第2页是上传图片。

第1页将数据推送到第2页。 (这里我没有做推更换,意味着第2页位于首页顶部)

第二页是选择图像,并将表单数据和图像上传到服务器。

我的问题是

上传完成后,如何重置所有2个页面?

我正在使用firebase服务器上传我的数据!

提前致谢

forms dart flutter flutter-layout
1个回答
4
投票

example

例如,使用GlobalKey<FormState>访问表单状态和reset()字段。考虑到这一点,您可以:

  1. 在第二个屏幕的构造函数中传递第一个表单字段键并同时重置(当您的数据处理到服务器时等)

class Screen1 extends StatefulWidget {
  _Screen1State createState() => _Screen1State();
}

class _Screen1State extends State<Screen1> {
  final _formKeyScreen1 = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First screen'),
      ),
      body: Center(
        child: Form(
          key: _formKeyScreen1,
          child: Column(
            children: <Widget>[
              TextFormField(),
              TextFormField(),
              TextFormField(),
              RaisedButton(
                child: Text('Navigate to second screen...'),
                onPressed: () => Navigator.of(context).push(MaterialPageRoute(
                      builder: (BuildContext context) => Screen2(_formKeyScreen1),
                    )),
              )
            ],
          ),
        ),
      ),
    );
  }
}

class Screen2 extends StatefulWidget {
  final GlobalKey<FormState> firstScreenFormKey;
  Screen2(this.firstScreenFormKey);
  _Screen2State createState() => _Screen2State();
}

class _Screen2State extends State<Screen2> {
  final _formKeyScreen2 = GlobalKey<FormState>();

  void _processData() {
    // Process your data and upload to server
    _formKeyScreen2.currentState?.reset();
    widget.firstScreenFormKey?.currentState?.reset();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Second screen'),
      ),
      body: Center(
        child: Form(
          key: _formKeyScreen2,
          child: Column(
            children: <Widget>[
              TextFormField(),
              TextFormField(),
              TextFormField(),
              RaisedButton(
                child: Text('Submit data'),
                onPressed: () => _processData(),
              )
            ],
          ),
        ),
      ),
    );
  }
}
  1. 或者,当第二个屏幕从堆栈弹出时,你可以重置第一个屏幕表格,这个IMO应该足够了,因为我相信你不会在处理后从第二个屏幕表格数据访问,因此,如果只是为了演示目的,重新归还应该就足够了。在这种情况下,您将以下列结尾

class Screen1 extends StatefulWidget {
  _Screen1State createState() => _Screen1State();
}

class _Screen1State extends State<Screen1> {
  final _formKeyScreen1 = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First screen'),
      ),
      body: Center(
        child: Form(
          key: _formKeyScreen1,
          child: Column(
            children: <Widget>[
              TextFormField(),
              TextFormField(),
              TextFormField(),
              RaisedButton(
                child: Text('Navigate to second screen...'),
                onPressed: () {
                  Navigator.of(context)
                      .push(MaterialPageRoute(
                        builder: (BuildContext context) => Screen2(),
                      ))
                      .then((_) => _formKeyScreen1.currentState.reset());
                },
              )
            ],
          ),
        ),
      ),
    );
  }
}

class Screen2 extends StatefulWidget {
  _Screen2State createState() => _Screen2State();
}

class _Screen2State extends State<Screen2> {
  final _formKeyScreen2 = GlobalKey<FormState>();

  void _processData() {
    // Process your data and upload to server
    _formKeyScreen2.currentState?.reset();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Second screen'),
      ),
      body: Center(
        child: Form(
          key: _formKeyScreen2,
          child: Column(
            children: <Widget>[
              TextFormField(),
              TextFormField(),
              TextFormField(),
              RaisedButton(
                child: Text('Submit data'),
                onPressed: () => _processData(),
              )
            ],
          ),
        ),
      ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.