Home > Back-end >  Checkbox with flutter bloc
Checkbox with flutter bloc

Time:05-30

how to do the checkbox with flutter bloc?

can anyone help me?

thank you in advance

enter link of the package here

CodePudding user response:

Please refer this link : tap here

This is cubit class

class CheckboxCubit extends Cubit<CheckboxState> {
  CheckboxCubit() : super(CheckboxState(ischecked: false));

  void changeValue(bool value) {
    emit(state.copyWith(changeState: value));
  }
}

this is state class

class CheckboxState {
  bool ischecked = false;

  CheckboxState({required this.ischecked}) {
    if (ischecked) {
      ischecked = true;
    } else {
      ischecked = false;
    }
  }

  CheckboxState copyWith({required bool changeState}) {
    return CheckboxState(ischecked: changeState);
  }
}

this is the home screen

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        BlocBuilder<CheckboxCubit, CheckboxState>(
          builder: (context, state) {
            return Checkbox(
              value: state.ischecked,
              onChanged: (value) {
                context.read<CheckboxCubit>().changeValue(value!);
              },
            );
          },
        ),
      ],
    ));
  }
}
  • Related