mobdr/lib/ui/home/tab_home.dart

310 lines
12 KiB
Dart

/*
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:mobdr/config/constant.dart';
import 'package:mobdr/cubit/language/language_cubit.dart';
import 'package:mobdr/cubit/language/app_localizations.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.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/home/photo_typology.dart';
import 'package:mobdr/ui/general/product_detail/product_detail.dart';
import 'package:mobdr/ui/reusable/reusable_widget.dart';
import 'package:mobdr/ui/reusable/cache_image_network.dart';
class TabHomePage extends StatefulWidget {
@override
_TabHomePageState createState() => _TabHomePageState();
}
class _TabHomePageState extends State<TabHomePage>
with AutomaticKeepAliveClientMixin {
// initialize global function and reusable widget
final _reusableWidget = ReusableWidget();
// _listKey is used for AnimatedList
final GlobalKey<AnimatedListState> _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<VisiteModel> visiteData = [];
@override
void initState() {
super.initState();
_languageCubit = BlocProvider.of<LanguageCubit>(context);
_getLocale().then((val) {
setState(() {
defaultLang = val!;
});
});
loadData().then((_) {
setState(() {
_isLoading = false;
});
});
}
@override
void dispose() {
super.dispose();
}
Future<String?> _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 / 4);
if (_isLoading) {
return Center(child: CircularProgressIndicator());
} else if (visiteData.isEmpty) {
return Center(
child: Text('Aucune visite trouvée.'),
);
}
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: AnimatedList(
key: _listKey,
initialItemCount: visiteData.length,
physics: AlwaysScrollableScrollPhysics(),
itemBuilder: (context, index, animation) {
return _buildVisitelistCard(
visiteData[index], boxImageSize, animation, index);
},
));
}
Widget _buildVisitelistCard(
VisiteModel visiteData, boxImageSize, animation, index) {
return SizeTransition(
sizeFactor: animation,
child: Container(
margin: EdgeInsets.fromLTRB(12, 6, 12, 0),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
elevation: 2,
color: Colors.white,
child: Container(
margin: EdgeInsets.all(8),
child: Column(
children: [
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ProductDetailPage(
name: visiteData.name,
image: visiteData.image,
price: visiteData.price,
photo: visiteData.photo,
rating: visiteData.rating,
review: visiteData.review,
sale: visiteData.sale,
date: visiteData.date)));
},
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(10)),
child: buildCacheNetworkImage(
width: boxImageSize,
height: boxImageSize,
url: visiteData.image)),
SizedBox(
width: 10,
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(height: 8),
Text(
visiteData.name,
style: GlobalStyle.productName
.copyWith(fontSize: 13),
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
Container(
margin: EdgeInsets.only(top: 5),
child: Text(visiteData.date,
style: GlobalStyle.productSale),
),
Container(height: 5),
Container(
margin: EdgeInsets.only(top: 5),
child: Text(
visiteData.photo.toString() + ' Photo(s)',
style: GlobalStyle.productPrice),
),
Container(height: 5),
Container(
margin: EdgeInsets.only(top: 5),
child: Row(
children: [
Icon(Icons.store, color: SOFT_GREY, size: 20),
Text(' ' + visiteData.type_visite,
style: GlobalStyle.productName
.copyWith(fontSize: 13),
maxLines: 1,
overflow: TextOverflow.ellipsis)
],
),
),
],
),
)
],
),
),
Container(
margin: EdgeInsets.only(top: 12),
child: Row(
children: [
Expanded(
child: (visiteData.stock == 0)
? TextButton(
style: ButtonStyle(
minimumSize:
MaterialStateProperty.all(Size(0, 30)),
backgroundColor:
MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) =>
Colors.grey[300]!,
),
overlayColor: MaterialStateProperty.all(
Colors.transparent),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
)),
),
onPressed: null,
child: Text(
'Out of Stock',
style: TextStyle(
color: Colors.grey[600],
fontWeight: FontWeight.bold,
fontSize: 13),
textAlign: TextAlign.center,
))
: OutlinedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
PhotoTypologyPage()));
},
style: ButtonStyle(
minimumSize:
MaterialStateProperty.all(Size(0, 30)),
overlayColor: MaterialStateProperty.all(
Colors.transparent),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
)),
side: MaterialStateProperty.all(
BorderSide(color: SOFT_BLUE, width: 1.0),
)),
child: Text(
AppLocalizations.of(context)!
.translate('i18n_take_pictures')!,
style: TextStyle(
color: SOFT_BLUE,
fontWeight: FontWeight.bold,
fontSize: 13),
textAlign: TextAlign.center,
)),
),
],
),
)
],
),
),
),
),
);
}
// data initialization on loading.
Future<void> loadData() async {
try {
// visite initialisation
visiteData = await VisiteModel.getAllVisites();
} catch (e) {
// set errorMessage for debug
_errorMessage = 'Error loading visites : $e';
}
}
}