Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Flutter widget to return two widgets for use with CustomScrollView / slivers

I have a Flutter CustomScrollView with the following structure:

body: CustomScrollView(
  slivers: <Widget>[
    MyCustomTitle(...),
    MyCustomSliverGrid(...)
    MyCustomTitle(...),
    MyCustomSliverGrid(...)
    MyCustomTitle(...),
    MyCustomSliverGrid(...)
    ...
  ],
),

MyCustomTitle extends StatelessWidget and in the build method returns a SliverToBoxAdapter widget, and the MyCustomSliverGrid widget extends StatelessWidget and in the build method returns a SliverGrid widget.

How can I implement a single Widget (MyCustomSliverGridWithTitle) that returns both the custom title and the custom SliverGrid? From the build method of a StatelessWidget I can only return a single Widget, not two.

like image 646
Alex Vang Avatar asked May 11 '26 15:05

Alex Vang


1 Answers

You can bundle MyCustomTitle and MyCustomSliverGrid into one widget with MultiSliver.

class MyCustomSliverGridWithTitle extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiSliver(
      children: <Widget>[
        MyCustomTitle(...),
        MyCustomSliverGrid(...),
      ],
    );
  }
}
like image 76
Till Friebe Avatar answered May 14 '26 09:05

Till Friebe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!