1
0
mirror of https://github.com/go-vikunja/app synced 2024-06-15 08:54:16 +00:00
app-mirror-github/lib/pages/list/list.dart

126 lines
3.8 KiB
Dart
Raw Normal View History

2018-09-27 15:55:56 +00:00
import 'dart:async';
2022-04-10 13:31:56 +00:00
import 'dart:developer';
2018-09-27 15:55:56 +00:00
2018-09-15 16:21:48 +00:00
import 'package:flutter/material.dart';
2018-09-27 15:55:56 +00:00
import 'package:vikunja_app/components/AddDialog.dart';
import 'package:vikunja_app/components/TaskTile.dart';
2018-09-22 20:56:16 +00:00
import 'package:vikunja_app/global.dart';
2019-03-11 20:29:15 +00:00
import 'package:vikunja_app/models/list.dart';
2018-09-22 20:56:16 +00:00
import 'package:vikunja_app/models/task.dart';
2019-03-16 13:29:00 +00:00
import 'package:vikunja_app/pages/list/list_edit.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> {
2019-03-11 20:29:15 +00:00
TaskList _list;
2018-09-27 15:55:56 +00:00
List<Task> _loadingTasks = [];
bool _loading = true;
@override
void initState() {
2019-03-11 20:29:15 +00:00
_list = TaskList(
id: widget.taskList.id, title: widget.taskList.title, tasks: []);
super.initState();
}
2018-09-15 17:40:59 +00:00
2018-09-27 15:55:56 +00:00
@override
void didChangeDependencies() {
2019-03-15 06:52:50 +00:00
_loadList();
super.didChangeDependencies();
2018-09-27 15:55:56 +00:00
}
2018-09-15 16:21:48 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
2019-03-15 22:14:37 +00:00
appBar: AppBar(
title: new Text(_list.title),
actions: <Widget>[
IconButton(
icon: Icon(Icons.edit),
onPressed: () =>
Navigator.push(
2019-03-15 22:14:37 +00:00
context,
MaterialPageRoute(
builder: (context) => ListEditPage(
list: _list,
))).whenComplete(() {
_loadList();
setState(() {});
})
)
2019-03-15 22:14:37 +00:00
],
),
body: !this._loading
? RefreshIndicator(
child: _list.tasks.length > 0
? ListView(
padding: EdgeInsets.symmetric(vertical: 8.0),
children: ListTile.divideTiles(
context: context, tiles: _listTasks())
.toList(),
)
: Center(child: Text('This list is empty.')),
2019-03-15 22:14:37 +00:00
onRefresh: _loadList,
)
: Center(child: CircularProgressIndicator()),
floatingActionButton: Builder(
builder: (context) => FloatingActionButton(
onPressed: () => _addItemDialog(context), child: Icon(Icons.add)),
));
2018-09-15 16:21:48 +00:00
}
2018-09-15 17:40:59 +00:00
2018-09-27 15:55:56 +00:00
List<Widget> _listTasks() {
var tasks = (_list.tasks.map(_buildTile) ?? []).toList();
//tasks.addAll(_loadingTasks.map(_buildLoadingTile));
2018-09-27 15:55:56 +00:00
return tasks;
}
2018-09-27 15:55:56 +00:00
TaskTile _buildTile(Task task) {
// key: UniqueKey() seems like a weird workaround to fix the loading issue
// is there a better way?
return TaskTile(key: UniqueKey(), task: task,onEdit: () => _loadList());
}
2019-03-15 06:52:50 +00:00
Future<void> _loadList() {
2018-09-27 15:55:56 +00:00
return VikunjaGlobal.of(context)
.listService
.get(widget.taskList.id)
.then((list) {
setState(() {
2018-09-27 15:55:56 +00:00
_loading = false;
_list = list;
});
});
}
2019-03-15 06:52:50 +00:00
_addItemDialog(BuildContext context) {
2018-09-15 17:40:59 +00:00
showDialog(
2018-09-27 15:55:56 +00:00
context: context,
builder: (_) => AddDialog(
2019-03-15 06:52:50 +00:00
onAdd: (name) => _addItem(name, context),
2018-09-27 15:55:56 +00:00
decoration: new InputDecoration(
2019-03-11 20:38:05 +00:00
labelText: 'Task Name', hintText: 'eg. Milk')));
2018-09-15 17:40:59 +00:00
}
2019-03-15 06:52:50 +00:00
_addItem(String name, BuildContext context) {
var globalState = VikunjaGlobal.of(context);
2020-06-15 21:48:15 +00:00
var newTask = Task(
id: null, title: name, owner: globalState.currentUser, done: false, loading: true);
setState(() => _list.tasks.add(newTask));
globalState.taskService.add(_list.id, newTask).then((_) {
_loadList().then((_) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('The task was added successfully!'),
));
});
2019-03-14 21:27:13 +00:00
});
}
2018-09-15 16:21:48 +00:00
}