You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

385 lines
15KB

  1. import 'package:dio/dio.dart';
  2. import 'package:farm_tpf/custom_model/NotificationObjectDTO.dart';
  3. import 'package:farm_tpf/data/repository/repository.dart';
  4. import 'package:farm_tpf/data/repository/user_repository.dart';
  5. import 'package:farm_tpf/presentation/custom_widgets/widget_utils.dart';
  6. import 'package:farm_tpf/presentation/screens/account/sc_account.dart';
  7. import 'package:farm_tpf/presentation/screens/codes/code_detail_page.dart';
  8. import 'package:farm_tpf/presentation/screens/codes/code_page.dart';
  9. import 'package:farm_tpf/presentation/screens/control_device/sc_control_device.dart';
  10. import 'package:farm_tpf/presentation/screens/notification/sc_notification.dart';
  11. import 'package:farm_tpf/presentation/screens/notification/update_count_noti_bloc.dart';
  12. import 'package:farm_tpf/presentation/screens/plot/sc_plot.dart';
  13. import 'package:farm_tpf/presentation/screens/plot_detail/sc_plot_detail.dart';
  14. import 'package:farm_tpf/utils/NotificationsBloc.dart';
  15. import 'package:farm_tpf/utils/const_color.dart';
  16. import 'package:farm_tpf/utils/const_common.dart';
  17. import 'package:farm_tpf/utils/const_icons.dart';
  18. import 'package:farm_tpf/utils/pref.dart';
  19. import 'package:flutter/material.dart';
  20. import 'package:flutter_svg/flutter_svg.dart';
  21. import 'package:get/get.dart';
  22. import '../qr_scan/qr_scan_page.dart';
  23. class TabbarScreen extends StatefulWidget {
  24. static Route route() {
  25. return MaterialPageRoute<void>(builder: (_) => TabbarScreen());
  26. }
  27. @override
  28. _TabbarScreenState createState() => _TabbarScreenState();
  29. }
  30. class _TabbarScreenState extends State<TabbarScreen> {
  31. Stream<LocalNotification>? _notificationsStream;
  32. UserRepository _userRepository = UserRepository();
  33. // final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  34. var pref = LocalPref();
  35. String pushkey = "";
  36. String currentFullName = "";
  37. var token;
  38. var client;
  39. final changeTabbar = Get.put(TabbarSelected());
  40. List<TabbarItem> itemsTabbar = [
  41. TabbarItem(icon: AppIcons.icPlot, title: 'Lô trồng', index: TabBarIndex.plot),
  42. // TabbarItem(icon: AppIcons.icDevice, title: 'Thiết bị', index: TabBarIndex.device),
  43. TabbarItem(icon: AppIcons.icQrManage, title: 'Quản lý QR', index: TabBarIndex.qrManage),
  44. TabbarItem(icon: AppIcons.icQr, title: 'Quét QR', index: TabBarIndex.qr),
  45. TabbarItem(icon: AppIcons.icNotification, title: 'Thông báo', index: TabBarIndex.notification),
  46. TabbarItem(icon: AppIcons.icPerson, title: 'Cá nhân', index: TabBarIndex.account)
  47. ];
  48. Future<Null> getSharedPrefs() async {
  49. token = await pref.getString(DATA_CONST.TOKEN_KEY);
  50. currentFullName = await pref.getString(DATA_CONST.CURRENT_FULL_NAME);
  51. var options = BaseOptions(baseUrl: ConstCommon.baseUrl);
  52. options.headers["Authorization"] = "Bearer $token";
  53. client = Dio(options);
  54. // if (Platform.isIOS) {
  55. // _firebaseMessaging.requestNotificationPermissions(IosNotificationSettings());
  56. // }
  57. // _firebaseMessaging.configure(
  58. // onMessage: (Map<String, dynamic> message) async {
  59. // print("onMessage--tabbar-: $message");
  60. // try {
  61. // final String type = message['tbCropType'];
  62. // final String contents = message['contents'];
  63. // final String tbCropId = message['tbCropId'];
  64. // updateCountNotiBloc.getNotifications((data) {}, (err) {});
  65. // final notification = LocalNotification(type, contents, tbCropId);
  66. // NotificationsBloc.instance.newNotification(notification);
  67. // if (contents == "ENV_UPDATE") {
  68. // if (Get.isSnackbarOpen) Get.back();
  69. // Get.snackbar(null, 'Thông số môi trường được cập nhật');
  70. // } else if (contents == "PIC_UPDATE") {
  71. // if (Get.isSnackbarOpen) Get.back();
  72. // Get.snackbar(null, 'Người phụ trách được cập nhật');
  73. // } else {
  74. // //Go home
  75. // }
  76. // } catch (e) {
  77. // print('error');
  78. // print(e);
  79. // }
  80. // },
  81. // onBackgroundMessage: Platform.isIOS ? null : myBackgroundMessageHandler,
  82. // onLaunch: (Map<String, dynamic> message) async {
  83. // print("onLaunch: $message");
  84. // Future.delayed(Duration(milliseconds: 500), () {
  85. // updateCountNotiBloc.getNotifications((data) {}, (err) {});
  86. // _notificationNavigateOnFCM(message);
  87. // });
  88. // },
  89. // onResume: (Map<String, dynamic> message) async {
  90. // print("onResume: $message");
  91. // updateCountNotiBloc.getNotifications((data) {}, (err) {});
  92. // _notificationNavigateOnFCM(message);
  93. // },
  94. // );
  95. // _firebaseMessaging.requestNotificationPermissions(const IosNotificationSettings(sound: true, badge: true, alert: true, provisional: true));
  96. // _firebaseMessaging.onIosSettingsRegistered.listen((IosNotificationSettings settings) {
  97. // print("Settings registered: $settings");
  98. // });
  99. // if (pushkey?.isEmpty ?? true) {
  100. // _firebaseMessaging.getToken().then((String token) {
  101. // assert(token != null);
  102. // print("Push Messaging token: $token");
  103. // _userRepository.updateFcmToken(token).then((value) {
  104. // print("send push key successful");
  105. // pref.saveString(DATA_CONST.PUSH_KEY, token);
  106. // });
  107. // // client.put("");
  108. // });
  109. // } else {
  110. // print("Don't need get push key");
  111. // }
  112. var pushKey = await pref.getString(PrefKey.push_key);
  113. _userRepository.updateFcmToken(pushKey).then((value) {
  114. print("send push key successful");
  115. });
  116. if (currentFullName?.isEmpty ?? true) {
  117. try {
  118. var currentUser = await _userRepository.getUser();
  119. pref.saveString(DATA_CONST.CURRENT_FULL_NAME, currentUser.fullName ?? '');
  120. print("fullname: ${currentUser.fullName}");
  121. } catch (e) {
  122. print("error: ${e.toString()}");
  123. }
  124. }
  125. }
  126. @override
  127. void initState() {
  128. super.initState();
  129. getSharedPrefs();
  130. changeTabbar.initValue();
  131. updateCountNotiBloc.getNotifications((data) {}, (err) {});
  132. _notificationsStream = NotificationsBloc.instance.notificationsStream;
  133. _notificationsStream?.listen((notification) {
  134. updateCountNotiBloc.getNotifications((data) {}, (err) {});
  135. print('Notification: $notification');
  136. });
  137. }
  138. _notificationNavigateOnFCM(Map<String, dynamic> message) {
  139. try {
  140. final String type = message['tbCropType'];
  141. final String contents = message['contents'];
  142. final String tbCropId = message['tbCropId'];
  143. if (contents == "ENV_UPDATE") {
  144. Get.to(PlotDetailScreen(
  145. cropType: int.parse(type),
  146. cropId: int.parse(tbCropId),
  147. initialIndex: 0,
  148. ));
  149. } else if (contents == "PIC_UPDATE") {
  150. Get.to(PlotDetailScreen(
  151. cropType: int.parse(type),
  152. cropId: int.parse(tbCropId),
  153. initialIndex: 1,
  154. ));
  155. } else {
  156. //Go home
  157. }
  158. } catch (e) {
  159. //Go home
  160. }
  161. Get.to(PlotDetailScreen(
  162. cropType: 0,
  163. cropId: 1,
  164. initialIndex: 0,
  165. ));
  166. }
  167. Widget textCountNoti() {
  168. return StreamBuilder(
  169. stream: updateCountNotiBloc.actions,
  170. builder: (context, AsyncSnapshot<dynamic> snapshot) {
  171. if (snapshot.hasData) {
  172. var noti = snapshot.data as NotificationObjectDTO;
  173. var unreadNoti = (noti.numberUnreadTotal ?? 0) > 99 ? '99+' : '${noti.numberUnreadTotal}';
  174. return Text(
  175. '$unreadNoti',
  176. softWrap: true,
  177. style: TextStyle(color: Colors.white, fontSize: 10),
  178. );
  179. } else {
  180. return Text(
  181. 'O',
  182. softWrap: true,
  183. style: TextStyle(color: Colors.white, fontSize: 10),
  184. );
  185. }
  186. },
  187. );
  188. }
  189. @override
  190. Widget build(BuildContext context) {
  191. return Container(
  192. color: Colors.white,
  193. child: SafeArea(
  194. top: false,
  195. bottom: true,
  196. child: Scaffold(
  197. body: GetBuilder<TabbarSelected>(
  198. init: changeTabbar,
  199. builder: (tabbarSelected) {
  200. switch (tabbarSelected.index) {
  201. case TabBarIndex.plot:
  202. return PlotListScreen();
  203. break;
  204. // case TabBarIndex.device:
  205. // return ControlDeviceScreen();
  206. // break;
  207. case TabBarIndex.qrManage:
  208. return CodePage();
  209. break;
  210. case TabBarIndex.qr:
  211. return Container();
  212. break;
  213. case TabBarIndex.notification:
  214. return NotificationScreen();
  215. break;
  216. case TabBarIndex.account:
  217. return AccountScreen();
  218. break;
  219. default:
  220. return PlotListScreen();
  221. }
  222. }),
  223. bottomNavigationBar: Container(
  224. padding: EdgeInsets.all(4),
  225. height: 70,
  226. decoration: BoxDecoration(color: Colors.white, border: Border(top: BorderSide(color: Colors.grey, width: 0.35))),
  227. child: GetBuilder<TabbarSelected>(
  228. init: changeTabbar,
  229. builder: (tabbarSelected) {
  230. return Center(
  231. child: ListView.builder(
  232. scrollDirection: Axis.horizontal,
  233. shrinkWrap: true,
  234. physics: NeverScrollableScrollPhysics(),
  235. itemCount: itemsTabbar.length,
  236. itemBuilder: (context, index) {
  237. return GestureDetector(
  238. child: Container(
  239. width: (Get.width - 20) / 5,
  240. margin: EdgeInsets.all(1),
  241. padding: EdgeInsets.all(10),
  242. child: Column(
  243. mainAxisAlignment: MainAxisAlignment.center,
  244. children: [
  245. index == 3
  246. ? Badge(
  247. // badgeContent: textCountNoti(),
  248. // shape: BadgeShape.circle,
  249. // badgeColor: Colors.red,
  250. // position: BadgePosition(top: -15, start: 10),
  251. child: SvgPicture.asset(
  252. itemsTabbar[index].icon,
  253. width: 24,
  254. height: 24,
  255. color: (tabbarSelected.index == itemsTabbar[index].index) ? AppColors.YELLOW : AppColors.GRAY1,
  256. ),
  257. )
  258. : SvgPicture.asset(
  259. itemsTabbar[index].icon,
  260. width: 24,
  261. height: 24,
  262. color: (tabbarSelected.index == itemsTabbar[index].index) ? AppColors.YELLOW : AppColors.GRAY1,
  263. ),
  264. Flexible(
  265. child: Text(
  266. itemsTabbar[index].title,
  267. style: TextStyle(
  268. color: (tabbarSelected.index == itemsTabbar[index].index) ? AppColors.DEFAULT : Colors.grey,
  269. fontSize: 9),
  270. ),
  271. )
  272. ],
  273. )),
  274. onTap: () {
  275. //Open scan qr code when tap icon in tabbar
  276. if (index == 2) {
  277. changeTabbar.changeIndex(changeTabbar.index ?? TabBarIndex.plot);
  278. // scan(context);
  279. Get.to(
  280. QrCodeScannerScreen(),
  281. )?.then((value) {
  282. if (value != null) {
  283. _showAlertCheckCropCode(value);
  284. }
  285. });
  286. } else {
  287. changeTabbar.changeIndex(itemsTabbar[index].index);
  288. }
  289. // changeTabbar.changeIndex(itemsTabbar[index].index);
  290. },
  291. );
  292. }),
  293. );
  294. })),
  295. )));
  296. }
  297. _showAlertCheckCropCode(String qrCodeValue) async {
  298. var repository = Repository();
  299. Get.defaultDialog(title: "Kiểm tra thông tin lô ....", middleText: "", content: CircularProgressIndicator());
  300. try {
  301. Uri uri = Uri.parse(qrCodeValue);
  302. String code = uri.pathSegments.isNotEmpty ? uri.pathSegments.last : '';
  303. if (code.startsWith('LO')) {
  304. await repository.getPlotDetailByCode(code).then((value) {
  305. print("ok");
  306. if (Get.isDialogOpen ?? false) Get.back();
  307. Get.to(
  308. PlotDetailScreen(
  309. cropId: value.tbCropDTO?.id ?? -1,
  310. cropType: value.tbCropDTO?.tbCropTypeId ?? -1,
  311. initialIndex: 0,
  312. ),
  313. );
  314. }).catchError((onError) {
  315. Utils.showDialog(
  316. title: "Không tìm thấy lô",
  317. message: "Thử lại với mã tem khác?",
  318. textConfirm: "Thử lại",
  319. textCancel: "Huỷ",
  320. onConfirm: () {
  321. Get.back();
  322. // scan(context);
  323. });
  324. });
  325. } else if (code.startsWith('AC')) {
  326. var stamp = await repository.getStampDetailByCode(code: code);
  327. Get.to(() => CodeDetailPage(stampId: stamp.id ?? 0, stampCode: stamp.code ?? ''));
  328. }
  329. } catch (e) {
  330. Utils.showDialog(
  331. title: "Không tìm thấy lô",
  332. message: "Thử lại với mã tem khác?",
  333. textConfirm: "Thử lại",
  334. textCancel: "Huỷ",
  335. onConfirm: () {
  336. Get.back();
  337. // scan(context);
  338. },
  339. );
  340. }
  341. }
  342. }
  343. class TabbarSelected extends GetxController {
  344. TabBarIndex? index = TabBarIndex.plot;
  345. void initValue() {
  346. index = TabBarIndex.plot;
  347. update();
  348. }
  349. void changeIndex(TabBarIndex changedIndex) {
  350. index = changedIndex;
  351. update();
  352. }
  353. }
  354. enum TabBarIndex {
  355. plot,
  356. // device,
  357. qrManage,
  358. qr,
  359. notification,
  360. account,
  361. }
  362. class TabbarItem {
  363. TabBarIndex index;
  364. String icon;
  365. String title;
  366. TabbarItem({required this.icon, required this.title, required this.index});
  367. }