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.

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