Last active
May 9, 2022 12:27
-
-
Save KammererTob/40a971bb5635f1655be0271fa8e96d61 to your computer and use it in GitHub Desktop.
Hide button on scroll
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'; | |
import 'package:flutter/rendering.dart'; | |
const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith( | |
scaffoldBackgroundColor: darkBlue, | |
), | |
debugShowCheckedModeBanner: false, | |
home: AppView2(), | |
); | |
} | |
} | |
class LogoHidingScrollView extends StatefulWidget { | |
const LogoHidingScrollView({ | |
Key? key, | |
}) : super(key: key); | |
_LogoHidingScrollViewState createState() => _LogoHidingScrollViewState(); | |
} | |
class _LogoHidingScrollViewState extends State<LogoHidingScrollView> { | |
final ScrollController _scrollController = ScrollController(); | |
bool _isVisible = true; | |
void initState() { | |
_isVisible = true; | |
_scrollController.addListener(() { | |
if (_scrollController.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 (_scrollController.position.userScrollDirection == | |
ScrollDirection.forward) { | |
if (_isVisible == false) { | |
setState(() { | |
_isVisible = true; | |
}); | |
} | |
} | |
} | |
}); | |
super.initState(); | |
} | |
Widget build(BuildContext context) { | |
return SizedBox( | |
height: 500, | |
child: Stack( | |
children: [ | |
CustomScrollView( | |
controller: _scrollController, | |
scrollDirection: Axis.horizontal, | |
shrinkWrap: true, | |
slivers: <Widget>[ | |
SliverPadding( | |
padding: const EdgeInsets.all(20.0), | |
sliver: SliverList( | |
delegate: SliverChildListDelegate( | |
<Widget>[ | |
FlutterLogo( | |
size: 600, | |
) | |
], | |
), | |
), | |
), | |
], | |
), | |
Visibility( | |
visible: _isVisible, | |
child: Align( | |
alignment: Alignment.centerRight, | |
child: FloatingActionButton( | |
child: const Icon(Icons.arrow_forward_ios), onPressed: () {}), | |
), | |
) | |
], | |
), | |
); | |
} | |
} | |
class AppView2 extends StatefulWidget { | |
const AppView2({ | |
Key? key, | |
}) : super(key: key); | |
@override | |
_AppViewState createState() => _AppViewState(); | |
} | |
class _AppViewState extends State<AppView2> { | |
late ScrollController _hideButtonController; | |
bool _isVisible = true; | |
@override | |
void initState() { | |
_isVisible = true; | |
_hideButtonController = ScrollController(); | |
_hideButtonController.addListener(() { | |
if (_hideButtonController.positions | |
.any((pos) => pos.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.positions | |
.any((pos) => pos.userScrollDirection == ScrollDirection.forward)) { | |
if (_isVisible == false) { | |
setState(() { | |
_isVisible = true; | |
}); | |
} | |
} | |
} | |
}); | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return SafeArea( | |
child: Scaffold( | |
appBar: AppBar( | |
title: Text("Example app bar"), | |
), | |
body: CustomScrollView(slivers: <Widget>[ | |
SliverList( | |
delegate: SliverChildBuilderDelegate( | |
(BuildContext context, int index) { | |
return const LogoHidingScrollView(); | |
}, childCount: 10), | |
) | |
]))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment