From 9848c462f841b8df27f19c8c3b4a6d096af28d33 Mon Sep 17 00:00:00 2001 From: konrad Date: Mon, 18 Mar 2019 15:30:54 +0000 Subject: [PATCH] Fixed namespaces loading every time a widget was loaded (#34) --- lib/main.dart | 1 - lib/pages/home.dart | 93 +++++++++++++++--------------- lib/pages/namespace/namespace.dart | 24 ++++---- pubspec.lock | 7 +++ pubspec.yaml | 1 + 5 files changed, 68 insertions(+), 58 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index c60fe13..c7b2f6c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -12,7 +12,6 @@ class VikunjaApp extends StatelessWidget { final Widget home; const VikunjaApp({Key key, this.home}) : super(key: key); - // This widget is the root of your application. @override Widget build(BuildContext context) { return new MaterialApp( diff --git a/lib/pages/home.dart b/lib/pages/home.dart index 710d0c4..652adb0 100644 --- a/lib/pages/home.dart +++ b/lib/pages/home.dart @@ -1,6 +1,8 @@ import 'dart:async'; import 'package:flutter/material.dart'; +import 'package:after_layout/after_layout.dart'; + import 'package:vikunja_app/components/AddDialog.dart'; import 'package:vikunja_app/components/GravatarImage.dart'; import 'package:vikunja_app/pages/namespace/namespace.dart'; @@ -13,7 +15,7 @@ class HomePage extends StatefulWidget { State createState() => new HomePageState(); } -class HomePageState extends State { +class HomePageState extends State with AfterLayoutMixin { List _namespaces = []; Namespace get _currentNamespace => _selectedDrawerIndex >= 0 && _selectedDrawerIndex < _namespaces.length @@ -22,52 +24,8 @@ class HomePageState extends State { int _selectedDrawerIndex = -1; bool _loading = true; - _getDrawerItemWidget(int pos) { - if (pos == -1) { - return new PlaceholderPage(); - } - return new 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: new InputDecoration( - labelText: 'Namespace', hintText: 'eg. Personal Namespace'), - )); - } - - _addNamespace(String name, BuildContext context) { - VikunjaGlobal.of(context) - .namespaceService - .create(Namespace(id: null, name: name)) - .then((_) { - _loadNamespaces(); - Scaffold.of(context).showSnackBar(SnackBar( - content: Text('The namespace was created successfully!'), - )); - }); - } - - Future _loadNamespaces() { - return VikunjaGlobal.of(context).namespaceService.getAll().then((result) { - setState(() { - _loading = false; - _namespaces = result; - }); - }); - } - @override - void didChangeDependencies() { - super.didChangeDependencies(); + void afterFirstLayout(BuildContext context) { _loadNamespaces(); } @@ -128,4 +86,47 @@ class HomePageState extends State { body: _getDrawerItemWidget(_selectedDrawerIndex), ); } + + _getDrawerItemWidget(int pos) { + if (pos == -1) { + return new PlaceholderPage(); + } + return new 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: new InputDecoration( + labelText: 'Namespace', hintText: 'eg. Personal Namespace'), + )); + } + + _addNamespace(String name, BuildContext context) { + VikunjaGlobal.of(context) + .namespaceService + .create(Namespace(id: null, name: name)) + .then((_) { + _loadNamespaces(); + Scaffold.of(context).showSnackBar(SnackBar( + content: Text('The namespace was created successfully!'), + )); + }); + } + + Future _loadNamespaces() { + return VikunjaGlobal.of(context).namespaceService.getAll().then((result) { + setState(() { + _loading = false; + _namespaces = result; + }); + }); + } } diff --git a/lib/pages/namespace/namespace.dart b/lib/pages/namespace/namespace.dart index b4a7d65..85b6f36 100644 --- a/lib/pages/namespace/namespace.dart +++ b/lib/pages/namespace/namespace.dart @@ -2,6 +2,8 @@ import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:after_layout/after_layout.dart'; + import 'package:vikunja_app/components/AddDialog.dart'; import 'package:vikunja_app/global.dart'; import 'package:vikunja_app/models/list.dart'; @@ -17,10 +19,16 @@ class NamespacePage extends StatefulWidget { _NamespacePageState createState() => new _NamespacePageState(); } -class _NamespacePageState extends State { +class _NamespacePageState extends State + with AfterLayoutMixin { List _lists = []; bool _loading = true; + @override + void afterFirstLayout(BuildContext context) { + _loadLists(); + } + ///// // This essentially shows the lists. @override @@ -53,7 +61,7 @@ class _NamespacePageState extends State { }, ))).toList(), ), - onRefresh: _updateLists, + onRefresh: _loadLists, ) : Center(child: CircularProgressIndicator()), floatingActionButton: Builder( @@ -63,20 +71,14 @@ class _NamespacePageState extends State { ); } - @override - void didChangeDependencies() { - super.didChangeDependencies(); - _updateLists(); - } - Future _removeList(TaskList list) { return VikunjaGlobal.of(context) .listService .delete(list.id) - .then((_) => _updateLists()); + .then((_) => _loadLists()); } - Future _updateLists() { + Future _loadLists() { return VikunjaGlobal.of(context) .listService .getByNamespace(widget.namespace.id) @@ -107,7 +109,7 @@ class _NamespacePageState extends State { .create(widget.namespace.id, TaskList(id: null, title: name, tasks: [])) .then((_) { setState(() {}); - _updateLists(); + _loadLists(); Scaffold.of(context).showSnackBar( SnackBar( content: Text('The list was successfully created!'), diff --git a/pubspec.lock b/pubspec.lock index 8024014..f0ea579 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,6 +1,13 @@ # Generated by pub # See https://www.dartlang.org/tools/pub/glossary#lockfile packages: + after_layout: + dependency: "direct main" + description: + name: after_layout + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.7" archive: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index ee58330..f5b5eb4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,6 +15,7 @@ dependencies: cupertino_icons: ^0.1.2 flutter_secure_storage: 3.1.1 http: 0.12.0 + after_layout: ^1.0.7 dev_dependencies: flutter_test: