So, I have bigger scroll view that scrolls horizontally, and inside - little box (red color) and smaller scrollview (orange color) that scrolls vertically. There are two scrollbars on the bigger scrollview (1 - for horizontal), and second - for vertical inner.
And the problem - vertical scrollbar doesnt look right, because it can go only like blue arrow shows, and I want it to have either full height of the bigger scrollview, or be right near scrollable vertical part, but then dont hide itself if scrolled in horizontal direction.
Run on dartPad
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: darkBlue,
),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: ScrollSizingWidget(),
),
),
);
}
}
class ScrollSizingWidget extends StatefulWidget {
const ScrollSizingWidget({
Key? key,
}) : super(key: key);
@override
State<ScrollSizingWidget> createState() => _ScrollSizingWidgetState();
}
class _ScrollSizingWidgetState extends State<ScrollSizingWidget> {
final ScrollController _horizontal = ScrollController();
final ScrollController _vertical = ScrollController();
@override
void dispose() {
_horizontal.dispose();
_vertical.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scrollbar(
controller: _vertical,
notificationPredicate: (notification) => notification.depth == 1,
child: Scrollbar(
controller: _horizontal,
scrollbarOrientation: ScrollbarOrientation.bottom,
child: SingleChildScrollView(
controller: _horizontal,
scrollDirection: Axis.horizontal,
child: SizedBox(
height: 500,
width: 1000,
child: Column(
children:[
Container(width: 1000, height: 200, color: Colors.green),
Flexible(
child: SingleChildScrollView(
controller: _vertical,
child: Container(
height: 700,
width: 1000,
color: Colors.yellow,
)
)
),
]
)
),
),
),
);
}
}
I have used your code to reproduce the issue. If I understood your needs right, here is the fix:
Firstly, you say that your main
SingleChildScrollView
scrolls horizontally, but your widget tree starts with aScrollbar
which uses a verticalScrollController
. So you should create your widgets step by step, as you explained.Also, since you want to see the vertical
Scrollbar
through your mainSingleChildScrollView
, I wrapped both(red and orange Containers) withScrollbar
andSingleChildScrollView
to have the effect you want. Furthermore, I connected theseScrollbar
andSingleChildScrollView
with the same horizontalScrollController
. So now, not only the orangeContainer
, but both are scrollable and stick together, not independent.If you don't want the red
Container
being scrolled along with the orangeContainer
, check this:Lastly,
Scrollbar
's position in iOS is a bit buggy because of the notch, etc. So I wrapped yourScrollSizingWidget
withSafeArea
to fix the issue in iOS.If these answers are not what you expect, please don't hesitate to write.
Edit: After your explanations in the comments below, I have created another fix. I believe
CustomScrollView
andSliver
widgets are fits here perfectly. The redContainer
, which you want to stay in its position, should be wrapped with theSliverAppBar
. Lastly, the orangeContainer
, which you want to be able to scroll vertically, could be wrapped withSliverFixedExtentList
. Please check the code below: