Home > OS >  How to move onChanged to a separate method?
How to move onChanged to a separate method?

Time:06-09

In my onChanged method, I add an entry to the list and change the state of the checkbox. How to move onChanged into a separate method?

BlocConsumer<StudentBloc, StudentState>(
  listener: _checkboxListener,
  builder: (context, state) {
    return Expanded(
      child: ListView.builder(
        itemCount: _lessonsList.length,
        itemBuilder: (BuildContext context, int index) {
          final lesson = _lessonsList[index];
          bool? checkboxValue = _checkboxValues[index];
          return CheckboxListTile(
            title: Text(lesson.lessonName ?? ''),
            controlAffinity: ListTileControlAffinity.leading,
            contentPadding: EdgeInsets.zero,
            value: checkboxValue,
            onChanged: (bool? value) {
              _checkedLesson.add(lesson);
              setState(
                () {
                  _checkboxValues[index] = value ?? false;
                },
              );
            },
          );
        },
      ),
    );
  },
),

CodePudding user response:

you can do it like this:

BlocConsumer<StudentBloc, StudentState>(
  listener: _checkboxListener,
  builder: (context, state) {
    return Expanded(
      child: ListView.builder(
        itemCount: _lessonsList.length,
        itemBuilder: (BuildContext context, int index) {
          final lesson = _lessonsList[index];
          bool? checkboxValue = _checkboxValues[index];
          return CheckboxListTile(
            title: Text(lesson.lessonName ?? ''),
            controlAffinity: ListTileControlAffinity.leading,
            contentPadding: EdgeInsets.zero,
            value: checkboxValue,
            onChanged: myFunction,
          );
        },
      ),
    );
  },
),

void myFunction(bool? value) {
  _checkedLesson.add(lesson);
  setState(
        () {
      _checkboxValues[index] = value ?? false;
    },
  );
}

the fastest way to do this in Android Studio is to have the cursor on where you have (bool? value) now, and then press Ctrl Alt M. This is a shortcut to extract a method.

If you want to pass other parameters to this function you could do it like this:

BlocConsumer<StudentBloc, StudentState>(
  listener: _checkboxListener,
  builder: (context, state) {
    return Expanded(
      child: ListView.builder(
        itemCount: _lessonsList.length,
        itemBuilder: (BuildContext context, int index) {
          final lesson = _lessonsList[index];
          bool? checkboxValue = _checkboxValues[index];
          return CheckboxListTile(
            title: Text(lesson.lessonName ?? ''),
            controlAffinity: ListTileControlAffinity.leading,
            contentPadding: EdgeInsets.zero,
            value: checkboxValue,
            onChanged: (bool? value) => myFunction(value, index, lesson),
          );
        },
      ),
    );
  },
),

void myFunction(bool? value, int index, Lesson lesson) { //I don't know what type lesson is, I just assumed Lesson here
  _checkedLesson.add(lesson);
  setState(
        () {
      _checkboxValues[index] = value ?? false;
    },
  );
}

CodePudding user response:

The onChanged parameter takes a function with a nullable boolean parameter i.e. void Function(bool?)

So you have to create a function such as this one:

void aFunctionToPassIn(bool? value){
 // Do something in here
}

and then pass it to onChanged like onChanged: aFunctionToPassIn,

  • Related