mardi 21 septembre 2021

How to display tasks that are not "checked" on the other screen?

I am looking at my code and wondering for 2 hours now without luck so I will ask for help here.

I have a button, when I press it, it displays a random item from the list view. The problem is I also have a check box on the list view with each item. I do not want it to (Shuffle through the items with the checkbox ticked) only to shuffle through the Task in the list view that are unchecked/unticked/are not done.

Here is my code

class TaskData extends ChangeNotifier {
  List<Task> _tasks = [
    Task(name: "item1"),
    Task(name: "item2"),
    Task(name: "item3"),

  ];

  UnmodifiableListView<Task> get tasks {
    return UnmodifiableListView(_tasks);
  }

  int get taskCount {
    return _tasks.length;
  }
// <<Here is the code that shuffles through list
  Future<String> rann() async {
    return (_tasks.toList()..shuffle()).first.name;
  }
 
  void addTask(String newTaskTitle) {
    final task = Task(name: newTaskTitle);
    _tasks.add(task);
    notifyListeners();
  }

  void updateTask(Task task) {
    task.toggleDone();
    notifyListeners();
  }

In another script I have this one

class Task {
  final String name;
  bool isDone;

  Task({required this.name, this.isDone = false});

  void toggleDone() {
    isDone = !isDone;
  }
}

In another script file I have this code

        Padding(
          padding:
              const EdgeInsets.symmetric(horizontal: 20, vertical: 0),
          child: FutureBuilder(
            future: Provider.of<TaskData>(context).rann(),
            builder: (context, snapshot) {
              return Align(
                alignment: Alignment.center,
                child: Text(
                  "${snapshot.data}",
                  //softWrap: true,
                  textAlign: TextAlign.center,
                  //textWidthBasis: TextWidthBasis.longestLine,
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: 30,
                      fontWeight: FontWeight.w700),
                ),
              );
            },
          ),
        ),

In another script I have this one

class TasksList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<TaskData>(
      builder: (context, taskData, child) {
        return ListView.builder(
          itemBuilder: (context, index) {
            final task = taskData.tasks[index];
            return TaskTile(
              taskTitle: task.name,
              isChecked: task.isDone,
              checkboxCallback: (checkboxState) {
                taskData.updateTask(task);
              },
            );
          },
          itemCount: taskData.taskCount,
        );
      },
    );
  }
}

Any help would be appreciated!

Edit : I also forgot to include this part of code

class TaskTile extends StatelessWidget {
  final bool isChecked;
  final String taskTitle;
  final Function(bool?) checkboxCallback;
  final VoidCallback longPressCallback;

  TaskTile(
      {required this.isChecked,
      required this.taskTitle,
      required this.checkboxCallback,
      required this.longPressCallback});


  @override
  Widget build(BuildContext context) {
    return ListTile(
      onLongPress: longPressCallback,
      title: Text(
        taskTitle,
        // at the bottom, it sets decoration of text if isChecked is true, if its not its null
        style: TextStyle(
            decoration: isChecked ? TextDecoration.lineThrough : null),
      ),
      trailing: Checkbox(
        activeColor: Colors.blue,
        value: isChecked,
        onChanged: checkboxCallback,
      ),
    );
  }
}



Aucun commentaire:

Enregistrer un commentaire