InsideDarkWeb.com

OnChanged State of Checkbox in Flutter

I am working on the todo list app and used CheckBox to check off the todo on the list.

But it keeps going back to the unchecked state on refreshing the page.
I want to save the state in the database.
I am populating the todoList in getAllTodos

Here is the code:

List<Todo>_todoList=List<Todo>();

@override
  initState(){
    super.initState();

    getAllTodos();

  }
        getAllTodos()async{
            _todoService=TodoService();
            _todoList=List<Todo>();
        
            var todos= await _todoService.readTodo();
        
            todos.forEach((todo){
              setState(() {
                var model=Todo();
                model.id=todo['id'];
                model.title=todo['title'];
                model.dueDate=todo['dueDate'];
                model.category=todo['category'];
                model.isFinished=todo['isFinished'];
                _todoList.add(model);
              });
            });
          } 
        
            body: ListView.builder(itemCount: _todoList.length,itemBuilder: (context, index){
                return Padding(
                  padding:  EdgeInsets.only(top:8.0, left: 8.0, right: 8.0),
        

          child: Card (
                elevation: 8.0,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(0)
                ),
                  child: InkWell(
                    onTap: (){
                      setState(() {
                        _todoList[index].isChecked=!_todoList[index].isChecked;
                      });
                    },
                    child: ListTile(
                      leading: Checkbox(
                        checkColor: Colors.indigo,
                        value: _todoList[index].isChecked,
                        onChanged: (bool value){
                          setState(() {
                            _todoList[index].isChecked=value;
_todoService.saveTodo(_todoList[index]);
                          });
                        },
                      ),
                      title: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text(_todoList[index].title ?? 'No Title',
                            style: TextStyle(decoration: (_todoList[index].isChecked? TextDecoration.lineThrough: TextDecoration.none),
                            ),
                          ),
                          IconButton(icon: Icon(Icons.delete,color: Colors.red,
                          ),
                              onPressed: (){
                                _deleteFormDialog(context,_todoList[index].id);
                              }
                          ),
                        ],
                      ),
                      subtitle: Text(_todoList[index].dueDate ?? 'No Due Date'),
                    ),
                  ),
              ),
            );
          }),

Here is the isChecked value:

class Todo{
  bool isChecked=false; 
}

Please help me out.

Update: Added a line in the setState() of onChanged callback calling the service method to change the state of of the checkbox via _todoService.saveTodo(_todoList[index]);

Now the problem is that onChange() is called twice on a single tap. How do I correct the multi-calls in the onChange callback?

Stack Overflow Asked by Ruchita Bhaskar on November 18, 2021

1 Answers

One Answer

Follow the steps from here w.r.t checkbox manipulation.

Hopefully, everything works!

Answered by CATALUNA84 on November 18, 2021

Add your own answers!

Related Questions

How Can i fetch all data with average rating in laravel

1  Asked on December 12, 2020 by nadun-silva

         

Multiple conditions in SELECT Statement

1  Asked on December 12, 2020 by rick-smith

 

Set up different CORS rules based on the endpoint in Django

2  Asked on December 12, 2020 by augusto-samam-barrientos

     

Angular check and uncheck boxes from data

1  Asked on December 12, 2020 by mg91

 

how do i switch a condition based on bind variable

1  Asked on December 12, 2020 by vivek

   

Comparing two triangles in C (3, 3, 4 and 4, 3, 3 for example)

3  Asked on December 12, 2020 by etoile

 

Grouping unique values with low value counts

1  Asked on December 12, 2020 by yupper

 

Why should I press ENTER before CTRL+D to indicate an EOF to stdin?

1  Asked on December 11, 2020 by name-null

         

Pandas dataframe only select the columns that have all True

2  Asked on December 11, 2020 by learningisfun

     

I want to create fixed sidebar but scroll does not work

5  Asked on December 11, 2020 by nav

     

How can I extract all PDF Tags related to content with Python?

2  Asked on December 11, 2020 by martin-thoma

   

React Fizz-Buzz

2  Asked on December 11, 2020 by kamil-kos

   

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.