Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a Scrollbar to a CustomScollView

Tags:

flutter

dart

I've been trying to figure out how to add a Scrollbar Widget to a CustomScrollView. One would usually just have to wrap a ListView or ScrollView with a Scrollbar Widget, pass in the same ScrollController and boom, it's rendered.

Here is a minimal code block.

CustomScrollView(
            slivers: [
              SliverAppBar(
                title: Hero(
                  tag: 'title',
                  child: RichText(
                    text: TextSpan(
                      children: [
                        TextSpan(
                          text: 'Not',
                          style: TextStyle( fontSize: 20, fontWeight: FontWeight.w500),
                        ),
                        TextSpan(
                          text: '3s',
                          style: TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
                        )
                      ],
                    ),
                  ),
                ),
              ),
              SliverList(
                delegate: SliverChildBuilderDelegate(
                  (context, index) {
                    return Container(); })));
like image 321
Adeolaex Avatar asked Jul 20 '20 04:07

Adeolaex


1 Answers

I got it.

Create a _scrollController.

Wrap the CustomScrollView() in a ScrollBar() Widget.

Set the controller: parameter of BOTH CustomerScrollView and ScrollBar as the _scrollController.

final _scrollController = ScrollController();
return Scaffold(
  body: Scrollbar(
    controller: _scrollController,
    isAlwaysShown: true,
    child: CustomScrollView(
      controller: _scrollController,
      slivers: <Widget> [
        ...

Modify as you need.

like image 118
Andrew Young-Min Cho Avatar answered Nov 10 '22 01:11

Andrew Young-Min Cho