class YourPage extends StatelessWidget {
const YourPage({Key key}) : super(key: key);
ScrollController _scrollController;
bool _isScrolled = false;
@override
void initState() {
_scrollController = ScrollController();
_scrollController.addListener(_listenToScrollChange);
super.initState();
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: CustomScrollView(
controller: _scrollController,
slivers: <Widget>[
SliverAppBar(
expandedHeight: 144.0,
pinned: true,
forceElevated: true,
////////////////////////////////////
// HERE IS THE PART TO BE ADDED
title: AnimatedOpacity(
duration: Duration(milliseconds: 300),
opacity: _isScrolled ? 1.0 : 0.0,
curve: Curves.ease,
child: Text("YOUR TITLE HERE"),
),
////////////////////////////////////
flexibleSpace: FlexibleSpaceBar(
titlePadding: const EdgeInsets.only(bottom: 8.0),
centerTitle: true,
collapseMode: CollapseMode.parallax,
background: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 16.0),
padding: EdgeInsets.only(left: 32.0, right: 32.0),
child: Text(
'Some text',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontFamily: 'PlayfairDisplay',
fontStyle: FontStyle.italic,
fontSize: 16.0),
)),
Container(
margin: EdgeInsets.only(top: 16.0),
padding: EdgeInsets.only(left: 32.0, right: 32.0),
child: Text(
'some text',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontFamily: 'PlayfairDisplay',
fontSize: 16.0),
)),
],
),
),
)
],
),
),
);
}
// METHODS
void _listenToScrollChange() {
if (_scrollController.offset >= 48.0) {
setState(() {
_isScrolled = true;
});
} else {
setState(() {
_isScrolled = false;
});
}
}
}
i want to collapse the appbar using the less setState because due to multiple calls it gets laggy. also i want to add some animation while changing collapsed appbar color to white. i tried to add animated color but it dosent change the color on specific offset ...i hope you get my point.. thanks in advance.....
question from:
https://stackoverflow.com/questions/65889287/flexible-appbar-colapsing-smoothly 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…