Home > Software engineering >  how to scroll two listview builder (one -two listview builder is in top-bottom)simultaneously by usi
how to scroll two listview builder (one -two listview builder is in top-bottom)simultaneously by usi

Time:07-25

The one listview builder is in top,another is in bottom.I wants the both listvew builder should scroll simultaneously by analyze their index.Even i scroll it ,the first listview will be get index[1],at the same time the second one is always get index[1],here the listview builder have different sizedbox

CodePudding user response:

What i'm understand from your question is that, you want to scroll two listviews simultaneously when scrolling any of the listview alone. (if its not that the case elaborate your case. and include your code also)

May be you can use same scrollController for two listviews

CodePudding user response:

It's quite complex to do in flutter. You might not able to do it with index but you can achieve similar behavior while sync scrolling of two lists. If one list scrolls the other will also scroll. To achieve this behavior you can add linked_scroll_controller to your pubspec.yaml

class LinkedScrollables extends StatefulWidget {
  @override
  _LinkedScrollablesState createState() => _LinkedScrollablesState();
}

class _LinkedScrollablesState extends State<LinkedScrollables> {
  LinkedScrollControllerGroup _controllers;
  ScrollController _letters;
  ScrollController _numbers;

  @override
  void initState() {
    super.initState();
    _controllers = LinkedScrollControllerGroup();
    _letters = _controllers.addAndGet();
    _numbers = _controllers.addAndGet();
  }

  @override
  void dispose() {
    _letters.dispose();
    _numbers.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Directionality(
      textDirection: TextDirection.ltr,
      child: Row(
        children: [
          Expanded(
            child: ListView(
              controller: _letters,
              children: <Widget>[
                _Tile('Hello A'),
                _Tile('Hello B'),
                _Tile('Hello C'),
                _Tile('Hello D'),
                _Tile('Hello E'),
              ],
            ),
          ),
          Expanded(
            child: ListView(
              controller: _numbers,
              children: <Widget>[
                _Tile('Hello 1'),
                _Tile('Hello 2'),
                _Tile('Hello 3'),
                _Tile('Hello 4'),
                _Tile('Hello 5'),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class _Tile extends StatelessWidget {
  final String caption;

  _Tile(this.caption);

  @override
  Widget build(_) => Container(
        margin: const EdgeInsets.all(8.0),
        padding: const EdgeInsets.all(8.0),
        height: 250.0,
        child: Center(child: Text(caption)),
      );
}
  • Related