Advertisement
sourav8256

Fancy App Drawer Flutter

Sep 10th, 2023 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 KB | None | 0 0
  1. =============== How to use =======================================================================================
  2.  
  3. Navigator.push(context, MaterialPageRoute(builder: (context) {
  4. return AppDrawer(child: SubCategories(categoryJo: categoryJo[i],));
  5. }));
  6.  
  7.  
  8. ================= How to open and close drawer ===================================================================
  9.  
  10. onTap: () {
  11. // context.open();
  12. AppDrawer.of(context)!.toggle();
  13. },
  14.  
  15. ================= Import App Drawer =======================
  16.  
  17. import 'package:debasish_app/components/AppDrawer.dart';
  18.  
  19. ================== App Drawer Code (AppDrawer.dart) ================================================================
  20.  
  21. import 'package:flutter/material.dart';
  22. import 'package:legalapp/main.dart';
  23.  
  24. class AppDrawer extends StatefulWidget {
  25. final Widget child;
  26. const AppDrawer({key, required this.child}) : super(key: key);
  27.  
  28. static _AppDrawerState? of(BuildContext context) =>
  29. context.findAncestorStateOfType<_AppDrawerState>();
  30.  
  31. @override
  32. _AppDrawerState createState() => _AppDrawerState();
  33. }
  34.  
  35. class _AppDrawerState extends State<AppDrawer>
  36. with SingleTickerProviderStateMixin {
  37. static Duration duration = Duration(milliseconds: 300);
  38. late AnimationController _controller;
  39. static const double maxSlide = 255;
  40. static const dragRightStartVal = 60;
  41. static const dragLeftStartVal = maxSlide - 20;
  42. static bool shouldDrag = false;
  43.  
  44. @override
  45. void initState() {
  46. _controller =
  47. AnimationController(vsync: this, duration: _AppDrawerState.duration);
  48. super.initState();
  49. }
  50.  
  51. void close() => _controller.reverse();
  52.  
  53. void open() => _controller.forward();
  54.  
  55. void toggle() {
  56. if (_controller.isCompleted) {
  57. close();
  58. } else {
  59. open();
  60. }
  61. }
  62.  
  63. @override
  64. void dispose() {
  65. _controller.dispose();
  66. super.dispose();
  67. }
  68.  
  69. void _onDragStart(DragStartDetails startDetails) {
  70. bool isDraggingFromLeft = _controller.isDismissed &&
  71. startDetails.globalPosition.dx < dragRightStartVal;
  72. bool isDraggingFromRight = _controller.isCompleted &&
  73. startDetails.globalPosition.dx > dragLeftStartVal;
  74. shouldDrag = isDraggingFromLeft || isDraggingFromRight;
  75. }
  76.  
  77. void _onDragUpdate(DragUpdateDetails updateDetails) {
  78. if (shouldDrag == false) {
  79. return;
  80. }
  81. double delta = updateDetails.primaryDelta! / maxSlide;
  82. _controller.value += delta;
  83. }
  84.  
  85. void _onDragEnd(DragEndDetails dragEndDetails) {
  86. if (_controller.isDismissed || _controller.isCompleted) {
  87. return;
  88. }
  89.  
  90. double _kMinFlingVelocity = 365.0;
  91. double dragVelocity = dragEndDetails.velocity.pixelsPerSecond.dx.abs();
  92.  
  93. if (dragVelocity >= _kMinFlingVelocity) {
  94. double visualVelocityInPx = dragEndDetails.velocity.pixelsPerSecond.dx /
  95. MediaQuery.of(context).size.width;
  96. _controller.fling(velocity: visualVelocityInPx);
  97. } else if (_controller.value < 0.5) {
  98. close();
  99. } else {
  100. open();
  101. }
  102. }
  103.  
  104. @override
  105. Widget build(BuildContext context) {
  106. return GestureDetector(
  107. onHorizontalDragStart: _onDragStart,
  108. onHorizontalDragUpdate: _onDragUpdate,
  109. onHorizontalDragEnd: _onDragEnd,
  110. child: AnimatedBuilder(
  111. animation: _controller,
  112. builder: (BuildContext context, _) {
  113. double animationVal = _controller.value;
  114. double translateVal = animationVal * maxSlide;
  115. double scaleVal = 1 - (animationVal * 0.3);
  116. return Stack(
  117. children: <Widget>[
  118. CustomDrawer(),
  119. Transform(
  120. alignment: Alignment.centerLeft,
  121. transform: Matrix4.identity()
  122. ..translate(translateVal)
  123. ..scale(scaleVal),
  124. child: GestureDetector(
  125. onTap: () {
  126. if (_controller.isCompleted) {
  127. close();
  128. }
  129. },
  130. child: widget.child),
  131. ),
  132. ],
  133. );
  134. },
  135. ),
  136. );
  137. }
  138. }
  139.  
  140. class CustomDrawer extends StatelessWidget {
  141.  
  142. @override
  143. Widget build(BuildContext context) {
  144. return Material(
  145. color: const Color(0XFF525463),
  146. child: SafeArea(
  147. child: Theme(
  148. data: ThemeData(
  149. brightness: Brightness.dark,
  150. ),
  151. child: SingleChildScrollView(
  152. child: Column(
  153. crossAxisAlignment: CrossAxisAlignment.start,
  154. children: <Widget>[
  155. // Container(
  156. // padding: EdgeInsets.all(16),
  157. // child: Text('Flutter App', style: TextStyle(fontSize: 30, color: Colors.white)),
  158. // ),
  159. InkWell(
  160. onTap: () {
  161. AppDrawer.of(context)!.toggle();
  162. },
  163. child: Container(
  164. padding: EdgeInsets.all(16),
  165. child: Icon(Icons.arrow_back_ios),
  166. ),
  167. ),
  168. // ListTile(
  169. // leading: Icon(Icons.home),
  170. // title: Text('Home'),
  171. // ),
  172. // ListTile(
  173. // leading: Icon(Icons.info),
  174. // title: Text('About'),
  175. // ),
  176. // ListTile(
  177. // leading: Icon(Icons.settings),
  178. // title: Text('Settings'),
  179. // ),
  180. // ListTile(
  181. // leading: Icon(Icons.lock),
  182. // title: Text('Logout'),
  183. // ),
  184. ListTile(),
  185. option(context: context,active: true, icon: Icon(Icons.home),name: "Home", ontap: () {
  186. Navigator.push(context,
  187. MaterialPageRoute(builder: (context) {
  188. return MyHomePage(title: "Test",);
  189. }));
  190. }),
  191. SizedBox(
  192. height: 47,
  193. ),
  194.  
  195. ],
  196. ),
  197. ),
  198. ),
  199. ),
  200. );
  201. }
  202.  
  203. Widget option({active = false, icon = const Icon(Icons.lock),required context,required name,ontap}) {
  204. return Column(
  205. children: [
  206. Container(
  207. width: 250,
  208. // color: Colors.yellow,
  209. child: Row(
  210. mainAxisAlignment: MainAxisAlignment.center,
  211. children: [
  212. InkWell(
  213. onTap: ontap,
  214. child: Container(
  215. height: 72,
  216. width: 72,
  217. // margin: EdgeInsets.only(left: 98),
  218. decoration: BoxDecoration(
  219. color: active ? Color(0XFF5DC0AF) : Color(0XFF454755),
  220. borderRadius: BorderRadius.circular(36.0),
  221. ),
  222. child: Center(
  223. child: icon,
  224. ),
  225. ),
  226. ),
  227. ],
  228. ),
  229. ),
  230. SizedBox(
  231. height: 10,
  232. ),
  233. Container(
  234. width: 250,
  235. child: Row(
  236. mainAxisAlignment: MainAxisAlignment.center,
  237. children: [
  238. Text(
  239. name?? "Home",
  240. style: TextStyle(color: Colors.white, fontSize: 16),
  241. ),
  242. ],
  243. ),
  244. )
  245. ],
  246. );
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement