I\'m using CustomScrollView, and providing it with a controller. ScrollController works, I even added a listener to it and print out the position of the scroll view.
I resolved my problem using the same ScrollController to both controllers. First I create the ScrollController():
ScrollController scollBarController = ScrollController();
Inside my code:
Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Scrollbar(
controller: scollBarController,
isAlwaysShown: true,
child: StaggeredGridView.count(
controller: scollBarController,
crossAxisCount: 16,
staggeredTiles: _staggeredTamanho,
shrinkWrap: true,
children: [
...
], //listStaggered,
mainAxisSpacing: 7.0,
),
),
),
Delaying it is not the right solution. Better to wait till the tree is done building by using
WidgetsBinding.instance
.addPostFrameCallback((_){});
sample
WidgetsBinding.instance.addPostFrameCallback((_) {
if(pageController.hasClients){
pageController.animateToPage(page index, duration: Duration(milliseconds: 1), curve: Curves.easeInOut);
}
});
I have tried with above solutions but set ScrollController initialScrollOffset, checking hasClients and jumpTo, WidgetsBinding no one is working for me.
at last solved my problem by checking positions 'scrollcontroller.positions.length' like
if (_sc.positions.length == 0 || _sc.position.pixels == 0.0) {
return Container();
}
You have to check controller positions to get rid of the error
Reference : https://api.flutter.dev/flutter/widgets/ScrollController/position.html
Initialize the scrollController:
ScrollController _scrollController = ScrollController();
Use the code bellow where you want to scroll:
SchedulerBinding.instance.addPostFrameCallback((_) {
_scrollController.jumpTo(_scrollController.position.maxScrollExtent);
});
I used this code to make the color of the appbar transparent at 0 position and black for more than 70
backgroundColor: !_scrollController.hasClients
? Colors.transparent
: _scrollController.offset > 70
? Color.fromRGBO(7, 7, 7, 1)
: Colors.transparent,
To set the initial position of a ScrollController
, use the initialScrollOffset
property:
_scrollController = ScrollController(initialScrollOffset: 50.0);