Flutter - Mobx and Checkbox()

Asked

Viewed 105 times

0

I’m using Mobx and I want to create a checkbox list, that list is already created, however I can’t make the values change when we click the checkbox, although the logic is running and adds items to the list the change is not detected, but if refresh already appears chekbox checked.

I have the following store:

ObservableList<Category> categoryCheck = ObservableList<Category>();
ObservableList<Category> categoriesTree = ObservableList<Category>();

@action
  void removeCategoryCheck(Category category) {
    categoryCheck.remove(category);

    categoryCheck = categoryCheck;
  }

  @action
  void addCategoryCheck(Category category) {
    categoryCheck.add(category);

    categoryCheck = categoryCheck;
  }

@action
  Future<void> getCategoriesTree() async {
    setLoading(true);
    clearCategoriesTree();
    final response = await repository.getCategoriesTree();
    response.map((category) {
      addToCategoriesTree(Category.fromJson(category));
    }).toList();
    categoriesTree = categoriesTree;
    setLoading(false);
  }

On my screen where I show the checkbox list, I have the following:

Widget _buildScreen(context) {
    return Observer(
      builder: (context) {
        return ListView(
          shrinkWrap: true,
          //padding: EdgeInsets.all(15.0),
          children: [                
            _buildCategories(context),
          ],
        );
      },
    );
  }


Widget _buildCategories(context) {
    if (_storeCategories.isLoading) {
      return Container(
        child: Text('A carregar Categorias'),
      );
    } else {
      return Container(
          padding: EdgeInsets.only(top: 10),
          height: 300,
          child: ListView(
            shrinkWrap: false,
            children: [
              TreeView(
                indent: 44,
                iconSize: 30,
                treeController: _controller,
                nodes: [
                  for (var item in _storeCategories.categoriesTree)
                    TreeNode(
                        content: Row(
                          children: [
                            Text(item.name,
                                style: TextStyle(color: Colors.black)),
                            Theme(
                              data: ThemeData(
                                unselectedWidgetColor: Colors.grey,
                              ),
                              child: Checkbox(
                                  value: _storeCategories.inCheckList(item),
                                  activeColor: Colors.green,
                                  onChanged: (bool newValue) {
                                    if (newValue) {
                                      _storeCategories.addCategoryCheck(item);
                                    } else {
                                      _storeCategories
                                          .removeCategoryCheck(item);
                                    }
                                  }),
                            )
                          ],
                        ),
                        children: item.childs.length > 0
                            ? [
                                for (var itemChild in item.childs)
                                  TreeNode(
                                      content: Row(
                                        children: [
                                          Text(itemChild.name,
                                              style: TextStyle(
                                                  color: Colors.black)),
                                          Theme(
                                            data: ThemeData(
                                              unselectedWidgetColor:
                                                  Colors.grey,
                                            ),
                                            child: Checkbox(
                                                value: _storeCategories
                                                    .inCheckList(itemChild),
                                                activeColor: Colors.green,
                                                onChanged: (bool newValue) {
                                                  if (newValue) {
                                                    _storeCategories
                                                        .addCategoryCheck(
                                                            itemChild);
                                                  } else {
                                                    _storeCategories
                                                        .removeCategoryCheck(
                                                            itemChild);
                                                  }
                                                }),
                                          )
                                        ],
                                      ),
                                      children: itemChild.childs.length > 0
                                          ? [
                                              for (var itemChildChild
                                                  in itemChild.childs)
                                                TreeNode(
                                                    content: Row(
                                                  children: [
                                                    Text(itemChildChild.name,
                                                        style: TextStyle(
                                                            color:
                                                                Colors.white)),
                                                    Theme(
                                                      data: ThemeData(
                                                        unselectedWidgetColor:
                                                            Colors.grey,
                                                      ),
                                                      child: Checkbox(
                                                          value: _storeCategories
                                                              .inCheckList(
                                                                  itemChildChild),
                                                          activeColor:
                                                              Colors.green,
                                                          onChanged:
                                                              (bool newValue) {
                                                            if (newValue) {
                                                              _storeCategories
                                                                  .addCategoryCheck(
                                                                      itemChildChild);
                                                            } else {
                                                              _storeCategories
                                                                  .removeCategoryCheck(
                                                                      itemChildChild);
                                                            }
                                                          }),
                                                    )
                                                  ],
                                                )),
                                            ]
                                          : []),
                              ]
                            : []),
                ],
              )
            ],
          ));
    }
  }
  • Your question is a little complex, and can be solved in a few different ways... But to summarize, so that your list "listen" to the modifications of Checkado you need to build the list from the Observablelist which receives these changes. Which in your case is the categoryCheck.

  • Take a look at my github Desafio_campominado there you find an example that can help you a lot. Follow some useful files: Minesweepercontroller; Fieldmodel; Minefield.

1 answer

0

Eventually I solved it, I created a new list only with the id’s of the categories, that is, whenever I add a new object to the list of selected categories, I also add the id to a list of strings, then in the checkbox "value" I check if the id exists in that list of ids.

Checkbox:

                                              child: Checkbox(
                                                  value: _storeCategories
                                                      .trueChecks
                                                      .contains(
                                                          itemChildChild
                                                              .id),
                                                  activeColor:
                                                      Colors.green,
                                                  onChanged: (bool
                                                      newValue) async {
                                                    if (newValue) {
                                                      _storeCategories
                                                          .addCategoryCheck(
                                                              itemChildChild);
                                                    } else {
                                                      _storeCategories
                                                          .removeCategoryCheck(
                                                              itemChildChild);
                                                    }
                                                  }),

Store With new list:

ObservableList<String> arrayIds = ObservableList<String>().asObservable();

Thus with a string list the element is reactive.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.