Created
July 29, 2020 04:07
-
-
Save Maadhav/2926541f8de8cc566632f1dda5e230eb to your computer and use it in GitHub Desktop.
Hide title in Flutter SliverAppBar 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
class SABT extends StatefulWidget { | |
final Widget child; | |
const SABT({ | |
Key key, | |
@required this.child, | |
}) : super(key: key); | |
@override | |
_SABTState createState() { | |
return new _SABTState(); | |
} | |
} | |
class _SABTState extends State<SABT> { | |
ScrollPosition _position; | |
bool _visible; | |
@override | |
void dispose() { | |
_removeListener(); | |
super.dispose(); | |
} | |
@override | |
void didChangeDependencies() { | |
super.didChangeDependencies(); | |
_removeListener(); | |
_addListener(); | |
} | |
void _addListener() { | |
_position = Scrollable.of(context)?.position; | |
_position?.addListener(_positionListener); | |
_positionListener(); | |
} | |
void _removeListener() { | |
_position?.removeListener(_positionListener); | |
} | |
void _positionListener() { | |
final FlexibleSpaceBarSettings settings = | |
context.dependOnInheritedWidgetOfExactType(); | |
print(settings.minExtent); | |
bool visible = settings == null || settings.currentExtent > settings.minExtent+10; | |
if (_visible != visible) { | |
setState(() { | |
_visible = visible; | |
}); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return AnimatedOpacity( | |
duration: Duration(milliseconds: 300), | |
opacity: _visible?1:0, | |
child: widget.child, | |
); | |
} | |
} |
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
SliverAppBar( | |
pinned: true, | |
expandedHeight: 300, | |
excludeHeaderSemantics: true, | |
flexibleSpace: FlexibleSpaceBar( | |
collapseMode: CollapseMode.pin, | |
background: Container( | |
decoration: BoxDecoration( | |
image: DecorationImage( | |
image: AssetImage("assets/images/cardimage.png"), | |
fit: BoxFit.fitHeight)), | |
centerTitle: true, | |
titlePadding: EdgeInsets.symmetric(horizontal: 20), | |
title: SABT(child: Text( | |
'Watch: Gameplay for the first 13 games optimised for Xbox Series X', | |
style: TextStyle( | |
fontFamily: 'League', | |
fontSize: 19, | |
color: Colors.white, | |
), | |
),), | |
), | |
), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment