Generate lists with hive in flutter

Viewed 49

I'm trying to generate pre-made lists in a Todo like app for work, but I can't figure out how. The lists changes depending on a dropdown value.

I'm able to write the lists in JSON and display them in the app but the main problem is saving the checkbox value for each item.

Please let me know if you need any more information.

Here is some of my code.

Visibility(
  visible: categorySelector,
  replacement: ListView.builder(
      shrinkWrap: true,
      itemCount: _nbtemplate.length,
      itemBuilder: (context, index) {
        return Container(
          margin: const EdgeInsets.all(4),
          decoration: BoxDecoration(
            border: Border.all(
              width: 2,
              color: widget.project.isCompleted ||
                  widget.template.isCompleted
                  ? Colors.green
                  : Colors.grey,
            ),
          ),
          child: ListTile(
            leading: Checkbox(
              value: checkBoxValue, ||
              widget.template.isCompleted,
              onChanged: (value) {
                setState(() {
                  checkBoxValue = value!;
                  _saveTile();
                });
                if (widget.project.isCompleted) {
                  widget.project.isCompleted = false;
                  updateProject(widget.project);
                }
                updateTmpl(widget.project, widget.template, value!);
              },
            ),
            title: Text(
              _nbtemplate[index]["name"],
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w500,
                decoration:
                    widget.project.isCompleted || checkBoxValue
                        ? TextDecoration.lineThrough
                        : null,
                decorationColor: Colors.green,
                decorationThickness: 2,
              ),
            ),
            subtitle: Text(
              _nbtemplate[index]["el"],
              style: TextStyle(
                fontSize: 12,
                decoration: widget.project.isCompleted ||
                    widget.template.isCompleted
                    ? TextDecoration.lineThrough
                    : null,
                decorationColor: Colors.green,
                decorationThickness: 2,
              ),
            ),
          ),
        );
      }),
  child: ListView.builder(
      shrinkWrap: true,
      itemCount: _shtemplate.length,
      itemBuilder: (context, index) {
        return Container(
          margin: const EdgeInsets.all(4),
          decoration: BoxDecoration(
            border: Border.all(
              width: 2,
              color: widget.project.isCompleted ||
                  widget.template.isCompleted
                  ? Colors.green
                  : Colors.grey,
            ),
          ),
          child: ListTile(
            leading: Checkbox(
              value: widget.project.isCompleted, ||
              widget.template.isCompleted,
              onChanged: (value) {
                setState(() {
                  widget.template.isCompleted = value!;
                });
                if (widget.project.isCompleted) {
                  widget.project.isCompleted = false;
                  updateProject(widget.project);
                }
                updateTmpl(widget.project, widget.template, value!);
              },
            ),
            title: Text(
              _shtemplate[index]["name"],
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w500,
                decoration: widget.project.isCompleted ||
                    widget.template.isCompleted
                    ? TextDecoration.lineThrough
                    : null,
                decorationColor: Colors.green,
                decorationThickness: 2,
              ),
            ),
            subtitle: Text(
              _shtemplate[index]["el"],
              style: TextStyle(
                fontSize: 12,
                decoration: widget.project.isCompleted ||
                    widget.template.isCompleted
                    ? TextDecoration.lineThrough
                    : null,
                decorationColor: Colors.green,
                decorationThickness: 2,
              ),
            ),
          ),
        );
      })),
1 Answers

If you are using model in project you can define bool checkBox = false; in variables and when each item was selected as done you can pass it to that item variable and save it, but remember when you use bool in model you should define initial value and not null

class TodoModel{
    String title;
    bool isDone = false; // or checkbox or what ever you like to call it
    TodoModel({required this.title,this.isDone});
}
Related