0

I am trying to create a custom textfield so that i can easily change the design in one place only. The problem is, i don't know how to add onchange property in this custom textfield whenever it is provided. Can someone please show me how to do this. Fyi, this onchange property is optional. Below is my code.

Custom Textfield:

  class CustomTextFieldOutline extends StatelessWidget {
  CustomTextFieldOutline(
      {@required this.label,
      @required this.controller,
      @required this.isObscureText,
      @required this.isValid,
      @required this.invalidMsg});

  final String label;
  final TextEditingController controller;
  final bool isObscureText;
  final bool isValid;
  final String invalidMsg;

  @override
  Widget build(BuildContext context) {
    return TextField(
      decoration: InputDecoration(
          labelText: label,
          errorText: isValid ? null : invalidMsg,
          errorStyle: TextStyle(color: constant.colorWhite),
          labelStyle: TextStyle(color: constant.colorWhite),
          errorBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: constant.colorWhite),
          ),
          focusedErrorBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: constant.colorWhite),
          ),
          enabledBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: constant.colorWhite),
          ),
          focusedBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: constant.colorWhite),
          )),
      style: TextStyle(color: constant.colorWhite),
      controller: controller,
      obscureText: isObscureText,
    );
  }
}

Textfield:

          CustomTextFieldOutline(
              label: constant.email,
              controller: _emailSignUpController,
              isValid: _isEmailValid,
              invalidMsg: _emailValidMsg,
              isObscureText: false,
            )

1 Answer 1

2

You can pass in a function as a parameter into your custom textfield. Declare a property called Function onPressed, so you're constructor will look like this:

 CustomTextFieldOutline(
      {@required this.label,
      @required this.controller,
      @required this.isObscureText,
      @required this.isValid,
      @required this.invalidMsg,
      @required this.onPressed
});

By doing this you can obtain custom onChanged functionality by defining a custom function for each custom textfield. Hope that helped!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.