Scroll multiple scrollable widgets in sync. I have managed to sync multiple scrollables by using their offset, utilizing their ScrollNotification.
Here is an example code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | class _MyHomePageState extends State<MyHomePage> { ScrollController _mycontroller1 = new ScrollController(); // make seperate controllers ScrollController _mycontroller2 = new ScrollController(); // for each scrollables @override Widget build(BuildContext context) { body: Container( height: 100, child: NotificationListener<ScrollNotification>( // this part right here is the key Stack( children: <Widget>[ SingleChildScrollView( // this one stays at the back controller: _mycontroller1, child: Column( children: <Widget>[ Text('LEFT '), Text('LEFT '), Text('LEFT '), Text('LEFT '), Text('LEFT '), Text('LEFT '), ],) ), SingleChildScrollView( // this is the one you scroll controller: _mycontroller2, child: Column(children: <Widget>[ Text(' RIGHT'), Text(' RIGHT'), Text(' RIGHT'), Text(' RIGHT'), Text(' RIGHT'), Text(' RIGHT'), ],) ), ]), onNotification: (ScrollNotification scrollInfo) { // HEY!! LISTEN!! // this will set controller1's offset the same as controller2's _mycontroller1.jumpTo(_mycontroller2.offset); // you can check both offsets in terminal print('check -- offset Left: '+_mycontroller1.offset.toInt().toString()+ ' -- offset Right: '+_mycontroller2.offset.toInt().toString()); } ) ) }} |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.