Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronising ListView Scroll Positions

I'm trying to give this screen a specific scroll behaviour. Each row has the data provided to it using the BLoC pattern.
The left side of the screen needs to only be scrolled vertically, whilst the right side needs to be scrolled both horizontally and vertically.
I'm currently doing this by having two separate list views for vertical scroll and sharing the BLoC. The right side widgets are then wrapped in a list view moving in the opposite direction.

enter image description here

I need to synchronise the vertical and horizontal list views (and potentially scroll views in the future) and haven't been able to successfully ensure the scroll positions are kept in sync. How can I sync multiple list view positions? Here is the code snippet for what I'm trying to do:

class ComposePage extends StatefulWidget {
  @override
  _ComposePageState createState() => _ComposePageState();
}

class _ComposePageState extends State<ComposePage> {
  final TrackingScrollController _scrollController = TrackingScrollController();

  @override
  Widget build(BuildContext context) {
    return Container(
      child: NotificationListener<ScrollNotification>(
        onNotification: (notification) {
          print("SCROLL");
      
_scrollController.position.setPixels(notification.metrics.pixels);
          for (ScrollPosition position in _scrollController.position)
            position.setPixels(notification.metrics.pixels);
        },
        child: Column(
          verticalDirection: VerticalDirection.up,
          children: <Widget>[
            Expanded(
              flex: 8,
              child: Material(
                type: MaterialType.canvas,
                elevation: 1.0,
                child: _buildBuildComposeContent(_scrollController),
              ),
            ),
            Expanded(
              flex: 1,
              child: Material(
                type: MaterialType.canvas,
                elevation: 20.0,
                child: ControlsHeader(),
              ),
            )
          ],
        ),
      ),
      color: Colors.green,
    );
  }

  _buildBuildComposeContent(TrackingScrollController scrollController) {
    return Container(
      child: Row(
        children: <Widget>[
          Expanded(
            flex: 1,
            child: _buildLabelArea(scrollController),
          ),
          Expanded(
            flex: 7,
            child: _buildEditArea(scrollController),
          ),
        ],
      ),
    );
  }

  Column _buildLabelArea(TrackingScrollController controller) {
    return Column(
      children: [
        Expanded(
          flex: 1,
          child: Container(
            color: Colors.pink,
          ),
        ),
        Expanded(
          flex: 8,
          child: ListView(
            scrollDirection: Axis.vertical,
            controller: controller,
            children: <Widget>[
              BlocProvider(
                bloc: TrackBloc(),
                child: TrackLabel(),
              ),
              BlocProvider(
                bloc: TrackBloc(),
                child: TrackLabel(),
              ),
              BlocProvider(
                bloc: TrackBloc(),
                child: TrackLabel(),
              ),
              BlocProvider(
                bloc: TrackBloc(),
                child: TrackLabel(),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Column _buildEditArea(TrackingScrollController controller) {
    return Column(
      children: <Widget>[
        Expanded(
          flex: 1,
          child: Playhead(),
        ),
        Expanded(
          flex: 8,
          child: Container(
            child: ListView(
              scrollDirection: Axis.vertical,
              controller: controller,
              children: <Widget>[
                BlocProvider(
                  bloc: TrackBloc(),
                  child: TrackView(isEditable: true),
                ),
                BlocProvider(
                  bloc: TrackBloc(),
                  child: TrackView(isEditable: true),
                ),
                BlocProvider(
                  bloc: TrackBloc(),
                  child: TrackView(isEditable: true),
                ),
                BlocProvider(
                  bloc: TrackBloc(),
                  child: TrackView(isEditable: true),
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}
like image 675
Jefferson Avatar asked Oct 21 '18 16:10

Jefferson


People also ask

How to programmatically scroll to a specific position in listview in Android?

How to programmatically scroll to a specific position in listView in Android? This example demonstrates how do I programmatically scroll to a specific position in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

How to disable scroll animation in sflistview in Android?

When AutoFitModeis Heightor grouping is enabled, the scroll animation will be disabled by default in Android and iOS platforms. If the ScrollToRowIndexmethod is called when loading the SfListView, set disableAnimationto trueto scroll to the appropriate row index, or else view does not scrolled in Android.

What are the different types of positions in a view?

Below are four different types of positions: MakeVisible: Scrolls a specific item to make visible in the view. If the item is already in view, scrolling will not occur. Start: Scrolls a specific item to be positioned at the begin of the view. End: Scrolls a specific item to be positioned at the end of the view.

How do I add a scrollcontroller to a listpage widget?

Firstly, we set up the ListPage widget, which consists of 2 buttons at the top, and a list of items below. Now, you can run the app to view the list and the 2 buttons above it. Secondly, we create a ScrollController … … and add it to the itemsWidget.


Video Answer


1 Answers

I usually use linked_scroll_controller for this purpose. Check out this article for detail.

like image 123
Crizant Avatar answered Nov 15 '22 09:11

Crizant