1
0
mirror of https://github.com/go-vikunja/app synced 2024-05-29 00:36:49 +00:00
app-mirror-github/lib/components/TaskTile.dart

187 lines
5.4 KiB
Dart
Raw Normal View History

2018-09-27 15:55:56 +00:00
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:provider/provider.dart';
2024-02-15 21:32:17 +00:00
import 'package:vikunja_app/components/TaskBottomSheet.dart';
2018-09-27 15:55:56 +00:00
import 'package:vikunja_app/models/task.dart';
import 'package:vikunja_app/utils/misc.dart';
import 'package:vikunja_app/pages/list/task_edit.dart';
2023-11-28 16:05:30 +00:00
import 'package:vikunja_app/utils/priority.dart';
import '../stores/project_store.dart';
2018-09-27 15:55:56 +00:00
2018-09-27 15:55:56 +00:00
class TaskTile extends StatefulWidget {
final Task task;
final Function onEdit;
final bool showInfo;
2018-09-27 15:55:56 +00:00
final bool loading;
2022-08-27 21:04:43 +00:00
final ValueSetter<bool>? onMarkedAsDone;
2018-09-27 15:55:56 +00:00
2022-09-03 15:43:16 +00:00
const TaskTile({
Key? key,
required this.task,
required this.onEdit,
this.loading = false,
this.showInfo = false,
this.onMarkedAsDone,
}) : super(key: key);
/*
2018-09-27 15:55:56 +00:00
@override
TaskTileState createState() {
return new TaskTileState(this.task, this.loading);
}
*/
2024-02-15 21:32:17 +00:00
@override
TaskTileState createState() => TaskTileState(this.task);
2018-09-27 15:55:56 +00:00
}
Widget? _buildTaskSubtitle(Task? task, bool showInfo, BuildContext context) {
2023-11-28 16:05:30 +00:00
Duration? durationUntilDue = task?.dueDate?.difference(DateTime.now());
2024-02-15 21:32:17 +00:00
if (task == null) return null;
2023-11-28 16:05:30 +00:00
List<TextSpan> texts = [];
2024-02-15 21:32:17 +00:00
if (showInfo && task.hasDueDate) {
texts.add(TextSpan(
text: "Due " + durationToHumanReadable(durationUntilDue!),
style: durationUntilDue.isNegative
? TextStyle(color: Colors.red)
: Theme.of(context).textTheme.bodyMedium));
2023-11-28 16:05:30 +00:00
}
2024-02-15 21:32:17 +00:00
if (task.priority != null && task.priority != 0) {
texts.add(TextSpan(
text: " !" + priorityToString(task.priority),
style: TextStyle(color: Colors.orange)));
2023-11-28 16:05:30 +00:00
}
//if(texts.isEmpty && task.description.isNotEmpty) {
// return HtmlWidget(task.description);
2024-02-15 21:32:17 +00:00
// }
2023-11-28 16:05:30 +00:00
2024-02-15 21:32:17 +00:00
if (texts.isNotEmpty) {
2023-11-28 16:05:30 +00:00
return RichText(text: TextSpan(children: texts));
}
return null;
}
2022-07-27 18:48:16 +00:00
class TaskTileState extends State<TaskTile> with AutomaticKeepAliveClientMixin {
2018-09-27 15:55:56 +00:00
Task _currentTask;
2022-08-27 21:04:43 +00:00
TaskTileState(this._currentTask);
2018-09-27 15:55:56 +00:00
@override
Widget build(BuildContext context) {
2022-07-27 18:48:16 +00:00
super.build(context);
final taskState = Provider.of<ProjectProvider>(context);
if (_currentTask.loading) {
2018-09-27 15:55:56 +00:00
return ListTile(
leading: Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(
height: Checkbox.width,
width: Checkbox.width,
child: CircularProgressIndicator(
strokeWidth: 2.0,
)),
),
2022-09-03 15:43:16 +00:00
title: Text(_currentTask.title),
2024-02-15 21:32:17 +00:00
subtitle: _currentTask.description.isEmpty
? null
: HtmlWidget(_currentTask.description),
2019-03-11 20:29:15 +00:00
trailing: IconButton(
icon: Icon(Icons.edit),
2024-02-15 21:32:17 +00:00
onPressed: () {},
),
2018-09-27 15:55:56 +00:00
);
}
2024-02-15 21:32:17 +00:00
return IntrinsicHeight(
child: Row(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
Container(
width: 4.0, // Adjust the width of the red line
color: widget.task.color,
//margin: EdgeInsets.only(left: 10.0),
),
Flexible(
child: ListTile(
onTap: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return TaskBottomSheet(task: widget.task, onEdit: widget.onEdit, taskState: taskState);
2024-02-15 21:32:17 +00:00
});
},
title: widget.showInfo
? RichText(
text: TextSpan(
text: null,
children: <TextSpan>[
// TODO: get list name of task
//TextSpan(text: widget.task.list.title+" - ", style: TextStyle(color: Colors.grey)),
TextSpan(text: widget.task.title),
],
style: TextStyle(
color: Theme.of(context).brightness == Brightness.dark
? Colors.white
: Colors.black,
),
))
: Text(_currentTask.title),
subtitle: _buildTaskSubtitle(widget.task, widget.showInfo, context),
leading: Checkbox(
value: _currentTask.done,
onChanged: (bool? newValue) {
_change(newValue);
},
),
trailing: IconButton(
icon: Icon(Icons.edit),
2024-02-15 21:32:17 +00:00
onPressed: () {
Navigator.push<Task>(
context,
MaterialPageRoute(
builder: (buildContext) => TaskEditPage(
task: _currentTask,
taskState: taskState,
),
),
2024-02-15 21:32:17 +00:00
)
.then((task) => setState(() {
if (task != null) _currentTask = task;
}))
.whenComplete(() => widget.onEdit());
}),
))
]));
2018-09-27 15:55:56 +00:00
}
2022-08-27 21:04:43 +00:00
void _change(bool? value) async {
value = value ?? false;
2018-09-27 15:55:56 +00:00
setState(() {
this._currentTask.loading = true;
2018-09-27 15:55:56 +00:00
});
Task? newTask = await _updateTask(_currentTask, value);
2018-09-27 15:55:56 +00:00
setState(() {
2024-02-15 21:32:17 +00:00
if (newTask != null) this._currentTask = newTask;
this._currentTask.loading = false;
2018-09-27 15:55:56 +00:00
});
widget.onEdit();
2018-09-27 15:55:56 +00:00
}
Future<Task?> _updateTask(Task task, bool checked) {
return Provider.of<ProjectProvider>(context, listen: false).updateTask(
context: context,
task: task.copyWith(
done: checked,
),
);
2018-09-27 15:55:56 +00:00
}
2022-07-27 18:48:16 +00:00
@override
bool get wantKeepAlive => _currentTask != widget.task;
2018-09-27 15:55:56 +00:00
}
typedef Future<void> TaskChanged(Task task, bool newValue);