Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how clear form filed after submitting data in flutter?

Tags:

flutter

dart

I am submitting data using post method to my backend php server. The data is successfully submitted on backend. The problem is that when click submitted the data in, the form must be cleared so user is unable click again on submitted button. I don't how to disable clear form field after click on submit button.

import 'dart:io';

import 'package:dio/dio.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/ui/Home.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:path/path.dart';

import 'InputDeco_design.dart';

class Prayerequest extends StatefulWidget {
  @override
  _PrayerequestState createState() => _PrayerequestState();
}

class _PrayerequestState extends State<Prayerequest> {
  TextEditingController _name = TextEditingController();
  TextEditingController _email = TextEditingController();
  TextEditingController _phone = TextEditingController();
  TextEditingController _country= TextEditingController();
  TextEditingController _description = TextEditingController();
  //TextController to read text entered in text field
  final GlobalKey<FormState> _formkey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> _scaffoldstate = new GlobalKey<ScaffoldState>();



  void _prayerrequest() async {

    try {
      FormData formData = new FormData.fromMap({
        "name": _name.text,
        "email": _email.text,
        "phone":_phone.text,
        "country":_country.text,
        "description":_description.text,
        "file":''
      });
      Response response = await Dio().post("https://livinghopemobile.com/public/api/save-request?token=123ab_@_@AbCD",data: formData);
      print("File upload response: $response");
      _showSnackBarMsg(response.data['message']);
    }
    catch (e) {
      print("expectation Caugch: $e");
    }



  }
  void _showSnackBarMsg(String msg){
    Fluttertoast.showToast(
        msg: "Thanks you!Your Prayer Request has been submitted",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.green,
        textColor: Colors.white,
        fontSize: 16.0
    );

  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldstate,
      appBar:AppBar(
          iconTheme: IconThemeData(color: Colors.amber[800]),
          backgroundColor: Colors.white,
          title: Text('Send your Prayer Request',style: TextStyle(
            fontSize: 14,
              color: Colors.amber[800]
          ),
          )

      ),
      body: Center(
        child: SingleChildScrollView(
          child: Form(
            key: _formkey,
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.all(10),
                  child: TextFormField(
                    controller: _name,
                    keyboardType: TextInputType.text,
                    decoration: buildInputDecoration(Icons.person,"Full Name"),
                    validator: (String value){
                      if(value.isEmpty)
                      {
                        return 'Please Enter Name';
                      }
                      return null;
                    },
                    onSaved: (String value){

                    },
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(10),
                  child: TextFormField(
                    controller: _email,
                    keyboardType: TextInputType.text,
                    decoration:buildInputDecoration(Icons.email,"Email"),
                    validator: (String value){
                      if(value.isEmpty)
                      {
                        return 'Please a Enter';
                      }
                      if(!RegExp("^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+.[a-z]").hasMatch(value)){
                        return 'Please a valid Email';
                      }
                      return null;
                    },
                    onSaved: (String value){

                    },
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(10),
                  child: TextFormField(
                    controller: _phone,
                    keyboardType: TextInputType.number,
                    decoration:buildInputDecoration(Icons.phone,"Phone No"),
                    validator: (String value){
                      if(value.isEmpty)
                      {
                        return 'Please enter phone no ';
                      }
                      return null;
                    },
                    onSaved: (String value){

                    },
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(10),
                  child: TextFormField(
                    controller: _country,
                    keyboardType: TextInputType.text,
                    decoration: buildInputDecoration(Icons.flag,"Country Name"),
                    validator: (String value){
                      if(value.isEmpty)
                      {
                        return 'Please Enter Country';
                      }
                      return null;
                    },
                    onSaved: (String value){

                    },
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(10),
                  child: TextFormField(
                    controller: _description,
                    maxLines: 200,
                    minLines: 3,
                    maxLength: 150,
                    keyboardType: TextInputType.text,
                    decoration: buildInputDecoration(Icons.comment,"Enter description"),
                    validator: (String value){
                      if(value.isEmpty)
                      {
                        return 'Please Enter Description';
                      }
                      return null;
                    },
                    onSaved: (String value){

                    },
                  ),
                ),

            // ignore: deprecated_member_use

                // ignore: deprecated_member_use

                SizedBox(height: 10,),
                SizedBox(
                  width: 200,
                  child: RaisedButton(
                    color: Colors.amber[800],
                    onPressed: (){
                      if(_formkey.currentState.validate())
                      {
                        _prayerrequest();



                      }

                    },

                    textColor:Colors.white,child: Text("Submit"),

                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
like image 545
mark anthony Avatar asked Oct 21 '25 03:10

mark anthony


1 Answers

you can reset it using form key

_formkey.currentState.reset()
like image 192
Ardeshir ojan Avatar answered Oct 24 '25 13:10

Ardeshir ojan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!