0

I want to change scrollView's offset with code so I use ScrollController:

ScrollController _controller;

_controller.addListener(() {
  print('call listener');
});

My way to change offset:

_controller.jumpTo(200);

it will callback listener once.

or

_controller.animateTo(200, duration: Duration(milliseconds: 1), curve: Curves.linear);

it will callback listener, too.

I wonder is there any way to change scrollView offset without callback listener.

Here is all my code and you can coppy and test:

import 'package:flutter/material.dart';

class SingleChildScrollViewDemoPage extends StatefulWidget {
  SingleChildScrollViewDemoPage({Key key}) : super(key: key);

  @override
  _SingleChildScrollViewDemoPageState createState() =>
      _SingleChildScrollViewDemoPageState();
}

class _SingleChildScrollViewDemoPageState
    extends State<SingleChildScrollViewDemoPage> {
  ScrollController _controller;

  @override
  void initState() {
    super.initState();
    _controller = ScrollController();
    _controller.addListener(() {
      print('call listener');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('SingleChildScrollView')),
      body: SingleChildScrollView(
        controller: _controller,
        child: Column(
          children: [
            RaisedButton(
              child: Text('change offset'),
              onPressed: () {
                //_controller.jumpTo(200);
                _controller.animateTo(200,
                    duration: Duration(milliseconds: 1), curve: Curves.linear);
              },
            ),
            Container(
              width: 375,
              height: 200,
              color: Colors.red,
            ),
            SizedBox(height: 30),
            Container(
              width: 375,
              height: 3000,
              color: Colors.green,
            ),
          ],
        ),
      ),
    );
  }
}

无夜之星辰
  • 5,426
  • 4
  • 25
  • 48
  • There is a dummy way, you can remove the listener before you scroll and add it back after scroll. Or and a bool value to ignore, it still will call listener, but you can design to do action or not. May I ask the reason that you do not want call listener but you still add one? – Sam Chan Jul 09 '21 at 08:45
  • Hello @SamChan. Here is a listView in which every cell has a horizontal scrollView, when user scroll the scrollView in the cell, all the other cells's scrollView should change it's offset to the same. My solution is fire eventBus when any cell's scrollView offset changes, so I addListener to the cell's ScrollController and fire eventBus when user change offset. Other cell call `_controller.jumpTo` when received eventBus. However, `_controller.jumpTo` will callback listener, it's become a bad circle. – 无夜之星辰 Jul 09 '21 at 08:59
  • 1
    I see, you may need [linked_scroll_controller](https://pub.dev/packages/linked_scroll_controller). It can link all `ScrollController`. Would you mind to try this one? – Sam Chan Jul 09 '21 at 09:38

0 Answers0