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.

417 lines
14KB

  1. import 'package:badges/badges.dart';
  2. import 'package:farm_tpf/authentication/authentication.dart';
  3. import 'package:farm_tpf/custom_model/NotificationObjectDTO.dart';
  4. import 'package:farm_tpf/data/repository/user_repository.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/profile/sc_update_profile.dart';
  8. import 'package:farm_tpf/utils/const_color.dart';
  9. import 'package:farm_tpf/utils/pref.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:flutter_bloc/flutter_bloc.dart';
  12. import 'package:get/get.dart';
  13. import 'package:get/state_manager.dart';
  14. import '../../../main.dart';
  15. class HomeDrawer extends StatefulWidget {
  16. const HomeDrawer(
  17. {Key key,
  18. this.screenIndex,
  19. this.iconAnimationController,
  20. this.callBackIndex})
  21. : super(key: key);
  22. final AnimationController iconAnimationController;
  23. final DrawerIndex screenIndex;
  24. final Function(DrawerIndex) callBackIndex;
  25. @override
  26. _HomeDrawerState createState() => _HomeDrawerState();
  27. }
  28. class _HomeDrawerState extends State<HomeDrawer> {
  29. var currentDrawerIndex = DrawerIndex.Home;
  30. var pref = LocalPref();
  31. UserRepository _userRepository = UserRepository();
  32. List<DrawerList> drawerList;
  33. final updateFullName = Get.put(UpdateFullName());
  34. @override
  35. void initState() {
  36. setdDrawerListArray();
  37. super.initState();
  38. }
  39. void setdDrawerListArray() {
  40. drawerList = <DrawerList>[
  41. DrawerList(
  42. index: DrawerIndex.ScanBarcode,
  43. labelName: "Quét mã QR",
  44. icon: Icon(Icons.camera)),
  45. DrawerList(
  46. index: DrawerIndex.Home,
  47. labelName: 'Danh sách lô',
  48. icon: Icon(Icons.home),
  49. ),
  50. DrawerList(
  51. index: DrawerIndex.ControlDevice,
  52. labelName: 'Điều khiển thiết bị',
  53. icon: Icon(Icons.device_hub),
  54. )
  55. ];
  56. }
  57. @override
  58. Widget build(BuildContext context) {
  59. return Container(
  60. child: Column(
  61. crossAxisAlignment: CrossAxisAlignment.stretch,
  62. mainAxisAlignment: MainAxisAlignment.start,
  63. children: <Widget>[
  64. Container(
  65. width: double.infinity,
  66. padding: const EdgeInsets.only(top: 45.0),
  67. child: Container(
  68. padding: const EdgeInsets.all(16.0),
  69. child: Column(
  70. crossAxisAlignment: CrossAxisAlignment.start,
  71. mainAxisAlignment: MainAxisAlignment.start,
  72. children: <Widget>[
  73. AnimatedBuilder(
  74. animation: widget.iconAnimationController,
  75. builder: (BuildContext context, Widget child) {
  76. return ScaleTransition(
  77. scale: AlwaysStoppedAnimation<double>(
  78. 1.0 - (widget.iconAnimationController.value) * 0.2),
  79. child: RotationTransition(
  80. turns: AlwaysStoppedAnimation<double>(Tween<double>(
  81. begin: 0.0, end: 24.0)
  82. .animate(CurvedAnimation(
  83. parent: widget.iconAnimationController,
  84. curve: Curves.fastOutSlowIn))
  85. .value /
  86. 360),
  87. child: Container(
  88. height: 100,
  89. width: 100,
  90. decoration: BoxDecoration(
  91. borderRadius: BorderRadius.circular(120),
  92. border: Border.all(
  93. width: 0.35, color: COLOR_CONST.DEFAULT),
  94. ),
  95. child: ClipRRect(
  96. borderRadius:
  97. const BorderRadius.all(Radius.circular(10.0)),
  98. child: FlutterLogo(
  99. size: 10,
  100. ),
  101. ),
  102. ),
  103. ),
  104. );
  105. },
  106. ),
  107. Padding(
  108. padding: const EdgeInsets.only(top: 8, left: 20),
  109. child: Text(
  110. '${Get.find<UpdateFullName>().fullName ?? ''}',
  111. style: TextStyle(
  112. fontWeight: FontWeight.w600,
  113. color: COLOR_CONST.GRAY1,
  114. fontSize: 18,
  115. ),
  116. ),
  117. ),
  118. ],
  119. ),
  120. ),
  121. ),
  122. const SizedBox(
  123. height: 4,
  124. ),
  125. Divider(
  126. height: 1,
  127. color: COLOR_CONST.GRAY1,
  128. ),
  129. Expanded(
  130. child: ListView.builder(
  131. physics: const BouncingScrollPhysics(),
  132. padding: const EdgeInsets.all(0.0),
  133. itemCount: drawerList.length,
  134. itemBuilder: (BuildContext context, int index) {
  135. return inkwell(drawerList[index]);
  136. },
  137. ),
  138. ),
  139. Divider(
  140. height: 1,
  141. color: COLOR_CONST.GRAY1,
  142. ),
  143. Column(
  144. children: <Widget>[
  145. ListTile(
  146. title: Text(
  147. 'Thông báo',
  148. style: TextStyle(
  149. fontWeight: FontWeight.w600,
  150. fontSize: 16,
  151. ),
  152. textAlign: TextAlign.left,
  153. ),
  154. leading: _iconWithBadgeNotiCount(),
  155. onTap: () {
  156. navigationtoScreen(currentDrawerIndex);
  157. Navigator.of(context).push(
  158. MaterialPageRoute(builder: (_) => NotificationScreen()));
  159. },
  160. ),
  161. SizedBox(
  162. height: 2.0,
  163. ),
  164. ListTile(
  165. title: Text(
  166. 'Cài đặt',
  167. style: TextStyle(
  168. fontWeight: FontWeight.w600,
  169. fontSize: 16,
  170. ),
  171. textAlign: TextAlign.left,
  172. ),
  173. leading: Icon(
  174. Icons.settings,
  175. color: Colors.black,
  176. ),
  177. onTap: () {
  178. navigationtoScreen(currentDrawerIndex);
  179. Navigator.of(context).push(
  180. MaterialPageRoute(builder: (_) => UpdateProfileScreen()));
  181. },
  182. ),
  183. SizedBox(
  184. height: 2.0,
  185. ),
  186. ListTile(
  187. title: Text(
  188. 'Đăng xuất',
  189. style: TextStyle(
  190. fontWeight: FontWeight.w600,
  191. fontSize: 16,
  192. ),
  193. textAlign: TextAlign.left,
  194. ),
  195. leading: Icon(
  196. Icons.power_settings_new,
  197. color: Colors.red,
  198. ),
  199. onTap: () {
  200. _clickSignOut();
  201. },
  202. ),
  203. SizedBox(
  204. height: MediaQuery.of(context).padding.bottom,
  205. )
  206. ],
  207. ),
  208. ],
  209. ),
  210. );
  211. }
  212. Widget inkwell(DrawerList listData) {
  213. return Material(
  214. color: Colors.white,
  215. child: InkWell(
  216. splashColor: Colors.grey.withOpacity(0.1),
  217. highlightColor: Colors.transparent,
  218. onTap: () {
  219. if (listData.index == DrawerIndex.ScanBarcode) {
  220. scan(context);
  221. navigationtoScreen(currentDrawerIndex);
  222. } else {
  223. currentDrawerIndex = listData.index;
  224. navigationtoScreen(currentDrawerIndex);
  225. }
  226. },
  227. child: Stack(
  228. children: <Widget>[
  229. Container(
  230. padding: const EdgeInsets.only(top: 8.0, bottom: 8.0),
  231. child: Row(
  232. children: <Widget>[
  233. Container(
  234. width: 6.0,
  235. height: 46.0,
  236. ),
  237. const Padding(
  238. padding: EdgeInsets.all(4.0),
  239. ),
  240. listData.isAssetsImage
  241. ? Container(
  242. width: 24,
  243. height: 24,
  244. child: Image.asset(listData.imageName,
  245. color: currentDrawerIndex == listData.index
  246. ? COLOR_CONST.DEFAULT
  247. : COLOR_CONST.BLACK),
  248. )
  249. : Icon(listData.icon.icon,
  250. color: widget.screenIndex == listData.index
  251. ? COLOR_CONST.DEFAULT
  252. : COLOR_CONST.BLACK),
  253. const Padding(
  254. padding: EdgeInsets.all(4.0),
  255. ),
  256. Text(
  257. listData.labelName,
  258. style: TextStyle(
  259. fontWeight: FontWeight.w500,
  260. fontSize: 16,
  261. color: widget.screenIndex == listData.index
  262. ? COLOR_CONST.DEFAULT
  263. : COLOR_CONST.BLACK,
  264. ),
  265. textAlign: TextAlign.left,
  266. ),
  267. ],
  268. ),
  269. ),
  270. widget.screenIndex == listData.index
  271. ? AnimatedBuilder(
  272. animation: widget.iconAnimationController,
  273. builder: (BuildContext context, Widget child) {
  274. return Transform(
  275. transform: Matrix4.translationValues(
  276. (MediaQuery.of(context).size.width * 0.75 - 64) *
  277. (1.0 -
  278. widget.iconAnimationController.value -
  279. 1.0),
  280. 0.0,
  281. 0.0),
  282. child: Padding(
  283. padding: EdgeInsets.only(top: 8, bottom: 8),
  284. child: Container(
  285. width:
  286. MediaQuery.of(context).size.width * 0.75 - 64,
  287. height: 46,
  288. decoration: BoxDecoration(
  289. color: COLOR_CONST.DEFAULT.withOpacity(0.2),
  290. borderRadius: new BorderRadius.only(
  291. topLeft: Radius.circular(0),
  292. topRight: Radius.circular(28),
  293. bottomLeft: Radius.circular(0),
  294. bottomRight: Radius.circular(28),
  295. ),
  296. ),
  297. ),
  298. ),
  299. );
  300. },
  301. )
  302. : const SizedBox()
  303. ],
  304. ),
  305. ),
  306. );
  307. }
  308. Future<void> navigationtoScreen(DrawerIndex indexScreen) async {
  309. widget.callBackIndex(indexScreen);
  310. }
  311. _clickSignOut() async {
  312. context.bloc<AuthenticationBloc>().add(AuthenticationLogoutRequested());
  313. try {
  314. String pushKey = await pref.getString(DATA_CONST.PUSH_KEY);
  315. if (pushKey.isNotEmpty) {
  316. _userRepository
  317. .deleteFcmToken(pushKey)
  318. .then((value) {})
  319. .catchError((err) {})
  320. .whenComplete(() {
  321. pref.saveString(DATA_CONST.TOKEN_KEY, "");
  322. pref.saveString(DATA_CONST.PUSH_KEY, "");
  323. pref.saveString(DATA_CONST.CURRENT_FULL_NAME, "");
  324. });
  325. }
  326. } catch (e) {
  327. pref.saveString(DATA_CONST.CURRENT_FULL_NAME, "");
  328. pref.saveString(DATA_CONST.TOKEN_KEY, "");
  329. pref.saveString(DATA_CONST.PUSH_KEY, "");
  330. }
  331. }
  332. Widget _iconWithBadgeNotiCount() {
  333. return StreamBuilder(
  334. stream: updateCountNotiBloc.actions,
  335. builder: (context, AsyncSnapshot<dynamic> snapshot) {
  336. if (snapshot.hasData) {
  337. var noti = snapshot.data as NotificationObjectDTO;
  338. var unreadNoti = noti.numberUnreadTotal.toString();
  339. return Badge(
  340. badgeContent: Text(
  341. '' + unreadNoti,
  342. softWrap: true,
  343. style: TextStyle(color: Colors.white),
  344. ),
  345. shape: BadgeShape.square,
  346. badgeColor: Colors.red,
  347. borderRadius: 20,
  348. position: BadgePosition(top: -15, start: 10),
  349. child: Icon(
  350. Icons.notifications,
  351. color: Colors.black,
  352. ),
  353. );
  354. } else {
  355. return Badge(
  356. badgeContent: Text(
  357. '0',
  358. softWrap: true,
  359. style: TextStyle(color: Colors.white),
  360. ),
  361. shape: BadgeShape.square,
  362. badgeColor: Colors.red,
  363. borderRadius: 20,
  364. position: BadgePosition(top: -15, start: 10),
  365. child: Icon(
  366. Icons.notifications,
  367. color: Colors.black,
  368. ),
  369. );
  370. }
  371. },
  372. );
  373. }
  374. }
  375. enum DrawerIndex { ScanBarcode, Home, ControlDevice }
  376. class DrawerList {
  377. DrawerList({
  378. this.isAssetsImage = false,
  379. this.labelName = '',
  380. this.icon,
  381. this.index,
  382. this.imageName = '',
  383. });
  384. String labelName;
  385. Icon icon;
  386. bool isAssetsImage;
  387. String imageName;
  388. DrawerIndex index;
  389. }
  390. class UpdateFullName extends GetxController {
  391. var fullName;
  392. void init() {
  393. fullName = "";
  394. }
  395. void changeName(String name) {
  396. fullName = name;
  397. update();
  398. }
  399. }