1
0
mirror of https://github.com/go-vikunja/app synced 2024-06-02 18:49:47 +00:00
app-mirror-github/lib/pages/home.dart
Timo Reichl ee99869cf6
fix: warnings (#1)
* Ran make format

Signed-off-by: Timo Reichl <timo.reichl@mailbox.org>

* Add VS Code launch config

Signed-off-by: Timo Reichl <timo.reichl@mailbox.org>

* pages/list/list.dart: Stop spinning wheel after adding a task

Signed-off-by: Timo Reichl <timo.reichl@mailbox.org>

* stores/list_store.dart: Fix updateTask() not being a future

Signed-off-by: Timo Reichl <timo.reichl@mailbox.org>

* Replace FlatButton with TextButton widgets

Signed-off-by: Timo Reichl <timo.reichl@mailbox.org>

* components/TaskTile.dart: Remove dead code

Signed-off-by: Timo Reichl <timo.reichl@mailbox.org>

* theme/theme.dart: Fix accentColor deprecation

Signed-off-by: Timo Reichl <timo.reichl@mailbox.org>

* pages/list/list_edit.dart: Fix SnackBar.hideCurrentSnackBar() deprecation

Signed-off-by: Timo Reichl <timo.reichl@mailbox.org>

* Remove unused folder lib/managers

Signed-off-by: Timo Reichl <timo.reichl@mailbox.org>
2021-12-21 12:22:17 +01:00

187 lines
5.9 KiB
Dart

import 'dart:async';
import 'package:after_layout/after_layout.dart';
import 'package:flutter/material.dart';
import 'package:vikunja_app/components/AddDialog.dart';
import 'package:vikunja_app/components/ErrorDialog.dart';
import 'package:vikunja_app/global.dart';
import 'package:vikunja_app/models/namespace.dart';
import 'package:vikunja_app/pages/namespace/namespace.dart';
import 'package:vikunja_app/pages/namespace/namespace_edit.dart';
import 'package:vikunja_app/pages/placeholder.dart';
class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() => HomePageState();
}
class HomePageState extends State<HomePage> with AfterLayoutMixin<HomePage> {
List<Namespace> _namespaces = [];
Namespace get _currentNamespace =>
_selectedDrawerIndex >= 0 && _selectedDrawerIndex < _namespaces.length
? _namespaces[_selectedDrawerIndex]
: null;
int _selectedDrawerIndex = -1;
bool _loading = true;
bool _showUserDetails = false;
@override
void afterFirstLayout(BuildContext context) {
_loadNamespaces();
}
Widget _namespacesWidget() {
List<Widget> namespacesList = <Widget>[];
_namespaces.asMap().forEach((i, namespace) => namespacesList.add(ListTile(
leading: const Icon(Icons.folder),
title: Text(namespace.title),
selected: i == _selectedDrawerIndex,
onTap: () => _onSelectItem(i),
)));
return this._loading
? Center(child: CircularProgressIndicator())
: RefreshIndicator(
child: ListView(
padding: EdgeInsets.zero,
children:
ListTile.divideTiles(context: context, tiles: namespacesList)
.toList(),
),
onRefresh: _loadNamespaces,
);
}
Widget _userDetailsWidget(BuildContext context) {
return ListView(padding: EdgeInsets.zero, children: <Widget>[
ListTile(
title: Text('Logout'),
leading: Icon(Icons.exit_to_app),
onTap: () {
VikunjaGlobal.of(context).logoutUser(context);
},
),
]);
}
@override
Widget build(BuildContext context) {
var currentUser = VikunjaGlobal.of(context).currentUser;
return Scaffold(
appBar: AppBar(
title: Text(_currentNamespace?.title ?? 'Vikunja'),
actions: _currentNamespace == null
? null
: <Widget>[
IconButton(
icon: Icon(Icons.edit),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NamespaceEditPage(
namespace: _currentNamespace,
)))),
],
),
drawer: Drawer(
child: Column(children: <Widget>[
UserAccountsDrawerHeader(
// Removed until we find a way to disable the user email only for some occasions and not everywhere
accountEmail:
currentUser?.email == null ? null : Text(currentUser.email),
accountName: currentUser?.username == null
? null
: Text(currentUser.username),
onDetailsPressed: () {
setState(() {
_showUserDetails = !_showUserDetails;
});
},
currentAccountPicture: currentUser == null
? null
: CircleAvatar(
backgroundImage:
NetworkImage(currentUser.avatarUrl(context)),
),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/graphics/hypnotize.png"),
repeat: ImageRepeat.repeat,
colorFilter: ColorFilter.mode(
Theme.of(context).primaryColor, BlendMode.multiply),
),
),
),
Builder(
builder: (BuildContext context) => Expanded(
child: _showUserDetails
? _userDetailsWidget(context)
: _namespacesWidget(),
)),
Align(
alignment: FractionalOffset.bottomCenter,
child: Builder(
builder: (context) => ListTile(
leading: const Icon(Icons.add),
title: const Text('Add namespace...'),
onTap: () => _addNamespaceDialog(context),
),
),
),
]),
),
body: _getDrawerItemWidget(_selectedDrawerIndex),
);
}
_getDrawerItemWidget(int pos) {
if (pos == -1) {
return PlaceholderPage();
}
return NamespacePage(namespace: _namespaces[pos]);
}
_onSelectItem(int index) {
setState(() => _selectedDrawerIndex = index);
Navigator.of(context).pop();
}
_addNamespaceDialog(BuildContext context) {
showDialog(
context: context,
builder: (_) => AddDialog(
onAdd: (name) => _addNamespace(name, context),
decoration: InputDecoration(
labelText: 'Namespace',
hintText: 'eg. Personal Namespace',
),
));
}
_addNamespace(String name, BuildContext context) {
VikunjaGlobal.of(context)
.namespaceService
.create(Namespace(id: null, title: name))
.then((_) {
_loadNamespaces();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('The namespace was created successfully!'),
));
}).catchError((error) => showDialog(
context: context,
builder: (context) => ErrorDialog(error: error),
));
}
Future<void> _loadNamespaces() {
return VikunjaGlobal.of(context).namespaceService.getAll().then((result) {
setState(() {
_loading = false;
_namespaces = result;
});
});
}
}