Created
November 29, 2021 15:06
-
-
Save berkanaslan/ddc31047f5b3cce44c717958494d491f to your computer and use it in GitHub Desktop.
You can control the visibility depends on scroll position.
This file contains 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
class VisibilityController extends StatefulWidget { | |
final Widget child; | |
const VisibilityController({Key? key, required this.child}) : super(key: key); | |
@override | |
_VisibilityControllerState createState() { | |
return _VisibilityControllerState(); | |
} | |
} | |
class _VisibilityControllerState extends State<VisibilityController> { | |
ScrollPosition? scrollPosition; | |
bool isVisible = true; | |
@override | |
void dispose() { | |
removeListener(); | |
super.dispose(); | |
} | |
@override | |
void didChangeDependencies() { | |
super.didChangeDependencies(); | |
removeListener(); | |
addListener(); | |
} | |
void addListener() { | |
scrollPosition = Scrollable.of(context)?.position; | |
scrollPosition?.addListener(positionListener); | |
// positionListener(); | |
} | |
void removeListener() { | |
scrollPosition?.removeListener(positionListener); | |
} | |
void positionListener() { | |
final FlexibleSpaceBarSettings? settings = context.dependOnInheritedWidgetOfExactType<FlexibleSpaceBarSettings>(); | |
bool visible = true; | |
if (settings != null) visible = settings.minExtent != settings.currentExtent; | |
if (isVisible != visible) setState(() => isVisible = visible); | |
} | |
@override | |
Widget build(BuildContext context) => Visibility(visible: isVisible, child: widget.child); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment