I am trying to add a Text widget into CustomScrollView but I got issues like the target is not the same.
This is my widget:
@override
Widget build(BuildContext context) {
final double statusBarHeight = MediaQuery.of(context).padding.top;
return Scaffold(
key: scaffoldKey,
body: CustomScrollView(
semanticChildCount: 2,
slivers: <Widget>[
_buildAppBar(context, statusBarHeight),
Text('test')
],
));
}
The _buildAppBar method returns a SliverAppBar.
I need to use a Padding widget instead of the text, but I think that it will be like the same, that's the same issue.
Now lets start implementation of CustomScrollView in Flutter First of all, create a basic project and return a Custom Scroll View widget. Then, add a silver app bar and then add a silver grid. Here we only used two child widgets in the custom scroll view. The Entry point of code.
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.
A sliver that contains a single box widget. Slivers are special-purpose widgets that can be combined using a CustomScrollView to create custom scroll effects. A SliverToBoxAdapter is a basic sliver that creates a bridge back to one of the usual box-based widgets.
I found a better way to use non-slivers inside a CustomScrollView, use SliverToBoxAdapter widget. Give your non-sliver widget as child of SliverToBoxAdapter widget and your work done.
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverToBoxAdapter(
child: Stack(
children: <Widget>[
Container(
height: 200,
width: 200,
color: Colors.green,
),
Positioned(
child: Container(color: Colors.yellow),
top: 50,
left: 50,
)
],
),
)
],
),
);
The best answer is not correct, it causes assertion padding == null
.
@blaneyneil wrote the right solution: use to SliverToBoxAdapter.
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