Created
October 12, 2024 12:41
-
-
Save Heilum/ebe5a03cc9d65cdef561a9befb841cc8 to your computer and use it in GitHub Desktop.
HideableBottomWidgetWhenScrolling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class HideableWidgetController { | |
_HideableBottomWidgetWhenScrollingState? _state; | |
// Internal method to attach the state | |
void _addState(_HideableBottomWidgetWhenScrollingState state) { | |
_state = state; | |
} | |
// Internal method to detach the state | |
void _removeState() { | |
_state = null; | |
} | |
// Method to show the widget | |
void show() { | |
_state?.show(); | |
} | |
// Method to hide the widget | |
void hide() { | |
_state?.hide(); | |
} | |
} | |
class HideableBottomWidgetWhenScrolling extends StatefulWidget { | |
final HideableWidgetController controller; | |
final Widget child; | |
final Duration duration; | |
final Curve curve; | |
final bool initiallyVisible; | |
const HideableBottomWidgetWhenScrolling({ | |
super.key, | |
required this.controller, | |
required this.child, | |
this.duration = const Duration(milliseconds: 300), | |
this.curve = Curves.easeInOut, | |
this.initiallyVisible = true, | |
}); | |
@override | |
_HideableBottomWidgetWhenScrollingState createState() => | |
_HideableBottomWidgetWhenScrollingState(); | |
} | |
class _HideableBottomWidgetWhenScrollingState | |
extends State<HideableBottomWidgetWhenScrolling> | |
with SingleTickerProviderStateMixin { | |
late AnimationController _animationController; | |
late Animation<Offset> _offsetAnimation; | |
@override | |
void initState() { | |
super.initState(); | |
// Initialize the AnimationController with custom duration | |
_animationController = AnimationController( | |
duration: widget.duration, | |
vsync: this, | |
); | |
// Set initial visibility | |
if (!widget.initiallyVisible) { | |
_animationController.value = 1.0; // Hidden position | |
} | |
// Initialize the animation with custom curve | |
_offsetAnimation = Tween<Offset>( | |
begin: const Offset(0, 0), | |
end: const Offset(0, 1), | |
).animate( | |
CurvedAnimation( | |
parent: _animationController, | |
curve: widget.curve, | |
), | |
); | |
// Listen to the controller's visibility changes | |
widget.controller._addState(this); | |
} | |
@override | |
void dispose() { | |
widget.controller._removeState(); | |
_animationController.dispose(); | |
super.dispose(); | |
} | |
// Methods to show and hide the widget | |
void show() { | |
_animationController.reverse(); | |
} | |
void hide() { | |
_animationController.forward(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return SlideTransition( | |
position: _offsetAnimation, | |
child: widget.child, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment