PageView widget in vertical wrapped in Scrollbar Widget

1,157

You have to pass the controller for the Scrollbar widget with the same controller of PageView widget.

  • Define a PageController
    • final PageController pageController = PageController(initialPage: 0);
      
  • Pass the pageController to the controller property of Scrollbar widget.
    • return Scrollbar(
         controller: pageController,
         isAlwaysShown: true, // This forces the scrollbar to be visible always
         child: PageView(
           scrollDirection: Axis.vertical,
           controller: pageController,
      

You can set other properties like thickness and hoverthickness to addjust the thickness of the scroll bar. Refer this youtube video and documentation for more details.

A working demo. Also available as a dartpad here:

/// Flutter code sample for PageView

// Here is an example of [PageView]. It creates a centered [Text] in each of the three pages
// which scroll horizontally.

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

/// This is the main application widget.
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyStatelessWidget(),
      ),
    );
  }
}

/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final PageController pageController = PageController(initialPage: 0);
    return Scrollbar(
      controller: pageController,
      isAlwaysShown: true,
      thickness: 10,
      showTrackOnHover: true,
      hoverThickness: 15,
      radius: Radius.circular(0),
      child: PageView(
        scrollDirection: Axis.vertical,
        controller: pageController,
        children: const <Widget>[
          Center(
            child: Text('First Page'),
          ),
          Center(
            child: Text('Second Page'),
          ),
          Center(
            child: Text('Third Page'),
          )
        ],
      ),
    );
  }
}

Share:
1,157
GhoSt
Author by

GhoSt

Updated on December 29, 2022

Comments

  • GhoSt
    GhoSt over 1 year

    I'm trying to implement a list of image arranged in a column vertically. I used PageView Widget to make use of the PageChange listener to indicate what current page I currently am. However I am implementing this in WebView as well so I need to wrap it in ScrollBar. Although the Scrollbar is showing and moving accordingly, it is not draggable or not automatically scrolling if I used the ScrollBar. Is theres a work around this or a solution?

  • GhoSt
    GhoSt almost 3 years
    Kudos to you! Thank you you saved me! Keep safe!