@@ -64,32 +64,153 @@ class AdvancedDrawer extends StatefulWidget {
6464}
6565
6666class _AdvancedDrawerState extends State <AdvancedDrawer >
67- with SingleTickerProviderStateMixin {
68- late final AdvancedDrawerController _controller;
69- late final AnimationController _animationController;
70- late final Animation <double > _drawerScaleAnimation;
71- late final Animation <Offset > _childSlideAnimation;
72- late final Animation <double > _childScaleAnimation;
73- late final Animation <Decoration > _childDecorationAnimation;
67+ with TickerProviderStateMixin {
68+ final _spareController = AdvancedDrawerController ();
69+
70+ late AnimationController _spareAnimationController;
71+ late AnimationController _animationController;
72+
73+ late Animation <double > _drawerScaleAnimation;
74+ late Animation <Offset > _childSlideAnimation;
75+ late Animation <double > _childScaleAnimation;
76+ late Animation <Decoration > _childDecorationAnimation;
77+
7478 late double _offsetValue;
7579 late Offset _freshPosition;
80+
7681 bool _captured = false ;
7782 Offset ? _startPosition;
7883
7984 @override
8085 void initState () {
8186 super .initState ();
8287
83- _controller = widget.controller ?? AdvancedDrawerController ();
84- _controller.addListener (_handleControllerChanged);
88+ _initControllers ();
89+ }
90+
91+ @override
92+ void didUpdateWidget (covariant AdvancedDrawer oldWidget) {
93+ _initControllers ();
94+
95+ super .didUpdateWidget (oldWidget);
96+ }
97+
98+ @override
99+ Widget build (BuildContext context) {
100+ return Material (
101+ color: widget.backdropColor,
102+ child: GestureDetector (
103+ onHorizontalDragStart:
104+ widget.disabledGestures ? null : _handleDragStart,
105+ onHorizontalDragUpdate:
106+ widget.disabledGestures ? null : _handleDragUpdate,
107+ onHorizontalDragEnd: widget.disabledGestures ? null : _handleDragEnd,
108+ onHorizontalDragCancel:
109+ widget.disabledGestures ? null : _handleDragCancel,
110+ child: Container (
111+ color: Colors .transparent,
112+ child: Stack (
113+ children: [
114+ Align (
115+ alignment: widget.rtlOpening
116+ ? Alignment .centerRight
117+ : Alignment .centerLeft,
118+ child: FractionallySizedBox (
119+ widthFactor: widget.openRatio,
120+ child: ScaleTransition (
121+ scale: _drawerScaleAnimation,
122+ alignment: widget.rtlOpening
123+ ? Alignment .centerLeft
124+ : Alignment .centerRight,
125+ child: RepaintBoundary (
126+ child: widget.drawer,
127+ ),
128+ ),
129+ ),
130+ ),
131+ SlideTransition (
132+ position: _childSlideAnimation,
133+ textDirection:
134+ widget.rtlOpening ? TextDirection .rtl : TextDirection .ltr,
135+ child: ScaleTransition (
136+ scale: _childScaleAnimation,
137+ child: Builder (
138+ builder: (_) {
139+ final childStack = Stack (
140+ children: [
141+ RepaintBoundary (child: widget.child),
142+ ValueListenableBuilder <AdvancedDrawerValue >(
143+ valueListenable: _controller,
144+ builder: (_, value, __) {
145+ if (! value.visible) {
146+ return const SizedBox ();
147+ }
148+
149+ return Material (
150+ color: Colors .transparent,
151+ child: InkWell (
152+ onTap: _controller.hideDrawer,
153+ highlightColor: Colors .transparent,
154+ child: Container (),
155+ ),
156+ );
157+ },
158+ ),
159+ ],
160+ );
161+
162+ if (widget.animateChildDecoration &&
163+ widget.childDecoration != null ) {
164+ return AnimatedBuilder (
165+ animation: _childDecorationAnimation,
166+ builder: (_, child) {
167+ return Container (
168+ clipBehavior: Clip .antiAlias,
169+ decoration: _childDecorationAnimation.value,
170+ child: child,
171+ );
172+ },
173+ child: childStack,
174+ );
175+ }
176+
177+ return Container (
178+ clipBehavior: widget.childDecoration != null
179+ ? Clip .antiAlias
180+ : Clip .none,
181+ decoration: widget.childDecoration,
182+ child: childStack,
183+ );
184+ },
185+ ),
186+ ),
187+ ),
188+ ],
189+ ),
190+ ),
191+ ),
192+ );
193+ }
194+
195+ AdvancedDrawerController get _controller {
196+ return widget.controller ?? _spareController;
197+ }
198+
199+ void _initControllers () {
200+ _controller
201+ ..removeListener (_handleControllerChanged)
202+ ..addListener (_handleControllerChanged);
203+
204+ _spareAnimationController = AnimationController (
205+ vsync: this ,
206+ value: _controller.value.visible ? 1 : 0 ,
207+ );
85208
86- _animationController = widget.animationController ??
87- AnimationController (
88- vsync: this ,
89- value: _controller.value.visible ? 1 : 0 ,
90- );
209+ _animationController =
210+ widget.animationController ?? _spareAnimationController;
91211
92- _animationController.duration = widget.animationDuration;
212+ _animationController.reverseDuration =
213+ _animationController.duration = widget.animationDuration;
93214
94215 final parentAnimation = widget.animationCurve == null
95216 ? _animationController
@@ -119,101 +240,6 @@ class _AdvancedDrawerState extends State<AdvancedDrawer>
119240 ).animate (parentAnimation);
120241 }
121242
122- @override
123- Widget build (BuildContext context) {
124- return Material (
125- color: widget.backdropColor,
126- child: IgnorePointer (
127- ignoring: widget.disabledGestures,
128- child: GestureDetector (
129- onHorizontalDragStart: _handleDragStart,
130- onHorizontalDragUpdate: _handleDragUpdate,
131- onHorizontalDragEnd: _handleDragEnd,
132- onHorizontalDragCancel: _handleDragCancel,
133- child: Container (
134- color: Colors .transparent,
135- child: Stack (
136- children: [
137- Align (
138- alignment: widget.rtlOpening
139- ? Alignment .centerRight
140- : Alignment .centerLeft,
141- child: FractionallySizedBox (
142- widthFactor: widget.openRatio,
143- child: ScaleTransition (
144- scale: _drawerScaleAnimation,
145- alignment: widget.rtlOpening
146- ? Alignment .centerLeft
147- : Alignment .centerRight,
148- child: widget.drawer,
149- ),
150- ),
151- ),
152- SlideTransition (
153- position: _childSlideAnimation,
154- textDirection:
155- widget.rtlOpening ? TextDirection .rtl : TextDirection .ltr,
156- child: ScaleTransition (
157- scale: _childScaleAnimation,
158- child: Builder (
159- builder: (_) {
160- final childStack = Stack (
161- children: [
162- widget.child,
163- ValueListenableBuilder <AdvancedDrawerValue >(
164- valueListenable: _controller,
165- builder: (_, value, __) {
166- if (! value.visible) {
167- return const SizedBox ();
168- }
169-
170- return Material (
171- color: Colors .transparent,
172- child: InkWell (
173- onTap: _controller.hideDrawer,
174- highlightColor: Colors .transparent,
175- child: Container (),
176- ),
177- );
178- },
179- ),
180- ],
181- );
182-
183- if (widget.animateChildDecoration &&
184- widget.childDecoration != null ) {
185- return AnimatedBuilder (
186- animation: _childDecorationAnimation,
187- builder: (_, child) {
188- return Container (
189- clipBehavior: Clip .antiAlias,
190- decoration: _childDecorationAnimation.value,
191- child: child,
192- );
193- },
194- child: childStack,
195- );
196- }
197-
198- return Container (
199- clipBehavior: widget.childDecoration != null
200- ? Clip .antiAlias
201- : Clip .none,
202- decoration: widget.childDecoration,
203- child: childStack,
204- );
205- },
206- ),
207- ),
208- ),
209- ],
210- ),
211- ),
212- ),
213- ),
214- );
215- }
216-
217243 void _handleControllerChanged () {
218244 _controller.value.visible
219245 ? _animationController.forward ()
@@ -266,15 +292,11 @@ class _AdvancedDrawerState extends State<AdvancedDrawer>
266292
267293 @override
268294 void dispose () {
269- _controller.removeListener (_handleControllerChanged);
295+ _spareController
296+ ..removeListener (_handleControllerChanged)
297+ ..dispose ();
270298
271- if (widget.controller == null ) {
272- _controller.dispose ();
273- }
274-
275- if (widget.animationController == null ) {
276- _animationController.dispose ();
277- }
299+ _spareAnimationController.dispose ();
278300
279301 super .dispose ();
280302 }
0 commit comments