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/home_page.dart

134 lines
4.4 KiB
Dart
Raw Normal View History

2018-09-27 15:55:56 +00:00
import 'dart:async';
2018-09-15 15:01:45 +00:00
import 'package:flutter/material.dart';
2018-09-27 15:55:56 +00:00
import 'package:vikunja_app/components/AddDialog.dart';
2018-09-22 20:56:16 +00:00
import 'package:vikunja_app/components/GravatarImage.dart';
import 'package:vikunja_app/fragments/namespace.dart';
import 'package:vikunja_app/fragments/placeholder.dart';
import 'package:vikunja_app/global.dart';
import 'package:vikunja_app/models/namespace.dart';
import 'package:vikunja_app/models/task.dart';
import 'package:vikunja_app/models/user.dart';
2018-09-15 15:01:45 +00:00
class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() => new HomePageState();
}
class HomePageState extends State<HomePage> {
List<Namespace> _namespaces = [];
Namespace get _currentNamespace =>
_selectedDrawerIndex >= 0 && _selectedDrawerIndex < _namespaces.length
? _namespaces[_selectedDrawerIndex]
: null;
2018-09-15 15:01:45 +00:00
int _selectedDrawerIndex = -1;
2018-09-27 15:55:56 +00:00
bool _loading = true;
2018-09-15 15:01:45 +00:00
_getDrawerItemWidget(int pos) {
if (pos == -1) {
2018-09-15 15:01:45 +00:00
return new PlaceholderFragment();
}
return new NamespaceFragment(namespace: _namespaces[pos]);
2018-09-15 15:01:45 +00:00
}
_onSelectItem(int index) {
setState(() => _selectedDrawerIndex = index);
Navigator.of(context).pop();
}
2019-03-15 06:52:50 +00:00
_addNamespaceDialog(BuildContext context) {
showDialog(
2018-09-27 15:55:56 +00:00
context: context,
builder: (_) => AddDialog(
2019-03-15 06:52:50 +00:00
onAdd: (name) => _addNamespace(name, context),
decoration: new InputDecoration(
2018-09-27 15:55:56 +00:00
labelText: 'Namespace', hintText: 'eg. Personal Namespace'),
));
}
2019-03-15 06:52:50 +00:00
_addNamespace(String name, BuildContext context) {
VikunjaGlobal.of(context)
.namespaceService
.create(Namespace(id: null, name: name))
2019-03-15 06:52:50 +00:00
.then((_) {
2019-03-15 22:14:37 +00:00
_updateNamespaces();
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('The namespace was created successfully!'),
));
2019-03-15 06:52:50 +00:00
});
}
2018-09-27 15:55:56 +00:00
Future<void> _updateNamespaces() {
return VikunjaGlobal.of(context).namespaceService.getAll().then((result) {
setState(() {
2018-09-27 15:55:56 +00:00
_loading = false;
_namespaces = result;
});
});
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
_updateNamespaces();
2018-09-15 15:01:45 +00:00
}
@override
Widget build(BuildContext context) {
var currentUser = VikunjaGlobal.of(context).currentUser;
2018-09-15 15:01:45 +00:00
List<Widget> drawerOptions = <Widget>[];
_namespaces
.asMap()
.forEach((i, namespace) => drawerOptions.add(new ListTile(
leading: const Icon(Icons.folder),
title: new Text(namespace.name),
selected: i == _selectedDrawerIndex,
onTap: () => _onSelectItem(i),
)));
2018-09-15 15:01:45 +00:00
return new Scaffold(
2018-10-02 15:53:57 +00:00
appBar: AppBar(title: new Text(_currentNamespace?.name ?? 'Vikunja')),
2018-09-15 15:01:45 +00:00
drawer: new Drawer(
child: new Column(children: <Widget>[
new UserAccountsDrawerHeader(
accountEmail: currentUser == null ? null : Text(currentUser.email),
accountName: currentUser == null ? null : Text(currentUser.username),
2018-09-16 19:47:53 +00:00
currentAccountPicture: currentUser == null
? null
: CircleAvatar(
backgroundImage: GravatarImageProvider(currentUser.username)),
2018-09-15 17:40:59 +00:00
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/graphics/hypnotize.png"),
repeat: ImageRepeat.repeat,
colorFilter: ColorFilter.mode(
Theme.of(context).primaryColor, BlendMode.multiply)),
2018-09-15 17:40:59 +00:00
),
),
new Expanded(
2018-09-27 15:55:56 +00:00
child: this._loading
? Center(child: CircularProgressIndicator())
: RefreshIndicator(
child: ListView(
padding: EdgeInsets.zero,
children: ListTile.divideTiles(
context: context, tiles: drawerOptions)
.toList()),
onRefresh: _updateNamespaces,
)),
new Align(
alignment: FractionalOffset.bottomCenter,
2019-03-15 06:52:50 +00:00
child: Builder(
builder: (context) => ListTile(
2019-03-15 22:14:37 +00:00
leading: const Icon(Icons.add),
title: const Text('Add namespace...'),
onTap: () => _addNamespaceDialog(context),
),
),
),
])),
2018-09-15 15:01:45 +00:00
body: _getDrawerItemWidget(_selectedDrawerIndex),
);
}
2018-09-15 15:10:34 +00:00
}