0

Cubit State class

class AuthState extends Equatable {

  const AuthState({
    this.makePwdVisible = true,
  });

  final bool makePwdVisible;
 
  @override
  List<Object> get props => [makePwdVisible];

  AuthState copyWith({
    bool? makePwdVisible,
  }) {
    return AuthState(
      makePwdVisible: makePwdVisible ?? this.makePwdVisible,
    );
  }
}

Cubit class to emit state

class AuthenticationCubit extends Cubit<AuthState> {
  void togglePwdVisibility() {
    emit(
        state.copyWith(
            makePwdVisible: !state.makePwdVisible
        )
    );
  }
}

Class Contain TextFormField to accept password

class PasswordInput extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AuthenticationCubit, AuthState>(
      buildWhen: (previous, current) => previous.password != current.password,
      builder: (context, state) {
        return Padding(
          child: TextFormField(
            obscureText: state.makePwdVisible,
                  onPressed: () => context.read<AuthenticationCubit>().togglePwdVisibility()
              ) : null,
            ),
          ),
        );
      },
    );
  }
}

I am trying to update password visibility of TextFormField but when I click the eye button of TextFormField nothing happens. Cubit is not updating the state because state.makePwdVisible not get called on eye button toggle. While when I enter password and if password is wrong state.password.error is getting called. I don't know why Cubit is working for one variable password.error but not for makePwdVisible

  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Nov 12 '22 at 05:53

0 Answers0