gpt4 book ai didi

dart - 同步 ListView 滚动位置

转载 作者:行者123 更新时间:2023-12-03 13:29:18 25 4
gpt4 key购买 nike

我试图给这个屏幕一个特定的滚动行为。每行都有使用 BLoC 模式提供给它的数据。
屏幕左侧只需要垂直滚动,而右侧需要水平和垂直滚动。
我目前正在通过为垂直滚动和共享 BLoC 提供两个单独的 ListView 来做到这一点。然后右侧小部件被包装在一个以相反方向移动的 ListView 中。
enter image description here
我需要同步垂直和水平 ListView (以及将来可能的 ScrollView )并且无法成功确保滚动位置保持同步。如何同步多个 ListView 位置?这是我正在尝试做的代码片段:

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),
),
],
),
),
),
],
);
}
}

最佳答案

我通常使用 linked_scroll_controller以此目的。
退房 this article详情。

关于dart - 同步 ListView 滚动位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52917514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com