I'm new guy using the flutter and when I code my app I met a problem that how could I set SliverAppBar collapsed status by default. To solve my confuse, I check the flutter's doc, unfortunately I couldn't find the solution, so I'm here.
Below is my code, if you know how to solve it, help me plz. Thanks guys.
class CollapsingBarLayoutState extends State<CollapsingBarLayout>
with TickerProviderStateMixin {
final List<ListItem> listData = [];
@override
Widget build(BuildContext context) {
for (int i = 0; i < 20; i++) {
listData.add(new ListItem("$i", Icons.cake));
}
return Scaffold(
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
expandedHeight: 200.0,
pinned: true,
centerTitle: false,
snap: true,
floating: true,
titleSpacing: 0,
backgroundColor: Colors.green,
flexibleSpace: FlexibleSpaceBar(
centerTitle: true,
title: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: _buildBriefCalendarChildren(),
),
),
background: GridPage(
children: _buildCalendarPageChildren(),
column: 7,
row: 5,
)),
),
];
},
body: Center(
child: new ListView.builder(
itemBuilder: (BuildContext context, int index) {
return new ListItemWidget(listData[index]);
},
itemCount: listData.length,
),
),
),
);
}
}
In Android the CollapsingToolbar UI component is available within the design-support library, while in iOS there is no official UI component, but there are libraries that help us to do the same. In order to do the same in Flutter, we need to use the Widget called SliverAppBar together with FlexibleSpaceBar as a child.
The thing is When it's expanded, the title and icons (leading and actions) should be white in order to be seen correctly, and when it's collapsed, they should be changed to black.
SliverAppBar is a Material Design widget in flutter which gives scrollable or collapsible app-bar. The word Sliver is given to scrollable areas here. SliverAppBar basically gives us means to create an app-bar that can change appearance, blend in the background, or even disappear as we scroll.
There is a good plugin https://pub.dev/packages/after_layout I used in a situation like this. Just add a mixin and then the only method you would need to implement would look something like this:
void afterFirstLayout(BuildContext context) {
_scrollController //scroll controller of your list view under sliver
.animateTo((expandedHeight) - collapsedHeight,
duration: new Duration(milliseconds: 1),//0 dont work as duration
curve: Curves.linear)
.then((_) {
setState(() {
expandedPercentage = 0; //just my custom tracking of expansion percentage
});
});
}
Edit - better solution:
Initialise a new ScrollController(initialScrollOffset: expandedHeight - collapsedHeight);
and pass it as a controller
to your NestedScrollView
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With