Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions src/lib/features/tasks/view/screens/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,27 +177,6 @@ class HomeScreen extends StatelessWidget {
color: viewModel.sortByPriority
? Colors.white
: AppColors.primaryBlue,
// --- SỬ DỤNG MOCK DATA VÀO TASKCARD ---
TaskCard(
task: task1, // Truyền task1 vào đây
leading: Stack(
children: [
const CircleAvatar(radius: 15, backgroundImage: NetworkImage('https://i.pravatar.cc/150?u=user2')),
const Positioned(left: 10, child: CircleAvatar(radius: 15, backgroundImage: NetworkImage('https://i.pravatar.cc/150?u=user3'))),
const Positioned(left: 20, child: CircleAvatar(radius: 15, backgroundImage: NetworkImage('https://i.pravatar.cc/150?u=user4'))),
Positioned(
left: 30,
child: Container(
padding: const EdgeInsets.all(4),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
shape: BoxShape.circle,
),
child: Icon(
Icons.add_rounded,
size: 20,
color: Theme.of(context).colorScheme.primary,
),
),
const SizedBox(width: 5),
Text(
Expand Down
228 changes: 196 additions & 32 deletions src/lib/features/tasks/view/screens/task_detail_screen.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import '../../../../core/theme/app_colors.dart';
import '../../../../core/widgets/custom_input_field.dart';
import '../../model/task_model.dart';
import '../widgets/task_widgets.dart'; // Contains TimePickerWidget
import '../../viewmodel/task_viewmodel.dart';
import '../widgets/task_widgets.dart';

class TaskDetailScreen extends StatefulWidget {
final TaskModel task;

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

@override
Expand All @@ -19,50 +21,60 @@ class _TaskDetailScreenState extends State<TaskDetailScreen> {
late TimeOfDay _startTime;
late TimeOfDay _endTime;
late String _currentCategory;
late List<TagModel> _currentTags;

@override
void initState() {
super.initState();
// Initialize state variables with services from the passed task object
_titleController = TextEditingController(text: widget.task.title);
_descController = TextEditingController(text: widget.task.description);
_startTime = widget.task.startTime;
_endTime = widget.task.endTime;
_currentCategory = widget.task.category;
_currentTags = List.from(widget.task.tags);
}

@override
void dispose() {
// Dispose controllers to prevent memory leaks
_titleController.dispose();
_descController.dispose();
super.dispose();
}

void _toggleTag(TagModel tag) {
setState(() {
if (_currentTags.any((t) => t.id == tag.id)) {
_currentTags.removeWhere((t) => t.id == tag.id);
} else {
_currentTags.add(tag);
}
});
}

bool _isTagSelected(TagModel tag) => _currentTags.any((t) => t.id == tag.id);

void _saveChanges() {
// Update the local model
// (Note: Later, this will call TaskViewModel -> TaskService -> your ASP.NET Core API to update the database)
widget.task.title = _titleController.text;
widget.task.description = _descController.text;
widget.task.startTime = _startTime;
widget.task.endTime = _endTime;
widget.task.category = _currentCategory;

// Show success message
// Lưu tags mới vào task qua ViewModel
context.read<TaskViewModel>().updateTaskTags(widget.task.id, _currentTags);

ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: const Text('Task updated successfully!'),
backgroundColor: Theme.of(context).colorScheme.tertiary,
),
);

// Return to the previous screen
Navigator.pop(context);
}
Comment on lines 56 to 73
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Direct mutation of widget.task may cause unexpected side effects.

_saveChanges() directly mutates widget.task properties (lines 57-61). This modifies the original TaskModel object that was passed to this screen, which can lead to:

  • UI inconsistencies if the parent widget doesn't expect the object to change
  • Difficulty tracking state changes
  • Potential issues with provider/state management

Consider updating the task through the ViewModel instead of direct mutation.

🐛 Suggested approach using ViewModel

Add an updateTask method to TaskViewModel that handles all task field updates:

// In TaskViewModel
void updateTask(String taskId, {
  String? title,
  String? description,
  TimeOfDay? startTime,
  TimeOfDay? endTime,
  String? category,
  List<TagModel>? tags,
}) {
  final index = _tasks.indexWhere((t) => t.id == taskId);
  if (index != -1) {
    final task = _tasks[index];
    if (title != null) task.title = title;
    if (description != null) task.description = description;
    // ... etc
    notifyListeners();
  }
}

Then call this method from _saveChanges() instead of direct mutation.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/lib/features/tasks/view/screens/task_detail_screen.dart` around lines 56
- 73, The _saveChanges method currently mutates widget.task fields directly
which can cause unexpected side effects; instead, add an updateTask method on
TaskViewModel (e.g., TaskViewModel.updateTask(String taskId, {String? title,
String? description, TimeOfDay? startTime, TimeOfDay? endTime, String? category,
List<TagModel>? tags})) that finds the task by id, applies the non-null field
updates, calls notifyListeners(), and then call that method from _saveChanges
(and keep calling updateTaskTags as needed) so the ViewModel owns state changes
rather than mutating widget.task directly.


@override
Widget build(BuildContext context) {
// Format date for display
final viewModel = context.watch<TaskViewModel>();
String formattedDate = DateFormat('EEEE, d MMMM').format(widget.task.date);
final isDark = Theme.of(context).brightness == Brightness.dark;

Expand Down Expand Up @@ -92,8 +104,8 @@ class _TaskDetailScreenState extends State<TaskDetailScreen> {
),
body: SafeArea(
child: Hero(
tag: 'task_card_${widget.task.id}', // Must match the Hero tag in the Home/Statistics screen
child: Material( // Required inside Hero to prevent yellow underline text rendering issues
tag: 'task_card_${widget.task.id}',
child: Material(
type: MaterialType.transparency,
child: Container(
margin: const EdgeInsets.all(20),
Expand All @@ -104,36 +116,51 @@ class _TaskDetailScreenState extends State<TaskDetailScreen> {
? Border.all(color: Theme.of(context).colorScheme.outline)
: null,
boxShadow: [
BoxShadow(color: Colors.black.withValues(alpha: 0.05), blurRadius: 10, offset: const Offset(0, 5))
BoxShadow(
color: Colors.black.withValues(alpha: 0.05),
blurRadius: 10,
offset: const Offset(0, 5),
),
],
),
child: SingleChildScrollView(
padding: const EdgeInsets.all(25.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Input for Task Name
CustomInputField(label: 'Task Name', hint: '', controller: _titleController),
// Task Name
CustomInputField(
label: 'Task Name',
hint: '',
controller: _titleController,
),
const SizedBox(height: 20),

Text('Category Tag', style: Theme.of(context).textTheme.labelLarge),
// Category
Text(
'Category Tag',
style: Theme.of(context).textTheme.labelLarge,
),
const SizedBox(height: 10),

// Horizontal list of Category chips
SizedBox(
height: 40,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: categories.length,
itemBuilder: (context, index) {
bool isSelected = categories[index] == _currentCategory;
bool isSelected =
categories[index] == _currentCategory;
return Padding(
padding: const EdgeInsets.only(right: 10),
child: ChoiceChip(
label: Text(categories[index]),
selected: isSelected,
onSelected: (selected) {
if (selected) setState(() => _currentCategory = categories[index]);
if (selected) {
setState(
() => _currentCategory = categories[index],
);
}
},
backgroundColor: isDark
? Theme.of(context).colorScheme.surfaceContainerHighest
Expand Down Expand Up @@ -161,7 +188,7 @@ class _TaskDetailScreenState extends State<TaskDetailScreen> {
),
const SizedBox(height: 25),

// Display Task Date
// Date
Text('Date', style: Theme.of(context).textTheme.labelLarge),
const SizedBox(height: 5),
Text(
Expand All @@ -174,16 +201,131 @@ class _TaskDetailScreenState extends State<TaskDetailScreen> {
),
const SizedBox(height: 25),

// Time Pickers for Start and End time
// ─── Time Tags ────────────────────────────
Text(
'Thời gian',
style: Theme.of(context).textTheme.labelLarge,
),
const SizedBox(height: 10),
Wrap(
spacing: 8,
runSpacing: 8,
children: viewModel.timeTags.map((tag) {
final isSelected = _isTagSelected(tag);
return GestureDetector(
onTap: () => _toggleTag(tag),
child: AnimatedContainer(
duration: const Duration(milliseconds: 200),
padding: const EdgeInsets.symmetric(
horizontal: 14,
vertical: 8,
),
decoration: BoxDecoration(
color: isSelected
? tag.color
: tag.color.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(20),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (isSelected) ...[
const Icon(
Icons.check,
color: Colors.white,
size: 14,
),
const SizedBox(width: 4),
],
Text(
tag.name,
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w500,
color: isSelected
? Colors.white
: tag.color,
),
),
],
),
),
);
}).toList(),
),
const SizedBox(height: 20),

// ─── Status Tags ──────────────────────────
Text(
'Trạng thái',
style: Theme.of(context).textTheme.labelLarge,
),
const SizedBox(height: 10),
Wrap(
spacing: 8,
runSpacing: 8,
children: viewModel.statusTags.map((tag) {
final isSelected = _isTagSelected(tag);
return GestureDetector(
onTap: () => _toggleTag(tag),
child: AnimatedContainer(
duration: const Duration(milliseconds: 200),
padding: const EdgeInsets.symmetric(
horizontal: 14,
vertical: 8,
),
decoration: BoxDecoration(
color: isSelected
? tag.color
: tag.color.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(20),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (isSelected) ...[
const Icon(
Icons.check,
color: Colors.white,
size: 14,
),
const SizedBox(width: 4),
],
Text(
tag.name,
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w500,
color: isSelected
? Colors.white
: tag.color,
),
),
],
),
),
);
}).toList(),
),
const SizedBox(height: 25),

// Time Pickers
Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Start time', style: Theme.of(context).textTheme.labelLarge),
Text(
'Start time',
style: Theme.of(context).textTheme.labelLarge,
),
const SizedBox(height: 5),
TimePickerWidget(time: _startTime, onChanged: (newTime) => setState(() => _startTime = newTime)),
TimePickerWidget(
time: _startTime,
onChanged: (t) =>
setState(() => _startTime = t),
),
],
),
),
Expand All @@ -192,18 +334,29 @@ class _TaskDetailScreenState extends State<TaskDetailScreen> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('End time', style: Theme.of(context).textTheme.labelLarge),
Text(
'End time',
style: Theme.of(context).textTheme.labelLarge,
),
const SizedBox(height: 5),
TimePickerWidget(time: _endTime, onChanged: (newTime) => setState(() => _endTime = newTime)),
TimePickerWidget(
time: _endTime,
onChanged: (t) => setState(() => _endTime = t),
),
],
),
),
],
),
const SizedBox(height: 25),

// Input for Description
CustomInputField(label: 'Description', hint: '', controller: _descController, maxLines: 3),
// Description
CustomInputField(
label: 'Description',
hint: '',
controller: _descController,
maxLines: 3,
),
const SizedBox(height: 40),

// Save Button
Expand All @@ -213,11 +366,22 @@ class _TaskDetailScreenState extends State<TaskDetailScreen> {
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.primary,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(horizontal: 50, vertical: 15),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15)),
padding: const EdgeInsets.symmetric(
horizontal: 50,
vertical: 15,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
minimumSize: const Size(double.infinity, 50),
),
child: const Text('Save Changes', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
child: const Text(
'Save Changes',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
),
),
],
Expand All @@ -229,4 +393,4 @@ class _TaskDetailScreenState extends State<TaskDetailScreen> {
),
);
}
}
}
Loading
Loading