From 3d0c8d3724bdeaaa3ab95af6d75797bdfda2df29 Mon Sep 17 00:00:00 2001 From: Crondung <1922635@stud.hs-mannheim.de> Date: Sun, 5 Mar 2023 21:37:27 +0100 Subject: [PATCH] main page stateless, extended pageprovider --- lib/pages/main_page.dart | 48 ++++++++++---------------------- lib/providers/page_provider.dart | 12 ++++++++ lib/services/pages_service.dart | 2 +- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index 026d707..ef396ef 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -8,41 +8,16 @@ import 'package:smoke_cess_app/providers/settings_provider.dart'; import '../widgets/todo_icon.dart'; -class MyHomePage extends StatefulWidget { +class MyHomePage extends StatelessWidget { const MyHomePage({super.key}); - @override - MyHomePageState createState() => MyHomePageState(); -} - -class MyHomePageState extends State { - int _selectedIndex = 4; - bool _isConfigured = false; - - void _onItemTapped(int index) { - PageProvider pageProvider = context.read(); - setState(() { - if (_isConfigured) { - pageProvider.showForm = false; - _selectedIndex = index; - return; - } - AwesomeDialog( - context: context, - dialogType: DialogType.info, - title: 'Fehlende Konfiguration', - desc: 'Bitte QR Code Scannen!', - ).show(); - }); - } - @override Widget build(BuildContext context) { - var settingsProvider = context.watch(); - var tasksProvider = context.watch(); + SettingsProvider settingsProvider = context.watch(); + TasksProvider tasksProvider = context.watch(); PageProvider pageProvider = context.watch(); + bool isConfigured = settingsProvider.initialized; - _isConfigured = settingsProvider.initialized; return Scaffold( appBar: AppBar( title: Row( @@ -59,13 +34,20 @@ class MyHomePageState extends State { ], ), Text( - '${pages.values.elementAt(_selectedIndex)['title']} ${_isConfigured ? "Gruppe ${settingsProvider.settings?.group}" : ""}') + '${pageProvider.currentPageData['title']} ${isConfigured ? "Gruppe ${settingsProvider.settings?.group}" : ""}') ], )), - body: pages.values.elementAt(_selectedIndex)['page'], + body: pageProvider.currentPageData['page'], bottomNavigationBar: NavigationBar( - onDestinationSelected: _onItemTapped, - selectedIndex: _selectedIndex, + onDestinationSelected: isConfigured + ? pageProvider.currentPage + : (value) => AwesomeDialog( + context: context, + dialogType: DialogType.info, + title: 'Fehlende Konfiguration', + desc: 'Bitte QR Code Scannen!', + ).show(), + selectedIndex: pageProvider.currentPageIndex, destinations: pages.keys.map((key) { return NavigationDestination( icon: tasksProvider.tasks[key] ?? false diff --git a/lib/providers/page_provider.dart b/lib/providers/page_provider.dart index a17ff26..ce1741a 100644 --- a/lib/providers/page_provider.dart +++ b/lib/providers/page_provider.dart @@ -1,10 +1,22 @@ import 'package:flutter/material.dart'; +import 'package:smoke_cess_app/services/pages_service.dart'; class PageProvider extends ChangeNotifier { bool showForm = false; + Pages _currentPage = Pages.settings; void swap() { showForm = !showForm; notifyListeners(); } + + Map get currentPageData => pages[_currentPage]!; + + int get currentPageIndex => _currentPage.index; + + void currentPage(int index) { + showForm = false; + _currentPage = Pages.values[index]; + notifyListeners(); + } } diff --git a/lib/services/pages_service.dart b/lib/services/pages_service.dart index 0c19383..9ec00d6 100644 --- a/lib/services/pages_service.dart +++ b/lib/services/pages_service.dart @@ -8,8 +8,8 @@ import '../pages/sleep_page.dart'; enum Pages { mood, sleep, - relapse, timer, + relapse, settings, }