I am learning sliverapp bar….
here i have used customscroll view and i want three things
-
SliverAppBar for showing title ‘Transactions’
-
** SliverPersistentHeader**. for selecting based on switch value(either all or only positive numbers)
3)SliverList. to display all filtered data
I am facing problem in Switch’s onChange method..it does not change state of showAll variable eventhought i have applied setState..
Widget build(BuildContext context) {
return SafeArea(
child:Scaffold(
body: CustomScrollView(
slivers: [
//sliver appbar
SliverAppBar(
expandedHeight: 300,
flexibleSpace: FlexibleSpaceBar(
title: Text('All'+showAll.toString()),
),
pinned: true,
floating: true,
),
SliverPersistentHeader(
delegate: _PinnedHeaderDelegate(child: Container(
height: 50,
color: Colors.white,
child: Row(children: [
Text('Show All Data'),
Switch(
value: showAll,
onChanged: (val) {
print(val);
setState(() {
showAll = val;
});
}),
],),
)),
pinned: true,
),
//sliver list
buildShowData(),
],
),
),
);
}
buildShowData() {
List<int> data;
if(showAll==true)
data=numbers;
else
data=numbers.where((element) => element>0).toList();
return SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
final number=data[index];
return Card(
child: ListTile(
title: Text('$number',style: TextStyle(
color: number<0?Colors.red:Colors.green
),),
),
);
},
childCount: data.length,
),
);
}
}
2
Answers
send your code of _PinnedHeaderDelegate class
hope shouldRebuild is returning true…
Make sure that in _PinnedHeaderDelegate class shouldRebuild returns true
Output :
Switch is off :
Switch is on :