Skip to content

Add 'onFocusChange' Property to TextFormField Widget #137020

@YuneshShrestha

Description

@YuneshShrestha

Is there an existing issue for this?

Use case

  1. Validation: Developers can use 'onFocusChange' to trigger input validation when a user leaves the field, providing immediate feedback on the entered data.
  2. User Experience: It enables custom behaviors, like scrolling the view to keep the focused field visible when the keyboard appears.
  3. Accessibility: For making apps more accessible, it's necessary to handle focus changes to notify screen readers or provide auditory feedback.

Proposal

Change the color of the eye icon in a password field when the user leaves the TextFormField to provide visual feedback on focus changes.


TextFormField(
      obscureText: _isObscure && widget.isPassword,
      decoration: InputDecoration(
        border: const UnderlineInputBorder(
          borderSide: BorderSide(color: grey),
        ),
        focusedBorder: const UnderlineInputBorder(
          borderSide: BorderSide(color: primaryColor),
        ),
        labelText: widget.hintText,
        labelStyle: Theme.of(context).textTheme.labelSmall!.copyWith(
              color: Colors.black,
            ),
        suffixIcon: widget.isPassword
            ? IconButton(
                icon: Icon(
                  _isObscure ? Icons.visibility : Icons.visibility_off,
                  color: _isFocused ? primaryColor : Colors.grey,
                ),
                onPressed: _toggleObscure,
              )
            : null,
      ),
      style: TextStyle(color: Colors.black),
      validator: (value) {
        if (value == null || value.isEmpty) {
          return 'Please enter a value';
        }
        return null;
      },
      onSaved: (value) {
        // Do something with the value
      },
    // _handleFocus handle change of eye color
      onFocusChange: _handleFocus, 
    );
Android.Emulator.-.Pixel_3a_API_34_extension_level_7_x86_64_5554.2023-10-21.20-57-25.mp4

Metadata

Metadata

Assignees

No one assigned

    Labels

    r: solvedIssue is closed as solved

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions