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.

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