2018-09-15 16:21:48 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2018-09-17 16:16:50 +00:00
|
|
|
import 'package:fluttering_vikunja/global.dart';
|
|
|
|
import 'package:fluttering_vikunja/models/task.dart';
|
2018-09-15 16:21:48 +00:00
|
|
|
|
|
|
|
class ListPage extends StatefulWidget {
|
2018-09-17 16:16:50 +00:00
|
|
|
final TaskList taskList;
|
2018-09-15 16:21:48 +00:00
|
|
|
|
2018-09-17 16:16:50 +00:00
|
|
|
ListPage({this.taskList}) : super(key: Key(taskList.id.toString()));
|
2018-09-15 16:21:48 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
_ListPageState createState() => _ListPageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ListPageState extends State<ListPage> {
|
2018-09-17 16:16:50 +00:00
|
|
|
TaskList items;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
items = TaskList(
|
|
|
|
id: widget.taskList.id, title: widget.taskList.title, tasks: []);
|
|
|
|
super.initState();
|
|
|
|
}
|
2018-09-15 17:40:59 +00:00
|
|
|
|
2018-09-15 16:21:48 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2018-09-17 16:16:50 +00:00
|
|
|
title: new Text(items.title),
|
2018-09-15 16:21:48 +00:00
|
|
|
),
|
2018-09-15 17:40:59 +00:00
|
|
|
body: ListView(
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 8.0),
|
2018-09-16 19:47:33 +00:00
|
|
|
children: ListTile.divideTiles(
|
|
|
|
context: context,
|
2018-09-17 16:16:50 +00:00
|
|
|
tiles: items?.tasks?.map((task) => CheckboxListTile(
|
|
|
|
title: Text(task.text),
|
2018-09-16 19:47:33 +00:00
|
|
|
controlAffinity: ListTileControlAffinity.leading,
|
2018-09-17 16:16:50 +00:00
|
|
|
value: task.done ?? false,
|
|
|
|
subtitle: task.description == null
|
|
|
|
? null
|
|
|
|
: Text(task.description),
|
|
|
|
onChanged: (bool value) => _updateTask(task, value),
|
|
|
|
)) ??
|
|
|
|
[])
|
2018-09-16 19:47:33 +00:00
|
|
|
.toList(),
|
2018-09-15 16:21:48 +00:00
|
|
|
),
|
2018-09-16 19:47:33 +00:00
|
|
|
floatingActionButton: FloatingActionButton(
|
2018-09-17 16:16:50 +00:00
|
|
|
onPressed: () => _addItemDialog(), child: Icon(Icons.add)),
|
2018-09-15 16:21:48 +00:00
|
|
|
);
|
|
|
|
}
|
2018-09-15 17:40:59 +00:00
|
|
|
|
2018-09-17 16:16:50 +00:00
|
|
|
@override
|
|
|
|
void didChangeDependencies() {
|
|
|
|
super.didChangeDependencies();
|
|
|
|
_updateList();
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateTask(Task task, bool checked) {
|
|
|
|
// TODO use copyFrom
|
|
|
|
VikunjaGlobal.of(context)
|
|
|
|
.taskService
|
|
|
|
.update(Task(
|
|
|
|
id: task.id,
|
|
|
|
done: checked,
|
|
|
|
text: task.text,
|
|
|
|
description: task.description,
|
|
|
|
owner: null,
|
|
|
|
))
|
|
|
|
.then((_) => _updateList());
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateList() {
|
|
|
|
VikunjaGlobal.of(context).listService.get(widget.taskList.id).then((tasks) {
|
|
|
|
setState(() {
|
|
|
|
items = tasks;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_addItemDialog() {
|
2018-09-15 17:40:59 +00:00
|
|
|
var textController = new TextEditingController();
|
|
|
|
showDialog(
|
2018-09-16 19:47:33 +00:00
|
|
|
context: context,
|
|
|
|
child: new AlertDialog(
|
|
|
|
contentPadding: const EdgeInsets.all(16.0),
|
|
|
|
content: new Row(children: <Widget>[
|
|
|
|
Expanded(
|
|
|
|
child: new TextField(
|
|
|
|
autofocus: true,
|
|
|
|
decoration: new InputDecoration(
|
|
|
|
labelText: 'List Item', hintText: 'eg. Milk'),
|
|
|
|
controller: textController,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
]),
|
|
|
|
actions: <Widget>[
|
|
|
|
new FlatButton(
|
|
|
|
child: const Text('CANCEL'),
|
|
|
|
onPressed: () => Navigator.pop(context),
|
2018-09-15 17:40:59 +00:00
|
|
|
),
|
2018-09-16 19:47:33 +00:00
|
|
|
new FlatButton(
|
|
|
|
child: const Text('ADD'),
|
|
|
|
onPressed: () {
|
2018-09-17 16:16:50 +00:00
|
|
|
if (textController.text.isNotEmpty) _addItem(textController.text);
|
2018-09-16 19:47:33 +00:00
|
|
|
Navigator.pop(context);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
2018-09-15 17:40:59 +00:00
|
|
|
}
|
2018-09-17 16:16:50 +00:00
|
|
|
|
|
|
|
_addItem(String name) {
|
|
|
|
var globalState = VikunjaGlobal.of(context);
|
|
|
|
globalState.taskService
|
|
|
|
.add(
|
|
|
|
items.id,
|
|
|
|
Task(
|
|
|
|
id: null,
|
|
|
|
text: name,
|
|
|
|
owner: globalState.currentUser,
|
|
|
|
done: false))
|
|
|
|
.then((task) {
|
|
|
|
setState(() {
|
|
|
|
items.tasks.add(task);
|
|
|
|
});
|
|
|
|
}).then((_) => _updateList());
|
|
|
|
}
|
2018-09-15 16:21:48 +00:00
|
|
|
}
|