skip to Main Content

I have created some items in SingleChildScrollView and the problem is that none of the items are scrolled in horizontal mode, but the same code scrolls the items in vertical mode.

Items are scrollable when scrollDirection: Axis.vertical

It can be scrolled horizontally on the Android platform, but it cannot be scrolled on the web and Windows form.

Scaffold(
  appBar: CustomAppBar(),
  body: RawScrollbar(
    thumbVisibility: true,
    child: SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      physics: AlwaysScrollableScrollPhysics(),
      child: Row(
        children: List.generate(
            50,
            (index) => Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Container(
                      height: 50,
                      color: Colors.blue,
                      child: Center(child: Text('Item $index'))),
                )),
      ),
    ),
  ));

2

Answers


  1. Chosen as BEST ANSWER
    return MaterialApp(
            scrollBehavior: const MaterialScrollBehavior().copyWith(
              dragDevices: {
                PointerDeviceKind.mouse,
                PointerDeviceKind.touch,
                PointerDeviceKind.stylus,
                PointerDeviceKind.unknown,
                PointerDeviceKind.invertedStylus,
                PointerDeviceKind.trackpad
              },
            ),
            home: ...);
    

  2. To make your items scrollable horizontally across all platforms (including web and desktop), you can use ScrollConfiguration.

    The problem occurs because, on platforms like the web and Windows, the default behavior may only listen for specific input types. By explicitly specifying the devices that can control the scroll, you ensure it works across various platforms.

    Here’s how you can adjust your code:

    ScrollConfiguration(
      behavior: ScrollConfiguration.of(context).copyWith(
        // Allow scrolling with both touch and mouse devices
        dragDevices: {
          PointerDeviceKind.touch,  // For touch-based devices (e.g., mobile)
          PointerDeviceKind.mouse,  // For mouse-based devices (e.g., desktop)
        },
      ),
      child: ScrollableWidget(), // Replace with your SingleChildScrollView or other scrollable widget
    )
    

    Bonus Tip: To enable scrolling using the scroll thumb (the scrollbar handle), you can define a ScrollController and set it for both the RawScrollbar and the SingleChildScrollView.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search