In this tutorial you will learn how to create flutter category selection for multiple selection. We will use Getx for ListView. We will create object instances inside map. In the controller we will create the the map and we will also use Map’s where() and Contain methods. First we will create a model and then controller. Controller will be used inside View to create category objects as children.
Flutter check box list tile could be very useful when you fill in a form or options to choose form. Changing the value and toggling them between true and false could be confusing.
return ListView.builder(
itemCount: controller.categories.length,
itemBuilder: (BuildContext context, int index) {
return CheckboxListTile(
value: controller.selectedCategories
.contains(controller.categories[index]),
onChanged: (bool? selected) =>
controller.toggle(controller.categories[index]),
title: CategoryWidget(category: controller.categories[index]),
);
},
)
There are three properties inside CheckboxListTile
value: it’s true or false. This value should be toggled from onChange function
onChange: This is a callback function and it is responsible for changing the boolean value. Since it’s a call back function, you do whatever you want in the function and return true or false. And then the value property will get the changed value.
Once you toggle the value here, The value field will receive it.
title: It’s more like the string value you want to show on the screen. In general it should be a string
Actually with onChange callback, you can do much more things. You can define what values should be updated if you have a list of values.
onChange:(bool? selected){
print(selected);
your logic here
.......
.......
}
Here selected would contain the current value for this check box.
Here you can set a bool which could be true or false. And this bool should a global variable.