0

I am pretty new to flutter and want to know how can I clear all the input fields on a screen. I am not using a form field so I can't use reset function. I got a reset button where I want when I click it reinitialise all input fields and refresh the screen.

These are my variables -

  bool _check2 = false;
  bool _check3 = false;
  bool _check4 = false;

  int taxYear = DateTime.now().year;
  TextEditingController taxDueController = TextEditingController();
  DateTime? payoffDate;
  DateTime? filingTaxDate;
  bool? filingEx1;
  bool? filingEx2;
  DateTime? noticeDate;
  TextEditingController adjustmentTaxController = TextEditingController();
  DateTime? installmentStartDate;
  DateTime? installmentEndDate;
  DateTime? levyDate;
  int? noOfPayment;
  DateTime? payment1Date;
  TextEditingController payAmount1Controller = TextEditingController();
  DateTime? payment2Date;
  TextEditingController payAmount2Controller = TextEditingController();
  DateTime? payment3Date;
  TextEditingController payAmount3Controller = TextEditingController();
  DateTime? payment4Date;
  TextEditingController payAmount4Controller = TextEditingController();
  bool detailedComputation = false;
  DateTime payoffDateV0 = DateTime.now();
  DateTime dateInNotice = DateTime.now();
  final int _startYear = 2010;```
Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

0

You need to use a Stateful Widget and setState to update variables that are used in your UI.

This document will probably be helpful: https://docs.flutter.dev/development/ui/interactive

You can use setState in a stateful widget to update your field values to be null/empty. This will tell Flutter to re-build your form fields.

Something like this:

void resetButtonClicked() {
  setState(() {
     myVar = null;
     // clear other variables
  });
}

However, for the controllers, there is a clear method that you can use. See this answer: How to add clear button to TextField Widget

JakeD
  • 407
  • 2
  • 7
  • 19