Home > database >  How to send button title to cubit?
How to send button title to cubit?

Time:06-15

Need your help. I have 3 buttons with titles. And there is a cubit through which I want to handle events. Now when the button is clicked, true/false is passed to me according to the pressed button in cubit. But I need the text of the button I clicked to be passed. For example, I have selected the "Comment" button, and the text must also be translated into a cubit so that I can send this text to the server in the future. Tell me how to send your button title to cubit when the button is clicked?

widget

SizedBox(
              width: size.width / 3,
              child: GestureDetector(
                onTap: () {
                  cubit.reportButton(0);
                },
                child: ReportButton(
                  enabled: state.reports[0],
                  svg: constants.Assets.comment,
                  text: 'Comment',
                ),
              ),
            ),
            SizedBox(
              width: size.width / 3,
              child: GestureDetector(
                onTap: () {
                  cubit.reportButton(1);
                },
                child: ReportButton( 
                  enabled: state.reports[1],
                  svg: constants.Assets.attention,
                  text: 'Broken station',
                ),
              ),
            ),
            SizedBox(
              width: size.width / 3,
              child: GestureDetector(
                onTap: () {
                  cubit.reportButton(2);
                },
                child: ReportButton(
                  enabled: state.reports[2],
                  svg: constants.Assets.blockedStation,
                  text: 'Blocked station',
                ),
              ),
            ),

cubit

class ReportCubit extends Cubit<ReportState> {
  ReportCubit()
      : super(
          ReportInitial(
            images: [],
            isEnabled: false,
            reports: List.filled(3, false),
            isPhotoEnabled: false,
          ),
        );

  final List<bool> reportsList = [false, false, false];
  bool isEnabledC = false;

  void reportButton(int number) {
    reportsList[number] = !reportsList[number];
    if (reportsList.contains(true)) {
      isEnabledC = true;
    } else {
      isEnabledC = false;
    }
    emit(
      ReportInitial(
        isEnabled: isEnabledC,
        reports: reportsList,
      ),
    );
  }

state*

class ReportInitial extends ReportState {
  final List<bool> reports;
  final bool isEnabled;
  final List<XFile?> images;
  final String? comment;
  final bool isPhotoEnabled;

  ReportInitial({
    this.comment,
    required this.images,
    required this.isEnabled,
    required this.reports,
    required this.isPhotoEnabled,
  });
}

CodePudding user response:

update reportButton to:

  void reportButton(int number, {String? comment}) {
    reportsList[number] = !reportsList[number];
    if (reportsList.contains(true)) {
      isEnabledC = true;
    } else {
      isEnabledC = false;
    }
    emit(
      ReportInitial(
        isEnabled: isEnabledC,
        reports: reportsList,
        comment: comment,
      ),
    );
  }

and in the button widget:

SizedBox(
  width: size.width / 3,
  child: GestureDetector(
  onTap: () {
    cubit.reportButton(
      0,
      comment: 'my amazing comment',
    );
  },
  child: ReportButton(
    enabled: state.reports[0],
    svg: constants.Assets.comment,
    text: 'Comment',
  ),
),
  • Related