flutter
flutter copied to clipboard
TwoDimensionalScrollView with Scrollbars - Vertical Scrollbar has to be dragged twice with mouse on Windows and Web to scroll
Steps to reproduce
- run the code below in a dartPad or open https://dartpad.dev/?id=de938a40d5d53193c4006ab7e0205814
- try to drag the vertical scrollbar with the mouse down
- Scrollbar moves a bit than stops
- If you try a second time than it works
Additional: If you try to grap the horizontal handle after scrolling at least twice with the vertical handle, the same problem occurs for the horizontal handle. This is not the case if you start scrolling with the horizontal scrollbar after running the code.
Remark: With the previous flutter version 3.23.x this problem did not occure!
Expected results
Scrolling by dragging the scrollhandle should work right away.
Actual results
Scrollbar stops and user has to drag the handle a second time to scroll vertically.
Code sample
Code sample
import 'dart:math' as math;
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
scrollBehavior: const MaterialScrollBehavior().copyWith(
// Mouse dragging enabled for this demo
dragDevices: PointerDeviceKind.values.toSet(),
),
debugShowCheckedModeBanner: false,
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _myHomePageState();
}
class _myHomePageState extends State<MyHomePage> {
ScrollController vertScrollController = ScrollController();
ScrollController horScrollController = ScrollController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Test Scrolling")),
body: buildScrollTester(context),
);
}
Widget buildScrollTester(BuildContext context) {
return Scrollbar(
thumbVisibility: true,
interactive: true,
controller: horScrollController,
thickness: 12.0,
child: Scrollbar(
thumbVisibility: true,
interactive: true,
thickness: 12.0,
controller: vertScrollController,
child: TwoDimensionalGridView(
diagonalDragBehavior: DiagonalDragBehavior.free,
horizontalDetails: ScrollableDetails(direction: AxisDirection.right, controller: horScrollController),
verticalDetails: ScrollableDetails(direction: AxisDirection.down, controller: vertScrollController),
delegate: TwoDimensionalChildBuilderDelegate(
maxXIndex: 9,
maxYIndex: 9,
builder: (BuildContext context, ChildVicinity vicinity) {
return Container(
color: vicinity.xIndex.isEven && vicinity.yIndex.isEven ? Colors.amber[50] : (vicinity.xIndex.isOdd && vicinity.yIndex.isOdd ? Colors.purple[50] : null),
height: 200,
width: 200,
child: Center(child: Text('Row ${vicinity.yIndex}: Column ${vicinity.xIndex}')),
);
}),
),
),
);
}
}
class TwoDimensionalGridView extends TwoDimensionalScrollView {
const TwoDimensionalGridView({
super.key,
super.primary,
super.mainAxis = Axis.vertical,
super.verticalDetails = const ScrollableDetails.vertical(),
super.horizontalDetails = const ScrollableDetails.horizontal(),
required TwoDimensionalChildBuilderDelegate delegate,
super.cacheExtent,
super.diagonalDragBehavior = DiagonalDragBehavior.none,
super.dragStartBehavior = DragStartBehavior.start,
super.keyboardDismissBehavior = ScrollViewKeyboardDismissBehavior.manual,
super.clipBehavior = Clip.hardEdge,
}) : super(delegate: delegate);
@override
Widget buildViewport(
BuildContext context,
ViewportOffset verticalOffset,
ViewportOffset horizontalOffset,
) {
return TwoDimensionalGridViewport(
horizontalOffset: horizontalOffset,
horizontalAxisDirection: horizontalDetails.direction,
verticalOffset: verticalOffset,
verticalAxisDirection: verticalDetails.direction,
mainAxis: mainAxis,
delegate: delegate as TwoDimensionalChildBuilderDelegate,
cacheExtent: cacheExtent,
clipBehavior: clipBehavior,
);
}
}
class TwoDimensionalGridViewport extends TwoDimensionalViewport {
const TwoDimensionalGridViewport({
super.key,
required super.verticalOffset,
required super.verticalAxisDirection,
required super.horizontalOffset,
required super.horizontalAxisDirection,
required TwoDimensionalChildBuilderDelegate super.delegate,
required super.mainAxis,
super.cacheExtent,
super.clipBehavior = Clip.hardEdge,
});
@override
RenderTwoDimensionalViewport createRenderObject(BuildContext context) {
return RenderTwoDimensionalGridViewport(
horizontalOffset: horizontalOffset,
horizontalAxisDirection: horizontalAxisDirection,
verticalOffset: verticalOffset,
verticalAxisDirection: verticalAxisDirection,
mainAxis: mainAxis,
delegate: delegate as TwoDimensionalChildBuilderDelegate,
childManager: context as TwoDimensionalChildManager,
cacheExtent: cacheExtent,
clipBehavior: clipBehavior,
);
}
@override
void updateRenderObject(
BuildContext context,
RenderTwoDimensionalGridViewport renderObject,
) {
renderObject
..horizontalOffset = horizontalOffset
..horizontalAxisDirection = horizontalAxisDirection
..verticalOffset = verticalOffset
..verticalAxisDirection = verticalAxisDirection
..mainAxis = mainAxis
..delegate = delegate
..cacheExtent = cacheExtent
..clipBehavior = clipBehavior;
}
}
class RenderTwoDimensionalGridViewport extends RenderTwoDimensionalViewport {
RenderTwoDimensionalGridViewport({
required super.horizontalOffset,
required super.horizontalAxisDirection,
required super.verticalOffset,
required super.verticalAxisDirection,
required TwoDimensionalChildBuilderDelegate delegate,
required super.mainAxis,
required super.childManager,
super.cacheExtent,
super.clipBehavior = Clip.hardEdge,
}) : super(delegate: delegate);
@override
void layoutChildSequence() {
final double horizontalPixels = horizontalOffset.pixels;
final double verticalPixels = verticalOffset.pixels;
final double viewportWidth = viewportDimension.width + cacheExtent;
final double viewportHeight = viewportDimension.height + cacheExtent;
final TwoDimensionalChildBuilderDelegate builderDelegate =
delegate as TwoDimensionalChildBuilderDelegate;
final int maxRowIndex = builderDelegate.maxYIndex!;
final int maxColumnIndex = builderDelegate.maxXIndex!;
final int leadingColumn = math.max((horizontalPixels / 200).floor(), 0);
final int leadingRow = math.max((verticalPixels / 200).floor(), 0);
final int trailingColumn = math.min(
((horizontalPixels + viewportWidth) / 200).ceil(),
maxColumnIndex,
);
final int trailingRow = math.min(
((verticalPixels + viewportHeight) / 200).ceil(),
maxRowIndex,
);
double xLayoutOffset = (leadingColumn * 200) - horizontalOffset.pixels;
for (int column = leadingColumn; column <= trailingColumn; column++) {
double yLayoutOffset = (leadingRow * 200) - verticalOffset.pixels;
for (int row = leadingRow; row <= trailingRow; row++) {
final ChildVicinity vicinity =
ChildVicinity(xIndex: column, yIndex: row);
final RenderBox child = buildOrObtainChildFor(vicinity)!;
child.layout(constraints.loosen());
// Subclasses only need to set the normalized layout offset. The super
// class adjusts for reversed axes.
parentDataOf(child).layoutOffset = Offset(xLayoutOffset, yLayoutOffset);
yLayoutOffset += 200;
}
xLayoutOffset += 200;
}
// Set the min and max scroll extents for each axis.
final double verticalExtent = 200 * (maxRowIndex + 1);
verticalOffset.applyContentDimensions(
0.0,
clampDouble(
verticalExtent - viewportDimension.height, 0.0, double.infinity),
);
final double horizontalExtent = 200 * (maxColumnIndex + 1);
horizontalOffset.applyContentDimensions(
0.0,
clampDouble(
horizontalExtent - viewportDimension.width, 0.0, double.infinity),
);
// Super class handles garbage collection too!
}
}
Screenshots or Video
No response
Logs
No response
Flutter Doctor output
Doctor output
C:\Users\Marku>flutter doctor -v
[√] Flutter (Channel stable, 3.24.3, on Microsoft Windows [Version 10.0.19045.4894], locale de-DE)
• Flutter version 3.24.3 on channel stable at D:\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 2663184aa7 (5 weeks ago), 2024-09-11 16:27:48 -0500
• Engine revision 36335019a8
• Dart version 3.5.3
• DevTools version 2.37.3
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at C:\Program Files (x86)\Android\android-sdk
• Platform android-34, build-tools 34.0.0
• ANDROID_HOME = C:\Program Files (x86)\Android\android-sdk
• Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
• Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-9586694)
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
[√] Visual Studio - develop Windows apps (Visual Studio Enterprise 2022 17.10.3)
• Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Enterprise
• Visual Studio Enterprise 2022 version 17.10.35013.160
• Windows 10 SDK version 10.0.22621.0
[√] Android Studio (version 2022.2)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-9586694)
[√] VS Code (version 1.94.2)
• VS Code at C:\Users\Marku\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.98.0
[√] Connected device (3 available)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19045.4894]
• Chrome (web) • chrome • web-javascript • Google Chrome 129.0.6668.101
• Edge (web) • edge • web-javascript • Microsoft Edge 129.0.2792.89
[√] Network resources
• All expected network resources are available.
• No issues found!```
</details>
@mahop-net I think your case is similar to https://github.com/flutter/flutter/issues/154520 that has been fixed few days ago. Please switch to latest master and re-run your scenario to confirm if it resolves your issue or not.
@darshankawar Hello, yes, switching to master solves the problem. Thanks for your feedback!
(I have another problem with scrolling in TwoDimmensionalScrollView witch is not solved with latest master. But I can not reproduce this problem with a small sample. So probably this problem occurs in my code...)
Closing as this is fixed.
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.