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.

367 lines
12KB

  1. import 'package:farm_tpf/authentication/authentication.dart';
  2. import 'package:farm_tpf/data/repository/user_repository.dart';
  3. import 'package:farm_tpf/presentation/screens/notification/sc_notification.dart';
  4. import 'package:farm_tpf/presentation/screens/profile/sc_update_profile.dart';
  5. import 'package:farm_tpf/utils/const_color.dart';
  6. import 'package:farm_tpf/utils/pref.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter_bloc/flutter_bloc.dart';
  9. import 'package:get/get.dart';
  10. import 'package:get/state_manager.dart';
  11. class HomeDrawer extends StatefulWidget {
  12. const HomeDrawer(
  13. {Key key,
  14. this.screenIndex,
  15. this.iconAnimationController,
  16. this.callBackIndex})
  17. : super(key: key);
  18. final AnimationController iconAnimationController;
  19. final DrawerIndex screenIndex;
  20. final Function(DrawerIndex) callBackIndex;
  21. @override
  22. _HomeDrawerState createState() => _HomeDrawerState();
  23. }
  24. class _HomeDrawerState extends State<HomeDrawer> {
  25. var currentDrawerIndex = DrawerIndex.Home;
  26. var pref = LocalPref();
  27. UserRepository _userRepository = UserRepository();
  28. List<DrawerList> drawerList;
  29. final updateFullName = Get.put(UpdateFullName());
  30. @override
  31. void initState() {
  32. setdDrawerListArray();
  33. super.initState();
  34. }
  35. void setdDrawerListArray() {
  36. drawerList = <DrawerList>[
  37. DrawerList(
  38. index: DrawerIndex.ScanBarcode,
  39. labelName: "Quét mã QR",
  40. icon: Icon(Icons.camera)),
  41. DrawerList(
  42. index: DrawerIndex.Home,
  43. labelName: 'Danh sách lô',
  44. icon: Icon(Icons.home),
  45. ),
  46. DrawerList(
  47. index: DrawerIndex.ControlDevice,
  48. labelName: 'Điều khiển thiết bị',
  49. icon: Icon(Icons.device_hub),
  50. )
  51. ];
  52. }
  53. @override
  54. Widget build(BuildContext context) {
  55. return Container(
  56. child: Column(
  57. crossAxisAlignment: CrossAxisAlignment.stretch,
  58. mainAxisAlignment: MainAxisAlignment.start,
  59. children: <Widget>[
  60. Container(
  61. width: double.infinity,
  62. padding: const EdgeInsets.only(top: 45.0),
  63. child: Container(
  64. padding: const EdgeInsets.all(16.0),
  65. child: Column(
  66. crossAxisAlignment: CrossAxisAlignment.start,
  67. mainAxisAlignment: MainAxisAlignment.start,
  68. children: <Widget>[
  69. AnimatedBuilder(
  70. animation: widget.iconAnimationController,
  71. builder: (BuildContext context, Widget child) {
  72. return ScaleTransition(
  73. scale: AlwaysStoppedAnimation<double>(
  74. 1.0 - (widget.iconAnimationController.value) * 0.2),
  75. child: RotationTransition(
  76. turns: AlwaysStoppedAnimation<double>(Tween<double>(
  77. begin: 0.0, end: 24.0)
  78. .animate(CurvedAnimation(
  79. parent: widget.iconAnimationController,
  80. curve: Curves.fastOutSlowIn))
  81. .value /
  82. 360),
  83. child: Container(
  84. height: 100,
  85. width: 100,
  86. decoration: BoxDecoration(
  87. borderRadius: BorderRadius.circular(120),
  88. border: Border.all(
  89. width: 0.35, color: COLOR_CONST.DEFAULT),
  90. ),
  91. child: ClipRRect(
  92. borderRadius:
  93. const BorderRadius.all(Radius.circular(10.0)),
  94. child: FlutterLogo(
  95. size: 10,
  96. ),
  97. ),
  98. ),
  99. ),
  100. );
  101. },
  102. ),
  103. Padding(
  104. padding: const EdgeInsets.only(top: 8, left: 20),
  105. child: Text(
  106. '${Get.find<UpdateFullName>().fullName ?? ''}',
  107. style: TextStyle(
  108. fontWeight: FontWeight.w600,
  109. color: COLOR_CONST.GRAY1,
  110. fontSize: 18,
  111. ),
  112. ),
  113. ),
  114. ],
  115. ),
  116. ),
  117. ),
  118. const SizedBox(
  119. height: 4,
  120. ),
  121. Divider(
  122. height: 1,
  123. color: COLOR_CONST.GRAY1,
  124. ),
  125. Expanded(
  126. child: ListView.builder(
  127. physics: const BouncingScrollPhysics(),
  128. padding: const EdgeInsets.all(0.0),
  129. itemCount: drawerList.length,
  130. itemBuilder: (BuildContext context, int index) {
  131. return inkwell(drawerList[index]);
  132. },
  133. ),
  134. ),
  135. Divider(
  136. height: 1,
  137. color: COLOR_CONST.GRAY1,
  138. ),
  139. Column(
  140. children: <Widget>[
  141. ListTile(
  142. title: Text(
  143. 'Thông báo',
  144. style: TextStyle(
  145. fontWeight: FontWeight.w600,
  146. fontSize: 16,
  147. ),
  148. textAlign: TextAlign.left,
  149. ),
  150. leading: Icon(
  151. Icons.notifications,
  152. color: Colors.black,
  153. ),
  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. }
  327. enum DrawerIndex { ScanBarcode, Home, ControlDevice }
  328. class DrawerList {
  329. DrawerList({
  330. this.isAssetsImage = false,
  331. this.labelName = '',
  332. this.icon,
  333. this.index,
  334. this.imageName = '',
  335. });
  336. String labelName;
  337. Icon icon;
  338. bool isAssetsImage;
  339. String imageName;
  340. DrawerIndex index;
  341. }
  342. class UpdateFullName extends GetxController {
  343. var fullName;
  344. void init() {
  345. fullName = "";
  346. }
  347. void changeName(String name) {
  348. fullName = name;
  349. update();
  350. }
  351. }