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.

202 lines
8.1KB

  1. import 'package:badges/badges.dart';
  2. import 'package:farm_tpf/custom_model/NotificationObjectDTO.dart';
  3. import 'package:farm_tpf/presentation/screens/account/sc_account.dart';
  4. import 'package:farm_tpf/presentation/screens/control_device/sc_control_device.dart';
  5. import 'package:farm_tpf/presentation/screens/notification/sc_notification.dart';
  6. import 'package:farm_tpf/presentation/screens/notification/update_count_noti_bloc.dart';
  7. import 'package:farm_tpf/presentation/screens/plot/sc_plot.dart';
  8. import 'package:farm_tpf/utils/const_color.dart';
  9. import 'package:farm_tpf/utils/const_icons.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:flutter_svg/flutter_svg.dart';
  12. import 'package:get/get.dart';
  13. import '../../../main.dart';
  14. class TabbarScreen extends StatefulWidget {
  15. static Route route() {
  16. return MaterialPageRoute<void>(builder: (_) => TabbarScreen());
  17. }
  18. @override
  19. _TabbarScreenState createState() => _TabbarScreenState();
  20. }
  21. class _TabbarScreenState extends State<TabbarScreen> {
  22. final changeTabbar = Get.put(TabbarSelected());
  23. List<TabbarItem> itemsTabbar = [
  24. TabbarItem(
  25. icon: AppIcons.icPlot, title: 'Lô trồng', index: TabBarIndex.plot),
  26. TabbarItem(
  27. icon: AppIcons.icDevice, title: 'Thiết bị', index: TabBarIndex.device),
  28. TabbarItem(icon: AppIcons.icQr, title: 'Quét QR', index: TabBarIndex.qr),
  29. TabbarItem(
  30. icon: AppIcons.icNotification,
  31. title: 'Thông báo',
  32. index: TabBarIndex.notification),
  33. TabbarItem(
  34. icon: AppIcons.icPerson, title: 'Cá nhân', index: TabBarIndex.account)
  35. ];
  36. @override
  37. void initState() {
  38. super.initState();
  39. changeTabbar.initValue();
  40. updateCountNotiBloc.getNotifications((data) {}, (err) {});
  41. }
  42. Widget textCountNoti() {
  43. return StreamBuilder(
  44. stream: updateCountNotiBloc.actions,
  45. builder: (context, AsyncSnapshot<dynamic> snapshot) {
  46. if (snapshot.hasData) {
  47. var noti = snapshot.data as NotificationObjectDTO;
  48. var unreadNoti =
  49. noti.numberUnreadTotal > 99 ? '99+' : '${noti.numberUnreadTotal}';
  50. return Text(
  51. '$unreadNoti',
  52. softWrap: true,
  53. style: TextStyle(color: Colors.white, fontSize: 10),
  54. );
  55. } else {
  56. return Text(
  57. 'O',
  58. softWrap: true,
  59. style: TextStyle(color: Colors.white, fontSize: 10),
  60. );
  61. }
  62. },
  63. );
  64. }
  65. @override
  66. Widget build(BuildContext context) {
  67. return Container(
  68. color: Colors.white,
  69. child: SafeArea(
  70. top: false,
  71. bottom: true,
  72. child: Scaffold(
  73. body: GetBuilder<TabbarSelected>(builder: (tabbarSelected) {
  74. switch (tabbarSelected.index) {
  75. case TabBarIndex.plot:
  76. return PlotListScreen();
  77. break;
  78. case TabBarIndex.device:
  79. return ControlDeviceScreen();
  80. break;
  81. case TabBarIndex.qr:
  82. break;
  83. case TabBarIndex.notification:
  84. return NotificationScreen();
  85. break;
  86. case TabBarIndex.account:
  87. return AccountScreen();
  88. break;
  89. default:
  90. return PlotListScreen();
  91. }
  92. }),
  93. bottomNavigationBar: Container(
  94. padding: EdgeInsets.all(4),
  95. height: 70,
  96. decoration: BoxDecoration(
  97. color: Colors.white,
  98. border: Border(
  99. top: BorderSide(color: Colors.grey, width: 0.35))),
  100. child: GetBuilder<TabbarSelected>(builder: (tabbarSelected) {
  101. return Center(
  102. child: ListView.builder(
  103. scrollDirection: Axis.horizontal,
  104. shrinkWrap: true,
  105. physics: NeverScrollableScrollPhysics(),
  106. itemCount: itemsTabbar.length,
  107. itemBuilder: (context, index) {
  108. return GestureDetector(
  109. child: Container(
  110. width: (Get.width - 20) / 5,
  111. margin: EdgeInsets.all(1),
  112. padding: EdgeInsets.all(10),
  113. child: Column(
  114. mainAxisAlignment: MainAxisAlignment.center,
  115. children: [
  116. index == 3
  117. ? Badge(
  118. badgeContent: textCountNoti(),
  119. shape: BadgeShape.circle,
  120. badgeColor: Colors.red,
  121. position: BadgePosition(
  122. top: -15, start: 10),
  123. child: SvgPicture.asset(
  124. itemsTabbar[index].icon,
  125. width: 24,
  126. height: 24,
  127. color: (tabbarSelected.index ==
  128. itemsTabbar[index]
  129. .index)
  130. ? AppColors.YELLOW
  131. : AppColors.GRAY1,
  132. ),
  133. )
  134. : SvgPicture.asset(
  135. itemsTabbar[index].icon,
  136. width: 24,
  137. height: 24,
  138. color: (tabbarSelected.index ==
  139. itemsTabbar[index].index)
  140. ? AppColors.YELLOW
  141. : AppColors.GRAY1,
  142. ),
  143. Flexible(
  144. child: Text(
  145. itemsTabbar[index].title,
  146. style: TextStyle(
  147. color: (tabbarSelected.index ==
  148. itemsTabbar[index].index)
  149. ? AppColors.DEFAULT
  150. : Colors.grey,
  151. fontSize: 10),
  152. ),
  153. )
  154. ],
  155. )),
  156. onTap: () {
  157. //Open scan qr code when tap icon in tabbar
  158. if (index == 2) {
  159. changeTabbar.changeIndex(changeTabbar.index);
  160. scan(context);
  161. } else {
  162. changeTabbar
  163. .changeIndex(itemsTabbar[index].index);
  164. }
  165. },
  166. );
  167. }),
  168. );
  169. })),
  170. )));
  171. }
  172. }
  173. class TabbarSelected extends GetxController {
  174. TabBarIndex index;
  175. void initValue() {
  176. index = TabBarIndex.plot;
  177. update();
  178. }
  179. void changeIndex(TabBarIndex changedIndex) {
  180. index = changedIndex;
  181. update();
  182. }
  183. }
  184. enum TabBarIndex { plot, device, qr, notification, account }
  185. class TabbarItem {
  186. TabBarIndex index;
  187. String icon;
  188. String title;
  189. TabbarItem({this.icon, this.title, this.index});
  190. }