Last active
October 19, 2020 18:08
-
-
Save paramadharmika/a828db9bc7225258ddb201cafda97216 to your computer and use it in GitHub Desktop.
home.widget refactored
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:bellybalance/components/diary/diary-page.widget.dart'; | |
import 'package:bellybalance/components/home/dashboard.widget.dart'; | |
import 'package:bellybalance/components/home/home.viewModel.dart'; | |
import 'package:bellybalance/components/home/news.viewModel.dart'; | |
import 'package:bellybalance/components/home/news.widget.dart'; | |
import 'package:bellybalance/components/settings/settings.widget.dart'; | |
import 'package:bellybalance/core/model/news/news.dart'; | |
import 'package:bellybalance/core/model/profile/user.profile.dart'; | |
import 'package:bellybalance/core/service/scanner.service.dart'; | |
import 'package:bellybalance/shared/components/message-box.widget.dart'; | |
import 'package:bellybalance/shared/localization/app-translations.dart'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_localizations/flutter_localizations.dart'; | |
import 'package:provider/provider.dart'; | |
import '../../main-app.dart'; | |
class HomePage extends StatefulWidget { | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> | |
with AutomaticKeepAliveClientMixin<HomePage> { | |
final _navigatorKeys = GlobalKey<NavigatorState>(); | |
AppTranslationsDelegate _newLocaleDelegate; | |
ScannerService _scannerService; | |
HomeViewModel _homeViewModel; | |
NewsViewModel _newsViewModel; | |
UserProfile _user; | |
bool _hasScannerAccess; | |
bool _hasDiaryAccess; | |
// Dummy Data just use for testing | |
final dummyNews = News(items: [ | |
Items( | |
id: 1, | |
active: false, | |
body: "Hi, now we have new realease version", | |
index: 0, | |
title: "New Application Version"), | |
Items( | |
id: 1, | |
active: false, | |
body: | |
"Scannern har utökats till att även hantera QR-koder. Ingen uppdatering av appen krävs.", | |
index: 0, | |
title: "Nu med VR-stöd"), | |
Items( | |
id: 2, | |
active: false, | |
body: "Lorem Ipsum", | |
index: 1, | |
title: "Lorem Ipsum") | |
]); | |
get allPageRoutes => { | |
"/": () => MaterialPageRoute( | |
builder: (context) => DashboardPage( | |
onChanged: _onTabSelected, | |
), | |
), | |
"/scanner": () => MaterialPageRoute( | |
builder: (context) => Container(), | |
), | |
"/diary": () => MaterialPageRoute( | |
builder: (context) => DiaryPage(), | |
), | |
"/settings": () => MaterialPageRoute( | |
builder: (context) => SettingsPage(), | |
), | |
}; | |
get trialScannerActiveRoutes => { | |
"/": () => MaterialPageRoute( | |
builder: (context) => DashboardPage( | |
onChanged: _onTabSelected, | |
), | |
), | |
"/scanner": () => MaterialPageRoute( | |
builder: (context) => Container(), | |
), | |
"/settings": () => MaterialPageRoute( | |
builder: (context) => SettingsPage(), | |
), | |
}; | |
get trialDiaryActiveRoutes => { | |
"/": () => MaterialPageRoute( | |
builder: (context) => DashboardPage( | |
onChanged: _onTabSelected, | |
), | |
), | |
"/diary": () => MaterialPageRoute( | |
builder: (context) => DiaryPage(), | |
), | |
"/settings": () => MaterialPageRoute( | |
builder: (context) => SettingsPage(), | |
), | |
}; | |
get trialDefaultRoutes => { | |
"/": () => MaterialPageRoute( | |
builder: (context) => DashboardPage( | |
onChanged: _onTabSelected, | |
), | |
), | |
"/settings": () => MaterialPageRoute( | |
builder: (context) => SettingsPage(), | |
), | |
}; | |
@override | |
void initState() { | |
_newLocaleDelegate = AppTranslationsDelegate(newLocale: Locale("sv")); | |
_homeViewModel = HomeViewModel(); | |
_newsViewModel = Provider.of<NewsViewModel>(context, listen: false); | |
_scannerService = Provider.of<ScannerService>(context, listen: false); | |
_showNewsPopUp(); | |
super.initState(); | |
} | |
_showNewsPopUp() async { | |
await _newsViewModel.fetchNews(context); | |
final news = _newsViewModel.news; | |
if (news.items.isNotEmpty) { | |
showDialog( | |
context: context, | |
barrierDismissible: false, | |
builder: (_context) => Center( | |
child: MessageBox( | |
image: AssetImage('assets/images/icon/belly-dietist.png'), | |
child: NewsWidget(), | |
onTap: () { | |
Navigator.of(_context, rootNavigator: true).pop(); | |
_closeNews(); | |
}, | |
), | |
), | |
); | |
} | |
} | |
_closeNews() { | |
final news = _newsViewModel.news; | |
final newsIdList = []; | |
news.items.forEach((item) { | |
if (item.active) { | |
newsIdList.add(item.id); | |
} | |
}); | |
print('getNewsIDList: $newsIdList'); | |
if (newsIdList.length > 0) { | |
_newsViewModel.markNewsAsDimissed(context, newsIdList); | |
} | |
} | |
@override | |
void dispose() { | |
super.dispose(); | |
} | |
Future<bool> _onWillPop() async { | |
final isFirstRouteInCurrentTab = | |
!await _navigatorKeys.currentState.maybePop(); | |
if (isFirstRouteInCurrentTab) { | |
// if not on the 'main' tab | |
if (_homeViewModel.currentIndex != 0) { | |
// select 'main' tab | |
_onTabSelected(0); | |
// back button handled by app | |
return false; | |
} | |
} | |
// let system handle back button if we're on the first route | |
return isFirstRouteInCurrentTab; | |
} | |
@override | |
Widget build(BuildContext context) { | |
super.build(context); | |
final l10n = AppTranslations.of(context); | |
_user = Provider.of<UserProfile>(context); | |
_hasScannerAccess = _user != null ? _user.hasScannerAccess : false; | |
_hasDiaryAccess = _user != null ? _user.hasDiaryAccess : false; | |
return WillPopScope( | |
onWillPop: _onWillPop, | |
child: Scaffold( | |
body: _buildBody(), | |
bottomNavigationBar: BottomNavigationBar( | |
currentIndex: _homeViewModel.currentIndex, | |
showUnselectedLabels: false, | |
type: BottomNavigationBarType.fixed, | |
onTap: _onTabSelected, | |
items: [ | |
BottomNavigationBarItem( | |
title: Text(l10n.text("home_tab"), style: textItemStyle()), | |
icon: Image.asset('assets/images/icon/home-inactive.png'), | |
activeIcon: Image.asset('assets/images/icon/home-active.png'), | |
), | |
if (_hasScannerAccess) | |
BottomNavigationBarItem( | |
title: Text(l10n.text("scanner_tab"), style: textItemStyle()), | |
icon: Image.asset('assets/images/icon/scanner-inactive.png'), | |
activeIcon: | |
Image.asset('assets/images/icon/scanner-active.png'), | |
), | |
if (_hasDiaryAccess) | |
BottomNavigationBarItem( | |
title: Text(l10n.text("diary_tab"), style: textItemStyle()), | |
icon: Image.asset('assets/images/icon/diary-inactive.png'), | |
activeIcon: Image.asset('assets/images/icon/diary-active.png'), | |
), | |
BottomNavigationBarItem( | |
title: Text(l10n.text("user_tab"), style: textItemStyle()), | |
icon: Image.asset('assets/images/icon/user-inactive.png'), | |
activeIcon: Image.asset('assets/images/icon/user-active.png'), | |
), | |
], | |
), | |
), | |
); | |
} | |
Widget _buildBody() => MaterialApp( | |
theme: MainApp().buildThemeData(), | |
debugShowCheckedModeBanner: false, | |
localizationsDelegates: [ | |
_newLocaleDelegate, | |
GlobalMaterialLocalizations.delegate, | |
GlobalWidgetsLocalizations.delegate, | |
], | |
navigatorKey: _navigatorKeys, | |
onGenerateRoute: (route) => allPageRoutes[route.name]()); | |
TextStyle textItemStyle() => | |
TextStyle(color: Colors.black, letterSpacing: 0.39, fontSize: 12); | |
_onTabSelected(int index) async { | |
final isFirstRouteInCurrentTab = | |
!await _navigatorKeys.currentState.maybePop(); | |
// _has prefix already contains full membership property | |
Map routes; | |
if (_hasScannerAccess && _hasDiaryAccess) { | |
routes = allPageRoutes; | |
} else if (_hasScannerAccess) { | |
routes = trialScannerActiveRoutes; | |
} else if (_hasDiaryAccess) { | |
routes = trialDiaryActiveRoutes; | |
} else { | |
routes = trialDefaultRoutes; | |
} | |
final routesKeys = routes.keys.toList(); | |
if (_homeViewModel.currentIndex == index && isFirstRouteInCurrentTab) { | |
return; | |
} | |
if (routesKeys[index].toLowerCase() == '/scanner') { | |
if (_hasScannerAccess) { | |
_scannerService.scan(); | |
} | |
} else { | |
setState(() { | |
_homeViewModel.currentIndex = index; | |
}); | |
final routeName = routesKeys[index].toString(); | |
_navigatorKeys | |
.currentState | |
.pushReplacementNamed(routeName); | |
} | |
} | |
@override | |
bool get wantKeepAlive => true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment