import 'package:flutter/material.dart'; // ================================ // MODEL CLASS FOR DATA PASSING // ================================ class Task { final int id; final String title; final String description; final bool isCompleted; Task({ required this.id, required this.title, required this.description, this.isCompleted = false, }); Task copyWith({ int? id, String? title, String? description, bool? isCompleted, }) { return Task( id: id ?? this.id, title: title ?? this.title, description: description ?? this.description, isCompleted: isCompleted ?? this.isCompleted, ); } } // ================================ // MAIN APPLICATION // ================================ void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Simple Task Manager', theme: ThemeData( primarySwatch: Colors.blue, useMaterial3: true, ), home: const TaskListScreen(), ); } } // ================================ // MAIN SCREEN - TASK LIST // ================================ class TaskListScreen extends StatefulWidget { const TaskListScreen({super.key}); @override State createState() => _TaskListScreenState(); } class _TaskListScreenState extends State { List tasks = [ Task(id: 1, title: 'Study Flutter', description: 'Learn widgets and layouts'), Task(id: 2, title: 'Practice Forms', description: 'Build form with validation'), Task(id: 3, title: 'Complete Project', description: 'Finish the mobile app'), ]; void _addTask(Task newTask) { setState(() { tasks.add(newTask); }); } void _removeTask(int taskId) { setState(() { tasks.removeWhere((task) => task.id == taskId); }); } void _toggleTask(int taskId) { setState(() { int index = tasks.indexWhere((task) => task.id == taskId); if (index != -1) { tasks[index] = tasks[index].copyWith( isCompleted: !tasks[index].isCompleted, ); } }); } void _showAddTaskForm() { showModalBottomSheet( context: context, isScrollControlled: true, builder: (context) => Padding( padding: EdgeInsets.only( bottom: MediaQuery.of(context).viewInsets.bottom, ), child: AddTaskForm(onAddTask: _addTask), ), ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Task Manager'), backgroundColor: Colors.blue, foregroundColor: Colors.white, ), body: Column( children: [ // Summary Card Container( width: double.infinity, margin: const EdgeInsets.all(16), padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.blue.shade50, borderRadius: BorderRadius.circular(12), border: Border.all(color: Colors.blue.shade200), ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ Column( children: [ Text( '${tasks.length}', style: const TextStyle( fontSize: 24, fontWeight: FontWeight.bold, color: Colors.blue, ), ), const Text('Total Tasks'), ], ), Column( children: [ Text( '${tasks.where((task) => task.isCompleted).length}', style: const TextStyle( fontSize: 24, fontWeight: FontWeight.bold, color: Colors.green, ), ), const Text('Completed'), ], ), ], ), ), // Task List Expanded( child: tasks.isEmpty ? const Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon( Icons.task_alt, size: 80, color: Colors.grey, ), SizedBox(height: 16), Text( 'No tasks yet!', style: TextStyle( fontSize: 18, color: Colors.grey, ), ), ], ), ) : ListView.builder( padding: const EdgeInsets.symmetric(horizontal: 16), itemCount: tasks.length, itemBuilder: (context, index) { final task = tasks[index]; return Card( margin: const EdgeInsets.only(bottom: 8), child: ListTile( leading: GestureDetector( onTap: () => _toggleTask(task.id), child: Container( width: 24, height: 24, decoration: BoxDecoration( shape: BoxShape.circle, border: Border.all( color: task.isCompleted ? Colors.green : Colors.grey, width: 2, ), color: task.isCompleted ? Colors.green : Colors.transparent, ), child: task.isCompleted ? const Icon( Icons.check, color: Colors.white, size: 16, ) : null, ), ), title: Text( task.title, style: TextStyle( decoration: task.isCompleted ? TextDecoration.lineThrough : null, color: task.isCompleted ? Colors.grey : null, ), ), subtitle: Text( task.description, style: TextStyle( color: task.isCompleted ? Colors.grey : null, ), ), trailing: IconButton( icon: const Icon(Icons.delete, color: Colors.red), onPressed: () => _removeTask(task.id), ), onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => TaskDetailScreen(task: task), ), ); }, ), ); }, ), ), ], ), floatingActionButton: FloatingActionButton( onPressed: _showAddTaskForm, child: const Icon(Icons.add), ), ); } } // ================================ // ADD TASK FORM // ================================ class AddTaskForm extends StatefulWidget { final Function(Task) onAddTask; const AddTaskForm({super.key, required this.onAddTask}); @override State createState() => _AddTaskFormState(); } class _AddTaskFormState extends State { final _formKey = GlobalKey(); final _titleController = TextEditingController(); final _descriptionController = TextEditingController(); @override void dispose() { _titleController.dispose(); _descriptionController.dispose(); super.dispose(); } void _submitForm() { if (_formKey.currentState!.validate()) { final newTask = Task( id: DateTime.now().millisecondsSinceEpoch, title: _titleController.text.trim(), description: _descriptionController.text.trim(), ); widget.onAddTask(newTask); Navigator.pop(context); ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Task added successfully!'), backgroundColor: Colors.green, ), ); } } @override Widget build(BuildContext context) { return Container( padding: const EdgeInsets.all(20), child: Form( key: _formKey, child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const Text( 'Add New Task', style: TextStyle( fontSize: 20, fontWeight: FontWeight.bold, ), textAlign: TextAlign.center, ), const SizedBox(height: 20), TextFormField( controller: _titleController, decoration: const InputDecoration( labelText: 'Task Title', border: OutlineInputBorder(), prefixIcon: Icon(Icons.title), ), validator: (value) { if (value == null || value.trim().isEmpty) { return 'Please enter a task title'; } if (value.trim().length < 3) { return 'Title must be at least 3 characters long'; } return null; }, ), const SizedBox(height: 16), TextFormField( controller: _descriptionController, decoration: const InputDecoration( labelText: 'Description', border: OutlineInputBorder(), prefixIcon: Icon(Icons.description), ), maxLines: 3, validator: (value) { if (value == null || value.trim().isEmpty) { return 'Please enter a description'; } return null; }, ), const SizedBox(height: 20), Row( children: [ Expanded( child: OutlinedButton( onPressed: () => Navigator.pop(context), child: const Text('Cancel'), ), ), const SizedBox(width: 12), Expanded( child: ElevatedButton( onPressed: _submitForm, child: const Text('Add Task'), ), ), ], ), ], ), ), ); } } // ================================ // TASK DETAIL SCREEN // ================================ class TaskDetailScreen extends StatelessWidget { final Task task; const TaskDetailScreen({super.key, required this.task}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Task Details'), backgroundColor: Colors.blue, foregroundColor: Colors.white, ), body: Padding( padding: const EdgeInsets.all(20), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // Status Badge Container( padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6), decoration: BoxDecoration( color: task.isCompleted ? Colors.green : Colors.orange, borderRadius: BorderRadius.circular(20), ), child: Text( task.isCompleted ? 'COMPLETED' : 'PENDING', style: const TextStyle( color: Colors.white, fontSize: 12, fontWeight: FontWeight.bold, ), ), ), const SizedBox(height: 20), // Task Title Text( 'Task Title:', style: TextStyle( fontSize: 16, color: Colors.grey.shade600, ), ), const SizedBox(height: 4), Text( task.title, style: const TextStyle( fontSize: 24, fontWeight: FontWeight.bold, ), ), const SizedBox(height: 20), // Task Description Text( 'Description:', style: TextStyle( fontSize: 16, color: Colors.grey.shade600, ), ), const SizedBox(height: 4), Text( task.description, style: const TextStyle(fontSize: 16), ), const SizedBox(height: 20), // Task ID Text( 'Task ID: ${task.id}', style: TextStyle( fontSize: 14, color: Colors.grey.shade500, ), ), const Spacer(), // Back Button SizedBox( width: double.infinity, child: ElevatedButton.icon( onPressed: () => Navigator.pop(context), icon: const Icon(Icons.arrow_back), label: const Text('Back to Tasks'), ), ), ], ), ), ); } }