颤抖如何重新加载整个页面

时间:2020-09-06 17:52:41

标签: flutter

在我的应用程序中,用户输入数据,然后将其上传到Firestore。之后,我希望所有字段为空,并且基本上,整个页面都将重新加载。单击动作或按钮后如何重新加载整个页面?我已经添加了相应的有状态类。

主类

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: TestForm(),
      ),
    );
  }
}

class TestForm extends StatefulWidget {
  @override
  _TestFormState createState() => _TestFormState();
}

如何通过单击按钮来重新加载整个表单,而无需进入新页面并可以再次返回。

class _TestFormState extends State<TestForm> {
  final _formKey = GlobalKey<FormState>();
  File _imageFile;
  String locWorking;
  Model model = Model();

  Future<void> _getLocation() async {
    Position position = await Geolocator()
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    print(position.toString());
    setState(() {
      model.location = position.toString();
    });
  }

  Future<void> _pickImage(ImageSource source) async {
    File selected = await ImagePicker.pickImage(source: source);

    setState(() {
      _imageFile = selected;
      String fileName = 'images/${DateTime.now()}.png';
      model.picName = fileName;
      model.picCheck = true;
    });
  }

  @override
  Widget build(BuildContext context) {
    final halfMediaWidth = MediaQuery.of(context).size.width / 2.0;
    model.checkBox = false;
    return Scaffold(
      appBar: AppBar(
        title: Text("Form Demo"),
      ),
      body: Form(
        key: _formKey,
        child: Column(
          children: <Widget>[
            Container(
              alignment: Alignment.topCenter,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                    alignment: Alignment.topCenter,
                    width: halfMediaWidth,
                    child: MyTextFormField(
                      hintText: 'Name',
                      validator: (String value) {
                        if (value.isEmpty) {
                          return 'Enter your first name';
                        }
                        return null;
                      },
                      onSaved: (String value) {
                        model.firstName = value;
                      },
                    ),
                  ),
                  Container(
                      alignment: Alignment.topCenter,
                      width: halfMediaWidth,
                      child: RaisedButton(
                          color: Colors.blueAccent,
                          onPressed: () {
                            _getLocation();
                          },
                          child: Text("Get Location")))
                ],
              ),
            ),
            MyTextFormField(
              hintText: 'Description',
              validator: (String value) {
                if (value.isEmpty) {
                  return 'Please Enter Description';
                }
                return null;
              },
              onSaved: (String value) {
                model.email = value;
              },
            ),
            StatefulBuilder(
              builder: (context, setState) => CheckboxListTile(
                title: Text("Need urgent repair"),
                value: model.checkBox,
                onChanged: (bool newValue) {
                  setState(() {
                    model.checkBox = newValue;
                  });
                },
                controlAffinity:
                    ListTileControlAffinity.leading, //  <-- leading Checkbox
              ),
            ),
            RaisedButton(
              color: Colors.blueAccent,
              onPressed: () {
                if (_formKey.currentState.validate()) {
                  print(locWorking);
                  _formKey.currentState.save();
                  if (model.location == null) {
                    print("No location");

                    setState(() {
                      model.dataCheck = false;
                    });
                    print(model.dataCheck);
                    showDialog(
                        context: context,
                        builder: (context) => AlertDialog(
                              title: new Text("Error"),
                              content:
                                  new Text("No location has been picked up"),
                              actions: <Widget>[
                                new FlatButton(
                                    onPressed: () {
                                      Navigator.of(context).pop();
                                    },
                                    child: new Text("Close"))
                              ],
                            ));
                  } else {
                    print("All checks passed...");
                    setState(() {
                      model.dataCheck = true;
                    });

                    print(model.dataCheck);
                  }
                  if (model.picCheck == false || model.picCheck == null) {
                    showDialog(
                        context: context,
                        builder: (context) => AlertDialog(
                              title: new Text("Notice"),
                              content: new Text("No picture has been added..."),
                              actions: <Widget>[
                                new FlatButton(
                                    onPressed: () {
                                      setState(() {
                                        model.picCheck = true;
                                        model.picName = "Null";
                                      });
                                      Navigator.of(context).pop();
                                    },
                                    child: new Text("No Picture Needed")),
                                new FlatButton(
                                    onPressed: () {
                                      Navigator.of(context).pop();
                                    },
                                    child: new Text("Close"))
                              ],
                            ));
                  }
                }
              },
              child: Text(
                'Check Data',
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
            ),
            if (_imageFile != null) ...[
              Uploader(
                file: _imageFile,
                fileName: model.picName,
              ),
            ],
            if (model.dataCheck == true && model.picCheck == true) ...[
              DataAdder(model: this.model),
            ],
          ],
        ),
      ),
      bottomNavigationBar: BottomAppBar(
        child: Row(
          children: <Widget>[
            IconButton(
              icon: Icon(Icons.photo_camera),
              onPressed: () => _pickImage(ImageSource.camera),
            ),
            IconButton(
              icon: Icon(Icons.photo_library),
              onPressed: () => _pickImage(ImageSource.gallery),
            ),
          ],
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

更新如何重置应用状态:

该问题想重置应用程序的状态,而不是文本字段。您可以使用the Flutter Phoenix软件包来实现此目的。

如何在按下按钮后重设TextField输入:

setState()会在每次调用时在您的类中重建build()方法,但是我假设您的意思是希望用户单击按钮后文本字段为空,您可以通过使用TextEditingController

声明:

TextEditingController controller = TextEditingController();

用例:

 Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            TextField(
              controller: controller,
              decoration: InputDecoration(
                hintText: "Enter your first name",
              ),
            ),
            RaisedButton(onPressed: () {
              controller.clear();
              child:
              Text(
                "Clear",
              );
            })
          ],
        ),

这应该在每次按下按钮时清除TextField。