ScrollController not attached to any scroll views

前端 未结 11 1165
难免孤独
难免孤独 2021-01-17 07:22

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.

相关标签:
11条回答
  • 2021-01-17 07:47

    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,
          ),
        ),
      ),
    
    0 讨论(0)
  • 2021-01-17 07:48

    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);
          }
    });
    
    0 讨论(0)
  • 2021-01-17 07:50

    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

    0 讨论(0)
  • 2021-01-17 07:53

    Initialize the scrollController:

    ScrollController _scrollController = ScrollController(); 
    

    Use the code bellow where you want to scroll:

    SchedulerBinding.instance.addPostFrameCallback((_) {
      _scrollController.jumpTo(_scrollController.position.maxScrollExtent);
    });
    
    0 讨论(0)
  • 2021-01-17 07:59

    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,
    
    0 讨论(0)
  • 2021-01-17 08:00

    To set the initial position of a ScrollController, use the initialScrollOffset property:

    _scrollController = ScrollController(initialScrollOffset: 50.0);
    
    0 讨论(0)
提交回复
热议问题