Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a non-sliver widget in a CustomScrollView

Tags:

flutter

dart

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.

like image 628
Joseph Arriaza Avatar asked Feb 05 '19 19:02

Joseph Arriaza


People also ask

How do I use CustomScrollView?

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.

What is slivers widget flutter?

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.

What is SliverToBoxAdapter in flutter?

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.


2 Answers

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,
            )
          ],
        ),
      )
    ],
  ),
);

this is the output of the above code

like image 172
Dhanraj Verma Avatar answered Oct 17 '22 10:10

Dhanraj Verma


The best answer is not correct, it causes assertion padding == null. @blaneyneil wrote the right solution: use to SliverToBoxAdapter.

like image 30
Dmitriy Blokhin Avatar answered Oct 17 '22 11:10

Dmitriy Blokhin