4

I want to scroll a SingleChildScrollView() horizontally with mouse wheel in Flutter web but the SingleChildScrollView() is inside a ListView() and when i try to scroll it with the mouse wheel it scrolls the ListView().

This is my code

 child: ListView(
 children: [
 Container(
 width: 420.0,
 child: Row(
 children: [
 Expanded(
 child: Text(
 'Popular Search',
 ),
 ),
 Expanded(
 flex: 3,
 child: SingleChildScrollView(
 scrollDirection: Axis.horizontal,
 child: Row(
 children: categoriesList.map((e) {
 return Container(
 decoration: BoxDecoration(
 borderRadius:
 BorderRadius.all(Radius.circular(12.0)),
 color: Colors.white,
 ),
 child: Text(
 e,
 style: TextStyle(
 fontSize: 13.0,
 ),
 ),
 );
 }).toList(),
 ),
 ),
 )
 ],
 ),
 ),
 ],
 ),
asked Jun 2, 2021 at 9:01
2
  • 1
    I'm also having the same issue :( Commented Aug 26, 2021 at 3:42
  • I'm also facing the same issue. I tried this stackoverflow.com/questions/69154468/… answer but did not work for me. Let me know if you found a solution. As a quick fix, I added a scrollbar & made it always visible for scrolling. Commented Nov 25, 2021 at 6:26

1 Answer 1

7

Wrap your inner SingleChildScrollView with Listener and add onPointerSignal callback, and you can get a offset in y axis when use mouse wheel. Then, scroll inner SingleChildScrollView and forbid outer ListView from scrolling. Here is my code and it works on dartpad.

final outerController = ScrollController();
final innerController = ScrollController();
ListView(
 controller: outerController,
 children: [
 Container(
 width: 420.0,
 child: Row(
 children: [
 Expanded(
 child: Text(
 'Popular Search',
 ),
 ),
 Expanded(
 flex: 3,
 child: Listener(
 onPointerSignal: (event) {
 if (event is PointerScrollEvent) {
 final offset = event.scrollDelta.dy;
 innerController.jumpTo(innerController.offset + offset);
 outerController.jumpTo(outerController.offset - offset);
 }
 },
 child: SingleChildScrollView(
 controller: innerController,
 scrollDirection: Axis.horizontal,
 child: Row(
 children: categoriesList.map((e) {
 return Container(
 decoration: BoxDecoration(
 borderRadius:
 BorderRadius.all(Radius.circular(12.0)),
 color: Colors.white,
 ),
 child: Text(
 e,
 style: TextStyle(
 fontSize: 13.0,
 ),
 ),
 );
 }).toList(),
 ),
 ),
 ))
 ],
 ),
 ),
 ],
 ),
answered Jan 30, 2022 at 16:27
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.