Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollbar is not draggable

I can see the scrollbar in the ListView, and I can scroll the ListView. But the problem is that I can not use/drag the scrollbar to scroll the ListView. It just appears as an indicator and doesn't respond touches/gestures.

Is this the expected behavior of the ScrollBar or am I doing something wrong? If so, how can I achieve that natively (without using a package, or do I have to)?

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  final ScrollController _scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Scrollbar(
            isAlwaysShown: true,
            controller: _scrollController,
            child: ListView.builder(
                controller: _scrollController,
                itemCount: 100,
                itemBuilder: (context, index) {
                  return Card(
                      child: ListTile(
                    title: Text("Item: ${index + 1}"),
                  ));
                }),
          ),
        ),
      ),
    );
  }
}
like image 824
kozhioyrin Avatar asked May 12 '26 15:05

kozhioyrin


2 Answers

I found two simple ways to make the Scrollbar draggable (based on Flutter 3.0) without importing a 3rd-party plug-in: A: In Scrollbar settings, add

interactive: true,

B: Replace Scrollbar with CupertinoScrollbar. For whatever reason, the CupertinoScrollbar's default behavior appears to be exactly what you're looking for, i.e. having a draggable scrollbar that serves more than just being an axis indicator.

like image 161
TriMontana Avatar answered May 15 '26 05:05

TriMontana


You can use draggable_scrollbar. By using draggable scrollbar the full code for you is given below.

import 'package:draggable_scrollbar/draggable_scrollbar.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class MyApp extends StatelessWidget {
  final ScrollController _scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: DraggableScrollbar.rrect(
            controller: _scrollController,
            padding: EdgeInsets.zero,
            child: ListView.builder(
                controller: _scrollController,
                itemCount: 100,
                itemBuilder: (context, index) {
                  return Card(
                      child: ListTile(
                    title: Text("Item: ${index + 1}"),
                  ));
                }),
          ),
        ),
      ),
    );
  }
}
like image 36
Salim Murshed Avatar answered May 15 '26 05:05

Salim Murshed



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!