This repository has been archived on 2022-04-20. You can view files and clone it, but cannot push or open issues or pull requests.
app/lib/pages/list_page.dart

129 lines
3.4 KiB
Dart
Raw Normal View History

2018-09-15 16:21:48 +00:00
import 'package:flutter/material.dart';
2018-09-22 20:56:16 +00:00
import 'package:vikunja_app/global.dart';
import 'package:vikunja_app/models/task.dart';
2018-09-15 16:21:48 +00:00
class ListPage extends StatefulWidget {
final TaskList taskList;
2018-09-15 16:21:48 +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> {
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(
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),
children: ListTile.divideTiles(
context: context,
tiles: items?.tasks?.map((task) => CheckboxListTile(
title: Text(task.text),
controlAffinity: ListTileControlAffinity.leading,
value: task.done ?? false,
subtitle: task.description == null
? null
: Text(task.description),
onChanged: (bool value) => _updateTask(task, value),
)) ??
[])
.toList(),
2018-09-15 16:21:48 +00:00
),
floatingActionButton: FloatingActionButton(
onPressed: () => _addItemDialog(), child: Icon(Icons.add)),
2018-09-15 16:21:48 +00:00
);
}
2018-09-15 17:40:59 +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(
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
),
new FlatButton(
child: const Text('ADD'),
onPressed: () {
if (textController.text.isNotEmpty) _addItem(textController.text);
Navigator.pop(context);
},
)
],
),
);
2018-09-15 17:40:59 +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
}