TaskDetailPage

PHOTO EMBED

Thu Oct 23 2025 04:05:37 GMT+0000 (Coordinated Universal Time)

Saved by @mehran

// import 'package:flutter/material.dart';
// import 'package:task_manager_app/base/dialogs.dart';
// import 'task.dart';

// class TaskDetailPage extends StatefulWidget {
//   final Task02 task;

//   const TaskDetailPage({super.key, required this.task});

//   @override
//   State<TaskDetailPage> createState() => _TaskDetailPageState();
// }

// class _TaskDetailPageState extends State<TaskDetailPage> {

//   // تابع برای ویرایش وظیفه
//   void _editTask() async {
//     final updatedTask = await showTaskDialog(
//       context: context,
//       existingTask: widget.task,
//     );

//     if (updatedTask != null) {
//       setState(() {
//         widget.task.title = updatedTask.title;
//         widget.task.description = updatedTask.description;
//       });
//       ScaffoldMessenger.of(context).showSnackBar(
//         const SnackBar(content: Text("وظیفه با موفقیت ویرایش شد ✅")),
//       );
//     }
//   }

//   void _deleteTask() {
//     showDialog(
//       context: context,
//       builder: (context) {
//         return AlertDialog(
//           title: const Text("حذف وظیفه"),
//           content: const Text("آیا مطمئن هستید که می‌خواهید این وظیفه را حذف کنید؟"),
//           actions: [
//             TextButton(
//               onPressed: () => Navigator.pop(context),
//               child: const Text("خیر"),
//             ),
//             ElevatedButton(
//               style: ElevatedButton.styleFrom(
//                 backgroundColor: Colors.redAccent,
//               ),
//               onPressed: () {
//                 Navigator.pop(context);
//                 Navigator.pop(context, "delete");
//               },
//               child: const Text("بله، حذف کن"),
//             ),
//           ],
//         );
//       },
//     );
//   }

//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//       appBar: AppBar(
//         title: Text(widget.task.title),
//         backgroundColor: Colors.blueAccent,
//         actions: [
//           IconButton(
//             icon: const Icon(Icons.edit),
//             onPressed: _editTask,
//             tooltip: "ویرایش وظیفه",
//           ),
//           IconButton(
//             icon: const Icon(Icons.delete),
//             onPressed: _deleteTask,
//             tooltip: "حذف وظیفه",
//           ),
//         ],
//       ),
//       body: Padding(
//         padding: const EdgeInsets.all(16),
//         child: Column(
//           crossAxisAlignment: CrossAxisAlignment.start,
//           children: [
//             Text(
//               "عنوان: ${widget.task.title}",
//               style: const TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
//             ),
//             const SizedBox(height: 12),
//             Text(
//               "جزئیات: ${widget.task.description}",
//               style: const TextStyle(fontSize: 18),
//             ),
//             const SizedBox(height: 12),
//             Row(
//               children: [
//                 const Text("وضعیت: ", style: TextStyle(fontSize: 18)),
//                 Checkbox(
//                   value: widget.task.isDone,
//                   onChanged: (value) {
//                     setState(() {
//                       widget.task.isDone = value!;
//                     });
//                     ScaffoldMessenger.of(context).showSnackBar(
//                       SnackBar(
//                         content: Text(value!
//                             ? "این وظیفه انجام شد 🎯"
//                             : "وظیفه هنوز کامل نشده 😅"),
//                         duration: const Duration(seconds: 1),
//                       ),
//                     );
//                   },
//                 ),
//               ],
//             ),
//             const Spacer(),
//             Center(
//               child: ElevatedButton.icon(
//                 onPressed: () {
//                   Navigator.pop(context, widget.task);
//                 },
//                 icon: const Icon(Icons.arrow_back),
//                 label: const Text("بازگشت"),
//               ),
//             ),
//           ],
//         ),
//       ),
//     );
//   }
// }
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:task_manager_app/base/dialogs.dart';
import 'package:task_manager_app/base/task_model.dart';
import 'task.dart';

class TaskDetailPage extends StatelessWidget {
  final int index;

  const TaskDetailPage({super.key, required this.index});

  @override
  Widget build(BuildContext context) {
    final taskModel = Provider.of<TaskModel>(context);
    
    // چک کردن ایندکس برای جلوگیری از RangeError
    if (index >= taskModel.tasks.length) {
      return Scaffold(
        appBar: AppBar(title: const Text("خطا")),
        body: const Center(child: Text("این تسک دیگر وجود ندارد")),
      );
    }

    final task = taskModel.tasks[index];

    void _editTask() async {
      final updatedTask = await showTaskDialog(
        context: context,
        existingTask: task,
      );

      if (updatedTask != null) {
        taskModel.updateTask(index, updatedTask);
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text("وظیفه با موفقیت ویرایش شد ✅")),
        );
      }
    }

    void _deleteTask() {
      showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: const Text("حذف وظیفه"),
            content: const Text("آیا مطمئن هستید که می‌خواهید این وظیفه را حذف کنید؟"),
            actions: [
              TextButton(
                onPressed: () => Navigator.pop(context),
                child: const Text("خیر"),
              ),
              ElevatedButton(
                style: ElevatedButton.styleFrom(backgroundColor: Colors.redAccent),
                onPressed: () {
                  taskModel.deleteTask(index);
                  Navigator.pop(context); // بستن دیالوگ
                  Navigator.pop(context); // بازگشت به صفحه اصلی
                },
                child: const Text("بله، حذف کن"),
              ),
            ],
          );
        },
      );
    }

    return Scaffold(
      appBar: AppBar(
        title: Text(task.title),
        backgroundColor: Colors.blueAccent,
        actions: [
          IconButton(
            icon: const Icon(Icons.edit),
            onPressed: _editTask,
            tooltip: "ویرایش وظیفه",
          ),
          IconButton(
            icon: const Icon(Icons.delete),
            onPressed: _deleteTask,
            tooltip: "حذف وظیفه",
          ),
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text("عنوان: ${task.title}", style: const TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
            const SizedBox(height: 12),
            Text("جزئیات: ${task.description}", style: const TextStyle(fontSize: 18)),
            const SizedBox(height: 12),
            Row(
              children: [
                const Text("وضعیت: ", style: TextStyle(fontSize: 18)),
                Checkbox(
                  value: task.isDone,
                  onChanged: (value) {
                    taskModel.toggleTaskDone(index, value!);
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(
                        content: Text(value ? "این وظیفه انجام شد 🎯" : "وظیفه هنوز کامل نشده 😅"),
                        duration: const Duration(seconds: 1),
                      ),
                    );
                  },
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
content_copyCOPY