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.

247 lines
9.4KB

  1. import 'package:farm_tpf/data/repository/user_repository.dart';
  2. import 'package:farm_tpf/presentation/screens/notification/update_count_noti_bloc.dart';
  3. import 'package:farm_tpf/utils/const_color.dart';
  4. import 'package:farm_tpf/utils/pref.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:get/get.dart';
  7. import 'home_drawer.dart';
  8. class DrawerUserController extends StatefulWidget {
  9. const DrawerUserController({
  10. Key key,
  11. this.drawerWidth = 250,
  12. this.onDrawerCall,
  13. this.screenView,
  14. this.animatedIconData = AnimatedIcons.arrow_menu,
  15. this.menuView,
  16. this.drawerIsOpen,
  17. this.screenIndex,
  18. }) : super(key: key);
  19. final double drawerWidth;
  20. final Function(DrawerIndex) onDrawerCall;
  21. final Widget screenView;
  22. final Function(bool) drawerIsOpen;
  23. final AnimatedIconData animatedIconData;
  24. final Widget menuView;
  25. final DrawerIndex screenIndex;
  26. @override
  27. _DrawerUserControllerState createState() => _DrawerUserControllerState();
  28. }
  29. class _DrawerUserControllerState extends State<DrawerUserController>
  30. with TickerProviderStateMixin {
  31. ScrollController scrollController;
  32. AnimationController iconAnimationController;
  33. AnimationController animationController;
  34. UserRepository _userRepository = UserRepository();
  35. double scrolloffset = 0.0;
  36. var pref = LocalPref();
  37. final updateFullName = Get.put(UpdateFullName());
  38. @override
  39. void initState() {
  40. animationController = AnimationController(
  41. duration: const Duration(milliseconds: 2000), vsync: this);
  42. iconAnimationController = AnimationController(
  43. vsync: this, duration: const Duration(milliseconds: 0));
  44. iconAnimationController
  45. ..animateTo(1.0,
  46. duration: const Duration(milliseconds: 0),
  47. curve: Curves.fastOutSlowIn);
  48. scrollController =
  49. ScrollController(initialScrollOffset: widget.drawerWidth);
  50. scrollController
  51. ..addListener(() {
  52. if (scrollController.offset <= 0) {
  53. if (scrolloffset != 1.0) {
  54. setState(() {
  55. scrolloffset = 1.0;
  56. try {
  57. //open Drawer
  58. updateCountNotiBloc.getNotifications((data) {}, (err) {});
  59. widget.drawerIsOpen(true);
  60. } catch (_) {}
  61. });
  62. }
  63. iconAnimationController.animateTo(0.0,
  64. duration: const Duration(milliseconds: 0),
  65. curve: Curves.fastOutSlowIn);
  66. } else if (scrollController.offset > 0 &&
  67. scrollController.offset < widget.drawerWidth) {
  68. iconAnimationController.animateTo(
  69. (scrollController.offset * 100 / (widget.drawerWidth)) / 100,
  70. duration: const Duration(milliseconds: 0),
  71. curve: Curves.fastOutSlowIn);
  72. } else if (scrollController.offset <= widget.drawerWidth) {
  73. if (scrolloffset != 0.0) {
  74. setState(() {
  75. scrolloffset = 0.0;
  76. try {
  77. widget.drawerIsOpen(false);
  78. } catch (_) {}
  79. });
  80. }
  81. iconAnimationController.animateTo(1.0,
  82. duration: const Duration(milliseconds: 0),
  83. curve: Curves.fastOutSlowIn);
  84. }
  85. });
  86. WidgetsBinding.instance.addPostFrameCallback((_) => getInitState());
  87. super.initState();
  88. }
  89. Future<bool> getInitState() async {
  90. scrollController.jumpTo(
  91. widget.drawerWidth,
  92. );
  93. return true;
  94. }
  95. @override
  96. Widget build(BuildContext context) {
  97. return Scaffold(
  98. backgroundColor: COLOR_CONST.WHITE,
  99. body: SingleChildScrollView(
  100. controller: scrollController,
  101. scrollDirection: Axis.horizontal,
  102. physics: const PageScrollPhysics(parent: ClampingScrollPhysics()),
  103. child: SizedBox(
  104. height: MediaQuery.of(context).size.height,
  105. width: MediaQuery.of(context).size.width + widget.drawerWidth,
  106. //we use with as screen width and add drawerWidth (from navigation_home_screen)
  107. child: Row(
  108. children: <Widget>[
  109. SizedBox(
  110. width: widget.drawerWidth,
  111. //we divided first drawer Width with HomeDrawer and second full-screen Width with all home screen, we called screen View
  112. height: MediaQuery.of(context).size.height,
  113. child: AnimatedBuilder(
  114. animation: iconAnimationController,
  115. builder: (BuildContext context, Widget child) {
  116. return Transform(
  117. //transform we use for the stable drawer we, not need to move with scroll view
  118. transform: Matrix4.translationValues(
  119. scrollController.offset, 0.0, 0.0),
  120. child: HomeDrawer(
  121. screenIndex: widget.screenIndex == null
  122. ? DrawerIndex.Home
  123. : widget.screenIndex,
  124. iconAnimationController: iconAnimationController,
  125. callBackIndex: (DrawerIndex indexType) {
  126. onDrawerClick();
  127. try {
  128. widget.onDrawerCall(indexType);
  129. } catch (e) {}
  130. },
  131. ),
  132. );
  133. },
  134. ),
  135. ),
  136. SizedBox(
  137. width: MediaQuery.of(context).size.width,
  138. height: MediaQuery.of(context).size.height,
  139. //full-screen Width with widget.screenView
  140. child: Container(
  141. decoration: BoxDecoration(
  142. color: COLOR_CONST.WHITE,
  143. boxShadow: <BoxShadow>[
  144. BoxShadow(color: COLOR_CONST.GRAY1, blurRadius: 24),
  145. ],
  146. ),
  147. child: Stack(
  148. children: <Widget>[
  149. //this IgnorePointer we use as touch(user Interface) widget.screen View, for example scrolloffset == 1 means drawer is close we just allow touching all widget.screen View
  150. IgnorePointer(
  151. ignoring: scrolloffset == 1 || false,
  152. child: widget.screenView,
  153. ),
  154. //alternative touch(user Interface) for widget.screen, for example, drawer is close we need to tap on a few home screen area and close the drawer
  155. if (scrolloffset == 1.0)
  156. InkWell(
  157. onTap: () {
  158. onDrawerClick();
  159. },
  160. ),
  161. // this just menu and arrow icon animation
  162. Padding(
  163. padding: EdgeInsets.only(
  164. top: MediaQuery.of(context).padding.top + 8,
  165. left: 8),
  166. child: SizedBox(
  167. width: AppBar().preferredSize.height - 8,
  168. height: AppBar().preferredSize.height - 8,
  169. child: Material(
  170. color: Colors.transparent,
  171. child: InkWell(
  172. borderRadius: BorderRadius.circular(
  173. AppBar().preferredSize.height),
  174. child: Center(
  175. // if you use your own menu view UI you add form initialization
  176. child: widget.menuView != null
  177. ? widget.menuView
  178. : AnimatedIcon(
  179. icon: widget.animatedIconData != null
  180. ? widget.animatedIconData
  181. : AnimatedIcons.arrow_menu,
  182. progress: iconAnimationController),
  183. ),
  184. onTap: () {
  185. FocusScope.of(context)
  186. .requestFocus(FocusNode());
  187. onDrawerClick();
  188. },
  189. ),
  190. ),
  191. ),
  192. ),
  193. ],
  194. ),
  195. ),
  196. ),
  197. ],
  198. ),
  199. ),
  200. ),
  201. );
  202. }
  203. void onDrawerClick() {
  204. getSharedPrefs();
  205. //if scrollcontroller.offset != 0.0 then we set to closed the drawer(with animation to offset zero position) if is not 1 then open the drawer
  206. if (scrollController.offset != 0.0) {
  207. scrollController.animateTo(
  208. 0.0,
  209. duration: const Duration(milliseconds: 400),
  210. curve: Curves.fastOutSlowIn,
  211. );
  212. } else {
  213. scrollController.animateTo(
  214. widget.drawerWidth,
  215. duration: const Duration(milliseconds: 400),
  216. curve: Curves.fastOutSlowIn,
  217. );
  218. }
  219. }
  220. Future<Null> getSharedPrefs() async {
  221. try {
  222. updateFullName.init();
  223. var currentFullName = await pref.getString(DATA_CONST.CURRENT_FULL_NAME);
  224. if (currentFullName.isEmpty) {
  225. var currentUser = await _userRepository.getUser();
  226. pref.saveString(DATA_CONST.CURRENT_FULL_NAME, currentUser.fullName);
  227. updateFullName.changeName(currentUser.fullName);
  228. } else {
  229. updateFullName.changeName(currentFullName);
  230. }
  231. } catch (e) {
  232. print("error: ${e.toString()}");
  233. }
  234. }
  235. }