Slivers in Flutter - Part (4)
SliverOpacity, SliverAnimatedOpacity, SliverFadeTransition, SliverVisibility, SliverIgnorePointer, SliverSafeArea.
- This is Part - 4 of Slivers in Flutter series. Make sure you check the first 3 parts.
- In this article, I'm going to explain some of the useful
Sliver
widgets, which are - SliverOpacity, SliverAnimatedOpacity, SliverFadeTransition, SliverVisibility, SliverIgnorePointer, SliverSafeArea.
SliverOpacity
- The
SliverOpacity
makessliver
widget transparent. - This works the same as the
Opacity
widget works forBox
widgets. The difference is this only works on theSliver
widgets. - It usually takes values between
0.0
to1.0
. - For the value 0.0, the sliver child is simply not painted at all. For the value 1.0, the sliver child is painted immediately.
- For values of
opacity
other than0.0
and1.0
, this class is relatively expensive because it requires painting the sliver child into an intermediate buffer. - Example :
- Output
SliverAnimatedOpacity
- If you see in the output of the above example, The opacity is directly jumping from
no visibility
tofull visiblity
. It's not looking smooth. - To do the smooth transition between
0.0
to1.0
values we have to useSliverAnimatedOpacity
. SliverAnimatedOpacity
is an animated version ofSliverOpacity
which automatically transitions the sliver child's opacity over a givenduration
whenever the given opacity changes.- Example Output
SliverFadeTransition
- This also animates the
opacity
of thesliver
widget. The only difference is it takesAnimation<double>
value instead ofdouble
values. - Example Output :
- Here I've used
Curves.fastOutSlowIn
animation. You can use any one of the curves defined in the below class:
SliverVisibility
SliverVisibility
is the same as theVisibility
widget except it takessliver
as a child.- It is used to show or hide a sliver child. When the
sliver
is not visible thereplacementSliver
is included instead. replacementSliver
is used when thesliver
child is not visible, assuming that none of themaintain
flags (in particular,maintainState
) are set.SliverVisibility
has different parameters like :- maintainAnimation : It is used to determine whether to maintain
animations
within the sliver subtree when it is not visible. - maintainInteractivity: It is used to determine whether to allow the sliver to be interactive when hidden.
- maintainSemantics: It is used to determine whether to maintain the semantics for the sliver when it is hidden (e.g. for accessibility).
- maintainSize: It is used to determine whether to maintain space for where the sliver would have been.
- maintainState: It is used to determine whether to maintain the State objects of the sliver subtree when it is not visible.
- replacementSliver: Widget defined here is visible when sliver is not visible.
- Example: Output:
SliverIgnorePointer
- When you want your sliver to ignore the tapping or any kind of interaction then simply wrap your
sliver
widget insideSliverIgnorePointer
. - It will disable the interaction of that widget.
- When
ignoring
property istrue
, the widget is not interactable. And whenignoringSemantics
property is true, the subtree will be invisible to the semantics layer - Example: Output:
SliverSafeArea
- If you've used
SafeArea
then,SliverSafeArea
is also doing the same thing except it takessliver
as a child instead ofBox
widget. - For example, this will indent the sliver by enough to avoid the status bar at the top of the screen.
- It will also indent the sliver by the amount necessary to avoid The Notch on the iPhone X or other similar creative physical features of the display. Output:
Wrapping Up
- This is the 4th part of the
Slivers In Flutter
series. Check previous articles for more information about Slivers. - If you liked this article make sure to give a thumbs up π. Also share it with others if you found this helpfulπ.
- See you in the next article. Until then..
Β