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.

410 lines
13KB

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