/* This is home page we used AutomaticKeepAliveClientMixin to keep the state when moving from 1 navbar to another navbar, so the page is not refresh overtime */ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:mobdr/config/constant.dart'; import 'package:mobdr/cubit/language/language_cubit.dart'; import 'package:mobdr/cubit/language/app_localizations.dart'; import 'package:mobdr/service/shared_prefs.dart'; import 'package:mobdr/config/global_style.dart'; import 'package:mobdr/model/visite_model.dart'; import 'package:mobdr/ui/general/chat_us.dart'; import 'package:mobdr/ui/general/notification.dart'; import 'package:mobdr/ui/reusable/reusable_widget.dart'; class TabHomePage extends StatefulWidget { @override _TabHomePageState createState() => _TabHomePageState(); } class _TabHomePageState extends State with AutomaticKeepAliveClientMixin { // initialize global function and reusable widget final _reusableWidget = ReusableWidget(); // _listKey is used for AnimatedList final GlobalKey _listKey = GlobalKey(); // keep the state to do not refresh when switch navbar @override bool get wantKeepAlive => true; String defaultLang = 'en'; late LanguageCubit _languageCubit; bool _isLoading = true; String _errorMessage = ''; late List todayVisitsData = []; late List previousVisitsData = []; @override void initState() { super.initState(); _languageCubit = BlocProvider.of(context); _getLocale().then((val) { setState(() { defaultLang = val!; }); }); loadData().then((_) { setState(() { _isLoading = false; }); }); } @override void dispose() { super.dispose(); } Future _getLocale() async { final SharedPreferences _pref = await SharedPreferences.getInstance(); String? lCode = _pref.getString('lCode'); return lCode; } void changeLocale(Locale locale) async { final SharedPreferences _pref = await SharedPreferences.getInstance(); await _pref.setString('lCode', locale.languageCode); await _pref.setString('cCode', locale.countryCode!); _languageCubit.changeLanguage(locale); defaultLang = locale.languageCode; } @override Widget build(BuildContext context) { // if we used AutomaticKeepAliveClientMixin, we must call super.build(context); super.build(context); final double boxImageSize = (MediaQuery.of(context).size.width / 3); if (_isLoading) { return Center(child: CircularProgressIndicator()); } return Scaffold( appBar: AppBar( automaticallyImplyLeading: false, elevation: GlobalStyle.appBarElevation, title: Text( AppLocalizations.of(context)!.translate('i18n_hello')! + ', ${SharedPrefs().prenom}', style: GlobalStyle.appBarTitle, ), backgroundColor: GlobalStyle.appBarBackgroundColor, systemOverlayStyle: GlobalStyle.appBarSystemOverlayStyle, actions: [ GestureDetector( onTap: () { Navigator.push(context, MaterialPageRoute(builder: (context) => ChatUsPage())); }, child: Icon(Icons.email, color: BLACK_GREY)), IconButton( icon: _reusableWidget.customNotifIcon( count: 8, notifColor: BLACK_GREY), onPressed: () { Navigator.push( context, MaterialPageRoute( builder: (context) => NotificationPage())); }), ], ), body: ListView( physics: AlwaysScrollableScrollPhysics(), children: [ _buildTodayVisits(boxImageSize), _builPreviousVisits(boxImageSize), ], ), ); } Widget _buildTodayVisits(boxImageSize) { return Column( children: [ Container( padding: EdgeInsets.fromLTRB(16, 16, 16, 0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('Today Visits', style: GlobalStyle.horizontalTitle), GestureDetector( onTap: () { //Navigator.push(context, MaterialPageRoute(builder: (context) => RestaurantListPage(title: 'Food Arround You'))); }, child: Text('View All', style: GlobalStyle.viewAll, textAlign: TextAlign.end), ) ], ), ), Container( margin: EdgeInsets.only(top: 8), height: boxImageSize * GlobalStyle.cardHeightMultiplication, child: ListView.builder( padding: EdgeInsets.symmetric(horizontal: 12), scrollDirection: Axis.horizontal, itemCount: todayVisitsData.length, itemBuilder: (BuildContext context, int index) { return _reusableWidget.buildHorizontalVisitListCard( context, todayVisitsData[index], ); }, )), ], ); } Widget _builPreviousVisits(boxImageSize) { return Column( children: [ Container( padding: EdgeInsets.fromLTRB(16, 16, 16, 0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('Previous Visits', style: GlobalStyle.horizontalTitle), GestureDetector( onTap: () { //Navigator.push(context, MaterialPageRoute(builder: (context) => RestaurantListPage(title: 'Food Arround You'))); }, child: Text('View All', style: GlobalStyle.viewAll, textAlign: TextAlign.end), ) ], ), ), Container( margin: EdgeInsets.only(top: 8), height: boxImageSize * GlobalStyle.cardHeightMultiplication, child: ListView.builder( padding: EdgeInsets.symmetric(horizontal: 12), scrollDirection: Axis.horizontal, itemCount: previousVisitsData.length, itemBuilder: (BuildContext context, int index) { return _reusableWidget.buildHorizontalVisitListCard( context, previousVisitsData[index]); }, )), ], ); } /// Initializes data when the page loads. Future loadData() async { try { // visite model initialisation todayVisitsData = await VisiteModel.getAllVisites(); previousVisitsData = await VisiteModel.getAllVisites(); } catch (e) { // set errorMessage for debug _errorMessage = 'Error loading visites : $e'; } } }