Repository navigation
Open
Labels
P2Important issues not at the top of the work listd: stackoverflowGood question for Stack Overflowf: scrollingViewports, list views, slivers, etc.frameworkflutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team
Description
Steps to Reproduce
The example below use LayoutBuilder to show Scrollbar when screen width is smaller than 960dp and remove the Scrollbar when larger than 960dp.
If we want isAlwaysShown: true, we must provide the same ScrollController to both Scrollbar and ScrollView.
When remove the ScrollView from the current widget tree, an exception is thrown.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(home: MyStatelessWidget());
}
}
class MyStatelessWidget extends StatefulWidget {
const MyStatelessWidget({Key? key}) : super(key: key);
@override
_MyStatelessWidgetState createState() => _MyStatelessWidgetState();
}
class _MyStatelessWidgetState extends State<MyStatelessWidget> {
ScrollController _scrollController = ScrollController();
@override
Widget build(BuildContext context) {
Widget _scaffold = Scaffold(
appBar: AppBar(
title: const Text('LayoutBuilder Example'),
),
body: Container(
child: Text(
'Example Content',
style: TextStyle(fontSize: 200),
),
),
);
return LayoutBuilder(builder: (
BuildContext context,
BoxConstraints constraints,
) {
Widget _result = _scaffold;
if (constraints.minWidth < 960) {
_result = wrapWithWidthScroll(_result);
}
return _result;
});
}
Widget wrapWithWidthScroll(Widget _scaffold) {
return Scrollbar(
isAlwaysShown: true,
controller: _scrollController,
child: SingleChildScrollView(
controller: _scrollController,
child: Container(width: 960, child: _scaffold),
scrollDirection: Axis.horizontal,
),
);
}
}
Expected results:
No error when we expand screen width (web or desktop) from ex. 900dp to ex. 1000dp.
Actual results:
Error when we expand screen width (web or desktop) from ex. 900dp to ex. 1000dp.
======== Exception caught by scheduler library =====================================================
The following assertion was thrown during a scheduler callback:
The Scrollbar's ScrollController has no ScrollPosition attached.
A Scrollbar cannot be painted without a ScrollPosition.
The Scrollbar attempted to use the PrimaryScrollController. This ScrollController should be associated with the ScrollView that the Scrollbar is being applied to. When providing your own ScrollController, ensure both the Scrollbar and the Scrollable widget use the same one.
When the exception was thrown, this was the stack:
#0 RawScrollbarState._maybeTriggerScrollbar.<anonymous closure>.<anonymous closure> (package:flutter/src/widgets/scrollbar.dart:960:13)
#1 RawScrollbarState._maybeTriggerScrollbar.<anonymous closure> (package:flutter/src/widgets/scrollbar.dart:985:10)
#2 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
#3 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1090:9)
#4 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:998:5)
...
====================================================================================================
flutter doctor -v
[√] Flutter (Channel beta, 2.2.0-10.3.pre, on Microsoft Windows [Version 10.0.19042.964], locale en-DE)
• Flutter version 2.2.0-10.3.pre at C:\...\flutter
• Framework revision 06e2fd6357 (5 days ago), 2021-05-08 11:28:22 -0700
• Engine revision a123e75c60
• Dart version 2.13.0 (build 2.13.0-211.14.beta)
Reacted by Kevin Vuilleumier, iliiliili and Felipe Sales
Metadata
Metadata
Assignees
Labels
P2Important issues not at the top of the work listd: stackoverflowGood question for Stack Overflowf: scrollingViewports, list views, slivers, etc.frameworkflutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team
Activity
goderbauer commentedon May 19, 2021
/cc @Piinks
Piinks commentedon Jun 4, 2021
@nguyenxndaidev can you not remove the Scrollbar widget when you remove the ScrollView or toggle the isAlwaysShown? When you remove the ScrollView and leave the Scrollbar there as it is, the Scrollbar expects that you have linked it to a ScrollView. The Scrollbar tries to paint itself since you have isAlwaysShown true but there is no Scrollable, so it fails to do so.
You could also use ScrollBehavior to apply the Scrollbar instead. That way you do not have to add a Scrollbar. Instead, the Scrollable itself will get a Scrollbar from the ScrollBehavior.buildScrollbar. So if you remove the ScrollView, the Scrollbar goes away too.
xuanswe commentedon Jun 11, 2021
I am not quite sure if I understand you correctly. In the provided snippet, both Scrollbar and ScrollView are removed if
constraints.minWidth >= 960.I will try this solution when I have time and consider it as a workaround. We should still need to fix this current issue, right?
xuanswe commentedon Jun 20, 2021
If we try to not remove the Scrollbar widget:
We will get this error:
xuanswe commentedon Jun 20, 2021
Oh, but if
isAlwaysShown: constraints.minWidth < 960 ? true : false, it works.Even though it works, it's just a workaround I think.
I need to test this workaround more for the 2D case.
xuanswe commentedon Jun 20, 2021
I couldn't make it work for the 2D case with this workaround. Or maybe it will work but will be too complex.
ibrahimsebbane commentedon Dec 29, 2023
Guys in my Case I have ScrollBar with child ScollablePositionedList instead of ScrollView what should I do ? it doesnt have the parameter controller (ScrollView doesn't have this)