Files
fcs/lib/pages/main/home_page.dart

406 lines
14 KiB
Dart
Raw Normal View History

2020-09-20 05:34:49 +06:30
import 'dart:async';
import 'dart:io';
2020-10-07 02:33:06 +06:30
import 'package:fcs/data/services/services.dart';
import 'package:fcs/domain/entities/user.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/transalation.dart';
import 'package:fcs/pages/box/box_list.dart';
import 'package:fcs/pages/chat/message_detail.dart';
import 'package:fcs/pages/chat/model/message_model.dart';
import 'package:fcs/pages/customer/customer_list.dart';
import 'package:fcs/pages/customer/model/customer_model.dart';
import 'package:fcs/pages/delivery/delivery_list.dart';
import 'package:fcs/pages/discount/discount_list.dart';
import 'package:fcs/pages/faq/faq_list_page.dart';
import 'package:fcs/pages/fcs_shipment/fcs_shipment_list.dart';
import 'package:fcs/pages/invoice/invoce_list.dart';
import 'package:fcs/pages/main/model/language_model.dart';
import 'package:fcs/pages/main/model/main_model.dart';
import 'package:fcs/pages/package/package_list.dart';
import 'package:fcs/pages/rates/shipment_rates.dart';
2020-10-07 14:42:07 +06:30
import 'package:fcs/pages/shipment/shipment_list.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/staff/staff_list.dart';
import 'package:fcs/pages/widgets/task_button.dart';
import 'package:fcs/pages/widgets/badge.dart';
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
import 'package:fcs/pages/widgets/bottom_widgets.dart';
import 'package:fcs/pages/widgets/right_left_page_rout.dart';
2020-05-29 15:53:37 +06:30
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
2020-08-30 21:26:37 +06:30
import 'package:flutter_icons/flutter_icons.dart';
2020-09-20 05:34:49 +06:30
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
2020-05-29 15:53:37 +06:30
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:intl/intl.dart';
import 'package:logging/logging.dart';
import 'package:provider/provider.dart';
2020-05-29 07:45:27 +06:30
2020-10-07 02:33:06 +06:30
import '../profile/profile_page.dart';
import '../signin/signin_page.dart';
import '../widgets/banner.dart';
import '../widgets/offline_redirect.dart';
2020-05-29 07:45:27 +06:30
final msgLog = Logger('backgroundMessageHandler');
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final log = Logger('_HomePageState');
2020-05-31 15:00:11 +06:30
bool login = false;
bool customer = true;
2020-09-04 15:30:10 +06:30
List<bool> isSelected = [true, false];
2020-09-20 05:34:49 +06:30
static FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
2020-05-29 07:45:27 +06:30
@override
void initState() {
super.initState();
2020-09-20 05:34:49 +06:30
MainModel mainModel = Provider.of<MainModel>(context, listen: false);
Services.instance.messagingService.init(
(message) {
print("Message from FCM:$message");
_showNotification(message);
},
onLaunch: (m) => _showNotiContent(m),
onResume: (m) => _showNotiContent(m),
onSetupComplete: (token) {
mainModel.setMessaginToken = token;
});
_initLocalNotifications();
}
String notiUserID, notiUserName;
_showNotiContent(Map<String, dynamic> message) {
try {
Map<String, dynamic> map = Map<String, dynamic>.from(message["data"]);
notiUserID = map['user_id'];
notiUserName = map['user_name'];
_startNotiTimer();
print("Notification:$map");
} catch (e) {
print("Error:$e");
}
}
_startNotiTimer() async {
var _duration = new Duration(milliseconds: 500);
new Timer.periodic(_duration, (t) => displayNoti(t));
}
void displayNoti(Timer timer) async {
MainModel mainModel = Provider.of<MainModel>(context, listen: false);
if (mainModel.isLogin()) {
timer.cancel();
bool isCustomer = mainModel.isCustomer();
String receiverID = isCustomer ? mainModel.user.id : notiUserID;
String receiverName = isCustomer ? mainModel.user.name : notiUserName;
MessageModel messageModel =
Provider.of<MessageModel>(context, listen: false);
messageModel.initQuery(receiverID);
User user = mainModel.user;
if (!isCustomer) {
CustomerModel customerModel =
Provider.of<CustomerModel>(context, listen: false);
user = await customerModel.getUser(receiverID);
}
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MessageDetail(
messageModel: messageModel,
receiverID: receiverID,
receiverName: receiverName,
))).then((value) {
if (user.userUnseenCount > 0) {
messageModel.seenMessages(user.id, true);
}
});
if (user.userUnseenCount > 0) {
messageModel.seenMessages(user.id, true);
}
}
}
_initLocalNotifications() {
var initializationSettingsAndroid =
new AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
_flutterLocalNotificationsPlugin.initialize(initializationSettings);
}
static Future _showNotification(Map<String, dynamic> message) async {
var pushTitle;
var pushText;
var action;
if (Platform.isAndroid) {
var nodeData = message['notification'];
pushTitle = nodeData['title'];
pushText = nodeData['body'];
action = nodeData['action'];
} else {
pushTitle = message['title'];
pushText = message['body'];
action = message['action'];
}
print("AppPushs params pushTitle : $pushTitle");
print("AppPushs params pushText : $pushText");
print("AppPushs params pushAction : $action");
// @formatter:off
var platformChannelSpecificsAndroid = new AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
playSound: true,
enableVibration: true,
importance: Importance.Max,
priority: Priority.High);
// @formatter:on
var platformChannelSpecificsIos =
new IOSNotificationDetails(presentSound: true);
var platformChannelSpecifics = new NotificationDetails(
platformChannelSpecificsAndroid, platformChannelSpecificsIos);
new Future.delayed(Duration.zero, () {
_flutterLocalNotificationsPlugin.show(
0,
pushTitle,
pushText,
platformChannelSpecifics,
payload: 'No_Sound',
);
});
2020-05-29 07:45:27 +06:30
}
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
2020-09-13 21:49:39 +06:30
User user = Provider.of<MainModel>(context).user;
2020-09-16 02:29:50 +06:30
if (user == null) {
return Container();
}
2020-10-07 02:33:06 +06:30
customer = Provider.of<MainModel>(context).isCustomer();
2020-09-16 02:29:50 +06:30
2020-06-01 14:24:45 +06:30
login = Provider.of<MainModel>(context).isLogin();
2020-09-04 15:30:10 +06:30
LanguageModel languageModel = Provider.of<LanguageModel>(context);
2020-06-03 00:42:31 +06:30
2020-10-07 02:33:06 +06:30
final faqBtn = TaskButton("faq.btn",
2020-09-07 16:05:28 +06:30
icon: MaterialCommunityIcons.frequently_asked_questions,
btnCallback: () => Navigator.of(context).push(CupertinoPageRoute(
2020-09-13 21:49:39 +06:30
builder: (context) => FAQListPage(),
2020-09-07 16:05:28 +06:30
)));
2020-10-07 02:33:06 +06:30
final packagesBtn = TaskButton("package.btn.name",
2020-06-01 11:52:12 +06:30
icon: Octicons.package,
2020-09-18 04:04:21 +06:30
btnCallback: () => Navigator.of(context).push<void>(
CupertinoPageRoute(builder: (context) => PackageList())));
2020-05-29 07:45:27 +06:30
2020-10-07 02:33:06 +06:30
final boxesBtn = TaskButton("boxes.name",
2020-06-04 01:36:49 +06:30
icon: MaterialCommunityIcons.package,
btnCallback: () =>
Navigator.of(context).push(BottomUpPageRoute(BoxList())));
2020-10-07 19:53:47 +06:30
final pickUpBtn = TaskButton("shipment",
2020-06-03 00:42:31 +06:30
icon: SimpleLineIcons.direction,
2020-06-01 14:24:45 +06:30
btnCallback: () =>
2020-10-07 14:42:07 +06:30
Navigator.of(context).push(BottomUpPageRoute(ShipmentList())));
2020-05-29 15:54:26 +06:30
2020-10-07 02:33:06 +06:30
final shipmentCostBtn = TaskButton("rate",
2020-05-31 15:00:11 +06:30
icon: FontAwesomeIcons.calculator,
2020-06-01 14:24:45 +06:30
btnCallback: () =>
Navigator.of(context).push(BottomUpPageRoute(ShipmentRates())));
2020-05-29 15:54:26 +06:30
2020-10-07 18:19:12 +06:30
final fcsShipmentBtn = TaskButton("FCSshipment.title",
2020-05-31 15:00:11 +06:30
icon: Ionicons.ios_airplane,
2020-10-08 03:32:52 +06:30
btnCallback: () => Navigator.of(context).push<void>(CupertinoPageRoute(
builder: (context) => FcsShipmentList(),
)));
2020-05-29 16:14:17 +06:30
2020-09-20 05:34:49 +06:30
final notiBtnOrg =
2020-10-07 02:33:06 +06:30
TaskButton("message.btn", icon: Icons.message, btnCallback: () {
2020-09-20 05:34:49 +06:30
MessageModel messageModel =
Provider.of<MessageModel>(context, listen: false);
messageModel.initQuery(user.id);
2020-05-29 07:45:27 +06:30
Navigator.push(
context,
2020-09-20 05:34:49 +06:30
BottomUpPageRoute(MessageDetail(
messageModel: messageModel,
)),
).then((value) {
if (user.userUnseenCount > 0) {
messageModel.seenMessages(user.id, true);
}
});
if (user.userUnseenCount > 0) {
messageModel.seenMessages(user.id, true);
}
2020-05-29 07:45:27 +06:30
});
2020-09-20 05:34:49 +06:30
final notiBtn = badgeCounter(notiBtnOrg, user.userUnseenCount);
2020-05-29 07:45:27 +06:30
2020-10-07 02:33:06 +06:30
final staffBtn = TaskButton(
2020-09-18 04:04:21 +06:30
"staff.title",
icon: MaterialCommunityIcons.worker,
btnCallback: () => Navigator.of(context).push<void>(CupertinoPageRoute(
builder: (context) => StaffList(),
)),
);
2020-05-29 16:14:17 +06:30
2020-10-07 02:33:06 +06:30
final customersBtn = TaskButton("customers.btn",
2020-06-02 00:00:05 +06:30
icon: Feather.users,
2020-09-18 04:04:21 +06:30
btnCallback: () => Navigator.of(context).push<void>(CupertinoPageRoute(
builder: (context) => CustomerList(),
)));
2020-06-02 00:00:05 +06:30
2020-10-07 02:33:06 +06:30
final invoicesBtn = TaskButton("invoices.btn",
2020-06-02 00:00:05 +06:30
icon: FontAwesomeIcons.fileInvoice,
btnCallback: () =>
2020-06-02 14:56:51 +06:30
Navigator.of(context).push(BottomUpPageRoute(InvoiceList())));
2020-05-29 07:45:27 +06:30
2020-10-07 02:33:06 +06:30
final discountBtn = TaskButton("discount.btn",
icon: Entypo.price_ribbon,
2020-06-26 16:04:40 +06:30
btnCallback: () =>
Navigator.of(context).push(BottomUpPageRoute(DiscountList())));
2020-10-07 02:33:06 +06:30
final deliveryBtn = TaskButton("delivery.title",
2020-06-26 16:17:40 +06:30
icon: MaterialCommunityIcons.truck_fast,
btnCallback: () =>
Navigator.of(context).push(BottomUpPageRoute(DeliverList())));
2020-06-02 14:52:31 +06:30
2020-05-31 15:00:11 +06:30
List<Widget> widgets = [];
2020-09-07 16:05:28 +06:30
widgets.add(faqBtn);
2020-09-13 21:49:39 +06:30
if (user != null) {
2020-10-07 02:33:06 +06:30
true ? widgets.add(pickUpBtn) : "";
!customer ? widgets.add(fcsShipmentBtn) : "";
2020-09-20 05:34:49 +06:30
customer ? widgets.add(notiBtn) : "";
2020-09-13 21:49:39 +06:30
user.hasStaffs() ? widgets.add(staffBtn) : "";
2020-10-07 02:33:06 +06:30
widgets.add(shipmentCostBtn);
2020-09-15 07:13:41 +06:30
user.hasPackages() ? widgets.add(packagesBtn) : "";
2020-10-07 02:33:06 +06:30
true ? widgets.add(boxesBtn) : "";
true ? widgets.add(deliveryBtn) : "";
2020-09-13 21:49:39 +06:30
user.hasCustomers() ? widgets.add(customersBtn) : "";
2020-10-07 02:33:06 +06:30
true ? widgets.add(invoicesBtn) : "";
true ? widgets.add(discountBtn) : "";
2020-09-13 21:49:39 +06:30
}
2020-05-29 07:45:27 +06:30
return OfflineRedirect(
child: FlavorBanner(
child: Scaffold(
appBar: AppBar(
2020-05-29 15:53:37 +06:30
elevation: 0,
2020-05-29 07:45:27 +06:30
backgroundColor: primaryColor,
title: ClipRRect(
2020-05-29 15:53:37 +06:30
child: Image.asset("assets/logo.jpg", height: 40),
2020-05-31 16:14:56 +06:30
borderRadius: new BorderRadius.circular(30.0),
2020-05-29 07:45:27 +06:30
),
2020-05-31 15:00:11 +06:30
actions: login
? <Widget>[
2020-06-02 00:00:05 +06:30
ToggleButtons(
children: <Widget>[
Image.asset(
2020-06-24 16:06:40 +06:30
'icons/flags/png/us.png',
2020-06-02 00:00:05 +06:30
package: 'country_icons',
fit: BoxFit.fitWidth,
width: 25,
),
Image.asset(
'icons/flags/png/mm.png',
package: 'country_icons',
fit: BoxFit.fitWidth,
width: 25,
)
],
onPressed: _langChange,
2020-09-04 15:30:10 +06:30
isSelected: languageModel.currentState,
2020-09-12 03:34:52 +06:30
selectedBorderColor: Colors.white24,
2020-05-31 15:00:11 +06:30
),
IconButton(
onPressed: () {
2020-08-26 16:01:13 +06:30
Navigator.of(context)
.push(RightLeftPageRoute(Profile()));
2020-05-31 15:00:11 +06:30
},
iconSize: 30,
2020-06-24 16:06:40 +06:30
icon: Icon(Icons.account_circle),
2020-05-31 15:00:11 +06:30
),
]
2020-05-31 16:14:56 +06:30
: <Widget>[
2020-06-02 00:00:05 +06:30
ToggleButtons(
children: <Widget>[
Image.asset(
2020-06-24 16:06:40 +06:30
'icons/flags/png/us.png',
2020-06-02 00:00:05 +06:30
package: 'country_icons',
fit: BoxFit.fitWidth,
width: 25,
),
Image.asset(
'icons/flags/png/mm.png',
package: 'country_icons',
fit: BoxFit.fitWidth,
width: 25,
)
],
onPressed: _langChange,
2020-09-04 15:30:10 +06:30
isSelected: languageModel.currentState,
2020-06-02 00:00:05 +06:30
),
2020-06-01 14:24:45 +06:30
FlatButton(
2020-05-31 15:00:11 +06:30
onPressed: () {
2020-06-02 14:56:51 +06:30
Navigator.of(context)
.push(BottomUpPageRoute(SigninPage()));
2020-05-31 15:00:11 +06:30
},
2020-06-01 14:24:45 +06:30
child: Text(
2020-06-02 00:00:05 +06:30
"Sign In",
2020-06-01 14:24:45 +06:30
style: siginButtonStyle,
),
2020-06-02 14:52:31 +06:30
),
2020-05-31 15:00:11 +06:30
]),
2020-05-29 15:53:37 +06:30
body: Container(
decoration: BoxDecoration(
2020-09-15 07:13:41 +06:30
gradient: LinearGradient(
2020-05-31 15:00:11 +06:30
begin: Alignment.topCenter,
2020-09-15 07:13:41 +06:30
end: Alignment.bottomCenter,
2020-05-31 15:00:11 +06:30
colors: [
Color(0xd0272262),
Color(0xfa272262),
2020-09-15 07:13:41 +06:30
],
2020-05-31 15:00:11 +06:30
),
2020-05-29 07:45:27 +06:30
),
2020-05-31 15:00:11 +06:30
child: Column(
2020-09-18 04:04:21 +06:30
crossAxisAlignment: CrossAxisAlignment.start,
2020-05-31 15:00:11 +06:30
children: <Widget>[
Expanded(
2020-09-04 15:30:10 +06:30
child: ListView(children: [
Wrap(
2020-10-07 02:33:06 +06:30
alignment: WrapAlignment.center,
2020-09-04 15:30:10 +06:30
children: widgets,
),
]),
2020-05-31 15:00:11 +06:30
),
2020-09-07 16:05:28 +06:30
BottomWidgets(),
2020-05-31 15:00:11 +06:30
],
2020-06-01 11:52:12 +06:30
))),
2020-05-29 07:45:27 +06:30
),
);
}
2020-06-02 00:00:05 +06:30
_langChange(index) {
2020-09-07 16:05:28 +06:30
var languageModel = Provider.of<LanguageModel>(context, listen: false);
2020-06-02 14:56:51 +06:30
languageModel.saveLanguage(Translation().supportedLanguages[index]);
2020-06-02 00:00:05 +06:30
setState(() {
isSelected.asMap().forEach((i, e) {
isSelected[i] = false;
});
isSelected[index] = !isSelected[index];
});
}
2020-10-07 02:33:06 +06:30
// Widget _buildBtn(String title, {IconData icon, BtnCallback btnCallback}) {
// return TaskButton(titleKey: title, icon: icon, btnCallback: btnCallback);
// }
2020-05-29 07:45:27 +06:30
}