skip to Main Content

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


  1. Please clear the text controllers value when you exit the page.

    Login or Signup to reply.
    1. Dispose the controller

    2. Clear the Conroler.

      ElevatedButton(
                       onPressed: (){
                         controller.clear();
      
                       }, 
                       child: Text("Send")
                      )
      
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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 separate StatefulWidget.

    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.

    import "package:flutter/material.dart";
    
    class CustomTextFormField extends StatefulWidget {
      const CustomTextFormField(
          {required this.icon,
          required this.hintText,
          required this.obscureText,
          super.key});
    
      final Icon icon;
      final String hintText;
      final bool obscureText;
    
      @override
      State<CustomTextFormField> createState() => _CustomTextFormFieldState();
    }
    
    class _CustomTextFormFieldState extends State<CustomTextFormField> {
      late TextEditingController controller;
    
      @override
      void initState() {
        controller = TextEditingController();
        super.initState();
      }
    
      @override
      void dispose() {
        controller.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Theme(
          data: Theme.of(context).copyWith(
            colorScheme: ThemeData().colorScheme.copyWith(
                  primary: Colors.red,
                ),
          ),
          child: TextFormField(
            obscureText: widget.obscureText,
            controller: controller,
            decoration: InputDecoration(
              hintText: widget.hintText,
              prefixIcon: widget.icon,
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(50),
              ),
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(50),
                borderSide: const BorderSide(
                  color: Color(0xFFCB3126),
                ),
              ),
            ),
          ),
        );
      }
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search