Created
December 26, 2022 09:18
-
-
Save thuycom205/3480026973017e9dccdfb7c6f6cfb36e to your computer and use it in GitHub Desktop.
This file contains 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 'dart:io'; | |
import 'package:flutter_screenutil/flutter_screenutil.dart'; | |
import 'package:badges/badges.dart'; | |
import 'package:firebase_messaging/firebase_messaging.dart'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; | |
import 'package:flutter_svg/flutter_svg.dart'; | |
import 'package:provider/provider.dart'; | |
import 'package:shopify_shop/core/constants/colors.dart'; | |
import 'package:shopify_shop/core/constants/text_ultis.dart'; | |
import 'package:shopify_shop/core/services/share_service.dart'; | |
import 'package:shopify_shop/core/utils/all_translations.dart'; | |
import 'package:shopify_shop/core/utils/connectivity_utils.dart'; | |
import 'package:shopify_shop/core/utils/fcm.dart'; | |
import 'package:shopify_shop/core/utils/local_notification_utils.dart'; | |
import 'package:shopify_shop/core/viewmodels/app_model.dart'; | |
import 'package:shopify_shop/core/viewmodels/home_model.dart'; | |
import 'package:shopify_shop/core/viewmodels/setting_model.dart'; | |
import 'package:shopify_shop/core/viewmodels/splash_model.dart'; | |
import 'package:shopify_shop/ui/tab/account_tab.dart'; | |
import 'package:shopify_shop/ui/tab/cart_tab.dart'; | |
import 'package:shopify_shop/ui/tab/other_root_tab.dart'; | |
import 'package:shopify_shop/ui/tab/setting_tab.dart'; | |
import 'package:shopify_shop/ui/views/base_view.dart'; | |
import 'package:shopify_shop/ui/views/home_old_view.dart'; | |
import 'package:shopify_shop/ui/views/home_view.dart'; | |
import 'package:shopify_shop/ui/views/wishlist_view.dart'; | |
import 'package:webview_flutter/webview_flutter.dart'; | |
import '../locator.dart'; | |
class RootPage extends StatefulWidget { | |
RootPage({Key key}) : super(key: key); | |
@override | |
_RootPageState createState() { | |
return _RootPageState(); | |
} | |
} | |
class _RootPageState extends State<RootPage> { | |
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance; | |
@override | |
void initState() { | |
super.initState(); | |
if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView(); | |
// For handling the received notifications | |
FirebaseMessaging.onMessage.listen((RemoteMessage message) { | |
print("Handling a o message: ${message.messageId}"); | |
// Parse the message received | |
if(message!=null){ | |
if (Platform.isAndroid) { | |
LocalNotificationUtils().flutterLocalNotificationsPlugin.show( | |
message.hashCode, | |
message.notification?.title, | |
message.notification?.body, | |
NotificationDetails( | |
android: AndroidNotificationDetails( | |
LocalNotificationUtils.channel.id, | |
LocalNotificationUtils.channel.name, | |
icon: 'ic_launcher_foreground', | |
), | |
)); | |
} else { | |
String imageUrl = message.notification?.apple?.imageUrl; | |
var iOSChannelSpecifics = IOSNotificationDetails(); | |
var androidPlatformChannelSpecifics = AndroidNotificationDetails( | |
'channel_ID', 'channel name', | |
playSound: true, | |
showProgress: true, | |
ticker: 'test ticker'); | |
var platformChannelSpecifics = NotificationDetails( | |
iOS: iOSChannelSpecifics); | |
LocalNotificationUtils().flutterLocalNotificationsPlugin | |
.show(1, | |
message.notification?.title, | |
message.notification?.body, | |
platformChannelSpecifics); | |
} | |
} | |
}); | |
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); | |
String deviceId =ShareService().getNotificationDeviceId(); | |
if(deviceId==null){ | |
FirebaseMessaging.instance.getToken().then((value) { | |
print("token firebase-------------- \n${value}"); | |
SettingModel settingModel =locator.get<SettingModel>(); | |
if(value!=null){ | |
ShareService().setNotificationDeviceId(value); | |
settingModel.addNotification(value); | |
} | |
}); | |
} | |
} | |
Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { | |
print("Handling a background message: ${message.messageId}"); | |
// Handle notification message | |
LocalNotificationUtils().flutterLocalNotificationsPlugin.show( | |
message.hashCode, | |
message.notification?.title, | |
message.notification?.body, | |
NotificationDetails( | |
android: AndroidNotificationDetails( | |
LocalNotificationUtils.channel.id, | |
LocalNotificationUtils.channel.name, | |
icon: 'ic_launcher', | |
), | |
)); | |
} | |
@override | |
void dispose() { | |
super.dispose(); | |
} | |
Map<String,Widget> _mapWidget= | |
{ | |
"Home": HomeViews(), | |
"Wishlist":WishlistView(), | |
"Cart":CartTab(), | |
"Account":AccountTab(), | |
"Setting":SettingTab(), | |
"Others":OtherRootTab() | |
}; | |
SplashModel splashModel=locator.get<SplashModel>(); | |
List<Widget> _listBody=[]; | |
@override | |
Widget build(BuildContext context) { | |
if (!splashModel.mobileData.app_state) { | |
// if (true ) { | |
return Scaffold( | |
/* body:IndexedStack( | |
index: model.selectedIndex, | |
children: _listBody, | |
),*/ | |
body: Center( | |
child: Text('The app is under construction. Please reinstall AllFetch mobile app and sign up a plan to live it!'), | |
), | |
); | |
} else { | |
ScreenUtil.init( | |
BoxConstraints( | |
maxWidth: MediaQuery.of(context).size.width, | |
maxHeight: MediaQuery.of(context).size.height), | |
designSize: Size(360, 690), | |
orientation: Orientation.portrait); | |
// ScreenUtil.init(context, | |
// designSize: Size(375, 812), allowFontScaling: true); | |
return BaseView<AppModel>( | |
onModelReady: (model){ | |
if(splashModel.mobileData.menu!=null&&splashModel.mobileData.menu.length>0){ | |
model.selectedKey= splashModel.mobileData.menu[0].menuTypeId; | |
_listBody =_buildBodyWidget(splashModel, model); | |
} | |
}, | |
builder: (context, model, child) { | |
return Scaffold( | |
/* body:IndexedStack( | |
index: model.selectedIndex, | |
children: _listBody, | |
),*/ | |
body: Center( | |
child: _mapWidget[model.selectedKey], | |
), | |
bottomNavigationBar: _buildBottomNavigationBar(splashModel,model), | |
); | |
}, | |
); | |
} | |
} | |
List<Widget> _buildBodyWidget(SplashModel splashModel, AppModel model){ | |
List<Widget> _list=[]; | |
if(splashModel.mobileData!=null&& splashModel.mobileData.menu!=null) { | |
if (splashModel.mobileData.menu.length == 0){ | |
}else{ | |
splashModel.mobileData.menu.forEach((element) { | |
_list.add(_mapWidget[element.menuTypeId]); | |
}); | |
} | |
} | |
return _list; | |
} | |
_buildAppbarWidget() { | |
return AppBar( | |
backgroundColor: appBarBackground, | |
automaticallyImplyLeading: false, | |
centerTitle: true, | |
elevation: 0, | |
title: Text( | |
allTranslations.text("Home"), | |
style: TextUtils.mediumInter | |
.copyWith(fontSize: 17, color: Colors.black), | |
), | |
); | |
} | |
void _onItemTapped(AppModel model, int index) { | |
model.selectedIndex = index; | |
if(_mapWidget.containsKey(splashModel.mobileData.menu[index].menuTypeId)){ | |
model.selectedKey= splashModel.mobileData.menu[index].menuTypeId; | |
}else{ | |
model.otherTitle = splashModel.mobileData.menu[index].title; | |
model.urlOtherApp=splashModel.mobileData.menu[index].url; | |
model.selectedKey = "Others"; | |
} | |
} | |
List<BottomNavigationBarItem> _listItemBottomNavigation(AppModel model,) { | |
return splashModel.mobileData.menu.map( | |
(e) { | |
if(e.menuTypeId=="Cart"){ | |
return BottomNavigationBarItem( | |
title: Container( | |
padding: EdgeInsets.only( | |
top: 5, | |
), | |
child: Text( | |
allTranslations.text("${e.title}"), | |
style: TextUtils.mediumInter.copyWith( | |
color: model.selectedKey == splashModel.mobileData.menu[splashModel.mobileData.menu.indexOf(e)].menuTypeId | |
? enableBottomNavItemColor | |
: disableBottomNavItemColor, | |
fontSize: 10, | |
letterSpacing: 0.4, | |
), | |
), | |
), | |
icon: Badge( | |
showBadge: true, | |
padding: EdgeInsets.all(5), | |
animationType: BadgeAnimationType.scale, | |
badgeContent: Text( | |
'${model.qty??"0"}', | |
style: TextUtils.mediumRoboto.copyWith( | |
fontSize: 9, | |
color: Colors.white, | |
letterSpacing: 0.2, | |
), | |
), | |
child: Container( | |
width: 20, | |
height: 20, | |
child: SvgPicture.asset( | |
"assets/icons/bottom_new/${e.iconUrl}.svg", | |
width: 11.53, | |
height: 12.81, | |
color: model.selectedKey == splashModel.mobileData.menu[splashModel.mobileData.menu.indexOf(e)].menuTypeId | |
? enableBottomNavItemColor | |
: disableBottomNavItemColor, | |
), | |
), | |
), | |
); | |
}else{ | |
if(_mapWidget.containsKey(e.menuTypeId)){ | |
return BottomNavigationBarItem( | |
title: Container( | |
padding: EdgeInsets.only( | |
top: 5, | |
), | |
child: Text( | |
allTranslations.text("${e.title}"), | |
style: TextUtils.mediumInter.copyWith( | |
color: model.selectedKey == splashModel.mobileData.menu[splashModel.mobileData.menu.indexOf(e)].menuTypeId | |
? enableBottomNavItemColor | |
: disableBottomNavItemColor, | |
fontSize: 10, | |
letterSpacing: 0.4, | |
), | |
), | |
), | |
icon: Container( | |
width: 20, | |
height: 20, | |
child: SvgPicture.asset( | |
"assets/icons/bottom_new/${e.iconUrl}.svg", | |
width: 12.96, | |
height: 12.96, | |
color: model.selectedKey == splashModel.mobileData.menu[splashModel.mobileData.menu.indexOf(e)].menuTypeId | |
? enableBottomNavItemColor | |
: disableBottomNavItemColor, | |
), | |
), | |
); | |
}else{ | |
return BottomNavigationBarItem( | |
title: Container( | |
padding: EdgeInsets.only( | |
top: 5, | |
), | |
child: Text( | |
allTranslations.text("${e.title}"), | |
style: TextUtils.mediumInter.copyWith( | |
color: model.selectedIndex == splashModel.mobileData.menu.indexOf(e) | |
? enableBottomNavItemColor | |
: disableBottomNavItemColor, | |
fontSize: 10, | |
letterSpacing: 0.4, | |
), | |
), | |
), | |
icon: Container( | |
width: 20, | |
height: 20, | |
child: SvgPicture.asset( | |
"assets/icons/camera.svg", | |
width: 12.96, | |
height: 12.96, | |
color: model.selectedIndex == splashModel.mobileData.menu.indexOf(e) | |
? enableBottomNavItemColor | |
: disableBottomNavItemColor, | |
), | |
), | |
); | |
} | |
} | |
} | |
).toList(); | |
} | |
void _showDialogExitApp() { | |
// flutter defined function | |
showDialog( | |
context: context, | |
builder: (BuildContext context) { | |
// return object of type Dialog | |
return AlertDialog( | |
title: new Text( | |
"Do you really want to exit?", | |
style: TextStyle( | |
color: Colors.black, | |
fontSize: 17, | |
fontWeight: FontWeight.normal, | |
), | |
), | |
actions: <Widget>[ | |
FlatButton( | |
child: new Text( | |
"Yes", | |
style: TextStyle( | |
color: Colors.black, | |
fontSize: 16, | |
fontWeight: FontWeight.w400, | |
fontFamily: "Open Sans", | |
), | |
), | |
onPressed: () { | |
pop(); | |
}, | |
), | |
FlatButton( | |
child: new Text( | |
"No", | |
style: TextStyle( | |
color: Colors.black, | |
fontSize: 16, | |
fontWeight: FontWeight.w400, | |
fontFamily: "Open Sans", | |
), | |
), | |
onPressed: () { | |
Navigator.of(context).pop(); | |
}, | |
), | |
], | |
); | |
}, | |
); | |
} | |
Future<void> pop() async { | |
await SystemChannels.platform.invokeMethod<void>('SystemNavigator.pop'); | |
} | |
_buildBottomNavigationBar(SplashModel splashModel, AppModel model) { | |
if(splashModel.mobileData!=null&& splashModel.mobileData.menu!=null){ | |
if(splashModel.mobileData.menu.length==0){ | |
return Container(); | |
}else if(splashModel.mobileData.menu.length==1){ | |
return Container( | |
height: 58, | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
Container( | |
width: 20, | |
height: 20, | |
child: SvgPicture.asset( | |
"assets/icons/bottom_new/${splashModel.mobileData.menu[0].iconUrl}.svg", | |
width: 12.96, | |
height: 12.96, | |
color: model.selectedIndex == 0 | |
? enableBottomNavItemColor | |
: disableBottomNavItemColor, | |
), | |
), | |
Container( | |
padding: EdgeInsets.only( | |
top: 5, | |
), | |
child: Text( | |
allTranslations.text("${splashModel.mobileData.menu[0].title}"), | |
style: TextUtils.mediumInter.copyWith( | |
color: model.selectedIndex == 0 | |
? enableBottomNavItemColor | |
: disableBottomNavItemColor, | |
fontSize: 10, | |
letterSpacing: 0.4, | |
), | |
), | |
) | |
], | |
), | |
); | |
}else{ | |
return BottomNavigationBar( | |
items:_listItemBottomNavigation(model,), | |
unselectedItemColor: Colors.grey, | |
unselectedLabelStyle: TextStyle(color: Colors.grey), | |
type: BottomNavigationBarType.fixed, | |
selectedItemColor: colorBottomNavigation, | |
onTap: (index) => _onItemTapped(model, index), | |
); | |
} | |
}else{ | |
return null; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment