I need to correct the animation in this flutter code. The completed tasks are getting separated from the incomplete tasks, which shouldn't happen. Also, completed tasks become unclickable. import 'package:flutter/material.dart';

class TaskListPage extends StatefulWidget {
  final String token;
  final String email;

  const TaskListPage({required this.token, required this.email});

  @override
  _TaskListPageState createState() => _TaskListPageState();
}

class _TaskListPageState extends State<TaskListPage> with TickerProviderStateMixin {
  final List<Map<String, dynamic>> tasks = [];
  final textController = TextEditingController();

  @override
  void initState() {
    super.initState();
    fetchTasks();
  }

  Future<void> fetchTasks() async {
    // Implementação da função fetchTasks permanece igual
  }

  Future<void> addTask(String title) async {
    if (title.trim().isEmpty) return;

    setState(() {
      tasks.insert(0, {
        'titulo': title.trim(),
        'concluida': false,
        'isBeingDeleted': false,
        'isAnimating': false,
        'animationController': AnimationController(
          vsync: this,
          duration: const Duration(milliseconds: 300),
        ),
        'offsetAnimation': Tween<Offset>(
          begin: Offset.zero,
          end: const Offset(0, 1),
        ).animate(CurvedAnimation(
          parent: AnimationController(vsync: this, duration: const Duration(milliseconds: 300)),
          curve: Curves.easeInOut,
        )),
      });
    });

    await updateTasksOnServer();
    textController.clear();
  }

  Future<void> updateTasksOnServer() async {
    // Implementação da função updateTasksOnServer permanece igual
  }

  void _toggleCompletion(int index) {
    if (index < 0 || index >= tasks.length) return;

    final task = tasks[index];
    final isCompleted = task['concluida'];

    setState(() {
      tasks[index]['concluida'] = !isCompleted;
      tasks[index]['isAnimating'] = true;
    });

    if (!isCompleted) {
      task['animationController'] = AnimationController(vsync: this, duration: const Duration(milliseconds: 300));
      task['offsetAnimation'] = Tween<Offset>(
        begin: Offset.zero,
        end: Offset(0, 1),
      ).animate(CurvedAnimation(
        parent: task['animationController'],
        curve: Curves.easeInOut,
      ));
      task['animationController'].forward();

      Future.delayed(const Duration(milliseconds: 300), () {
        setState(() {
          task['isAnimating'] = false;
          tasks.removeAt(index);
          tasks.add(task);
        });
      });
    } else {
      _sortTasks();
      Future.delayed(const Duration(milliseconds: 300), () {
        setState(() {
          tasks.forEach((task) => task['isAnimating'] = false);
        });
      });
    }

    updateTasksOnServer();
  }

  void _deleteTaskWithUndo(int index) {
    if (index < 0 || index >= tasks.length) return;

    setState(() {
      tasks[index]['isBeingDeleted'] = true;
    });

    Future.delayed(const Duration(seconds: 3), () {
      if (mounted && tasks[index]['isBeingDeleted'] == true) {
        if (index < tasks.length && tasks[index]['isBeingDeleted'] == true) {
          _deleteTask(index);
        }
      }
    });
  }

  void _undoDeleteTask(int index) {
    if (index < 0 || index >= tasks.length) return;

    setState(() {
      tasks[index]['isBeingDeleted'] = false;
    });
    updateTasksOnServer();
  }

  void _sortTasks() {
    setState(() {
      tasks.sort((a, b) {
        if (a['concluida'] && !b['concluida']) {
          return 1;
        } else if (!a['concluida'] && b['concluida']) {
          return -1;
        } else {
          return 0;
        }
      });
    });
  }

  void _deleteTask(int index) {
    if (index < 0 || index >= tasks.length) return;

    setState(() {
      tasks.removeAt(index);
    });
    updateTasksOnServer();
  }

  Color _getTextColor(Color backgroundColor) {
    return backgroundColor == const Color.fromARGB(255, 245, 245, 245) ? Colors.black : Colors.white;
  }

  Widget _buildItem(BuildContext context, int index) {
    final item = tasks[index];
    final backgroundColor = (index % 3 == 0
        ? const Color.fromARGB(255, 99, 12, 0)
        : index % 3 == 1
            ? const Color.fromARGB(255, 245, 245, 245)
            : const Color.fromARGB(255, 0, 63, 5));

    return AnimatedBuilder(
      animation: item['animationController'],
      builder: (context, child) {
        final tilt = item['isAnimating'] ? item['animationController'].value * 0.05 : 0.0;

        return SlideTransition(
          position: item['offsetAnimation'],
          child: Transform.rotate(
            angle: tilt,
            child: Container(
              margin: const EdgeInsets.symmetric(vertical: 4.0),
              decoration: BoxDecoration(
                color: backgroundColor,
                borderRadius: BorderRadius.circular(8.0),
                border: Border.all(
                  color: const Color.fromARGB(255, 0, 42, 116),
                  width: 2.0,
                ),
              ),
              child: ListTile(
                title: Text(
                  item['titulo'],
                  style: TextStyle(
                    color: _getTextColor(backgroundColor),
                    decoration: item['concluida'] ? TextDecoration.lineThrough : TextDecoration.none,
                  ),
                ),
                trailing: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Checkbox(
                      value: item['concluida'],
                      onChanged: (_) => _toggleCompletion(index),
                    ),
                    item['isBeingDeleted'] == true
                        ? IconButton(
                            icon: const Icon(Icons.undo),
                            onPressed: () => _undoDeleteTask(index),
                          )
                        : IconButton(
                            icon: const Icon(Icons.delete),
                            onPressed: () => _deleteTaskWithUndo(index),
                          ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Task List'),
        backgroundColor: const Color.fromARGB(255, 128, 0, 0),
      ),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: TextField(
              controller: textController,
              onSubmitted: (value) {
                addTask(value);
              },
              decoration: const InputDecoration(
                labelText: 'Add Task',
              ),
            ),
          ),
          Expanded(
            child: Stack(
              children: [
                ListView.builder(
                  padding: const EdgeInsets.all(0),
                  itemCount: tasks.length,
                  itemBuilder: (context, index) {
                    return _buildItem(context, index);
                  },
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}