I have a floating action button at the right bottom of a SingleChildScrollView
that I will like to disappear when scrolling down and appear when scrolling up like the attached gif file :
My code is below and will appear with any suggestion
final dataKey = new GlobalKey();
initState(){
super.initState();
_isVisible = true;
_hideButtonController = new ScrollController();
_hideButtonController.addListener((){
if(_hideButtonController.position.userScrollDirection == ScrollDirection.reverse){
if(_isVisible == true) {
/* only set when the previous state is false
* Less widget rebuilds
*/
print("**** ${_isVisible} up"); //Move IO away from setState
setState((){
_isVisible = false;
});
}
} else {
if(_hideButtonController.position.userScrollDirection == ScrollDirection.forward){
if(_isVisible == false) {
/* only set when the previous state is false
* Less widget rebuilds
*/
print("**** ${_isVisible} down"); //Move IO away from setState
setState((){
_isVisible = true;
});
}
}
}});
}
floatingActionButton: new Visibility(
visible: _isVisible,
child: new FloatingActionButton(backgroundColor: colorBlue,
onPressed: () => Scrollable.ensureVisible(dataKey.currentContext,duration: Duration(seconds: 1)),
child: Icon(Icons.arrow_upward),)),
body:SingleChildScrollView(
key: dataKey,
physics: BouncingScrollPhysics(),
controller: _hideButtonController,
)
As you can see that I have key: dataKey,
that simply automatically scrolls to the top of the page when click you can see I tried using Visibility
but it didn't work for me, and not sure what I did wrong but I will like the FAB to appear and disappear as shown in the attached GIF. Thanks in advance.