I made the TextFormField
Widget to be able to use it wherever needed. The codes are as follows;
Widget TextFormFieldWidget(
TextEditingController controller,
Icon icon,
String hintText,
bool obscureText,
BuildContext context,
) {
return Theme(
data: Theme.of(context).copyWith(
colorScheme: ThemeData().colorScheme.copyWith(
primary: Colors.red,
),
),
child: TextFormField(
obscureText: obscureText,
controller: controller,
decoration: InputDecoration(
hintText: hintText,
prefixIcon: icon,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(50),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(50),
borderSide: const BorderSide(
color: Color(0xFFCB3126),
),
),
),
),
);
}
I’m having a problem. Here’s the problem: I’m making a registration page. When the user exits the page and enters back, the values entered in the TextFormField
are not reset. For example, let me give an example that the user gives up after entering their e-mail address. When you leave the page and enter back, the e-mail address is not deleted.
How can I solve the problem? Thanks for help.
4
Answers
Please clear the text controllers value when you exit the page.
Dispose the controller
Clear the Conroler.
In case you use one (a shared) TextController an the one TextController gets passed in to each TextFormFieldWidget, the the single controller will have a single state.
Thus, it keeps its value when getting attached to a new TextFormFieldWidget.
Please use different TextEditingController for different fields.
The issue with creating a function to return a
Widget
is such. If you’re making a custom widget for your app, let’s say reusable custom text field, It is suggested to be made into a separateStatefulWidget
.When you use stateful widget, you make a new instance of the same widget as per needed.
Use the below mentioned code to create the same for yours.