PIbd-31_MasenkinMS_PMU/lib/add_task_dialog.dart

116 lines
3.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'task_provider.dart';
import 'task_model.dart';
import 'package:intl/intl.dart';
class AddTaskDialog extends StatefulWidget {
@override
_AddTaskDialogState createState() => _AddTaskDialogState();
}
class _AddTaskDialogState extends State<AddTaskDialog> {
final _formKey = GlobalKey<FormState>();
String _title = '';
String? _description;
String? _category;
DateTime? _selectedDeadline;
TaskPriority _selectedPriority = TaskPriority.medium;
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text('Add New Task'),
content: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextFormField(
decoration: InputDecoration(labelText: 'Task Title*'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a task title';
}
return null;
},
onSaved: (value) {
_title = value ?? '';
},
),
TextFormField(
decoration: InputDecoration(labelText: 'Description (optional)'),
onSaved: (value) {
_description = value;
},
),
TextFormField(
decoration: InputDecoration(labelText: 'Category (optional)'),
onSaved: (value) {
_category = value;
},
),
SizedBox(height: 20),
Text('Deadline: ${_selectedDeadline != null ? DateFormat('yMMMd').format(_selectedDeadline!) : 'No deadline'}'),
ElevatedButton(
child: Text('Select Deadline'),
onPressed: () => _selectDeadline(context),
),
DropdownButton<TaskPriority>(
value: _selectedPriority,
onChanged: (TaskPriority? newValue) {
setState(() {
_selectedPriority = newValue!;
});
},
items: TaskPriority.values.map((TaskPriority priority) {
return DropdownMenuItem<TaskPriority>(
value: priority,
child: Text(priority.toString().split('.').last),
);
}).toList(),
),
],
),
),
actions: [
TextButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
ElevatedButton(
child: Text('Add Task'),
onPressed: () {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
Provider.of<TaskProvider>(context, listen: false).addTask(
title: _title,
description: _description,
category: _category,
deadline: _selectedDeadline,
priority: _selectedPriority,
);
Navigator.of(context).pop();
}
},
),
],
);
}
Future<void> _selectDeadline(BuildContext context) async {
final DateTime? picked = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime.now(),
lastDate: DateTime(2100),
);
if (picked != null) {
setState(() {
_selectedDeadline = picked;
});
}
}
}