如何同步滚动两个SingleChildScrollView小部件?
new Positioned(
child: new SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: new Scale()
),
),
new Positioned(
child: new SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: new chart()
)
)
我需要这两个小部件有完全相同的滚动位置(两者都有相同的宽度,只能水平滚动)。它必须在用户操作之后和在代码中更改时同步。
发布于 2019-11-28 13:42:53
就像xster说的,你必须在一个滚动视图上使用scrollcontroller,在另一个滚动视图上使用通知侦听器,代码如下。
class StackO extends StatefulWidget {
// const stack({Key key}) : super(key: key);
@override
_StackOState createState() => _StackOState();
}
class _StackOState extends State<StackO> {
ScrollController _scrollController = new ScrollController();
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
new Positioned(
child: new SingleChildScrollView(
controller: _scrollController,
scrollDirection: Axis.horizontal,
child: new Scale() // your widgets,
),
),
new Positioned(
child: new NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification scrollInfo) {
print('scrolling.... ${scrollInfo.metrics.pixels}');
_scrollController.jumpTo(scrollInfo.metrics.pixels);
return false;
},
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: new chart() // your widgets,
),
),
),
],
),
);
}
}
发布于 2018-03-02 21:23:01
对于输入,可以通过作为构造函数参数传入的ScrollController来控制它们。
对于输出,您可以使用NotificationListener监听其中一个的移动,然后使用ScrollController将它们同步在一起。
您可以监听像https://docs.flutter.io/flutter/widgets/UserScrollNotification-class.html这样的命令来将它们紧密绑定在一起,也可以等待https://docs.flutter.io/flutter/widgets/ScrollEndNotification-class.html,然后在ScrollController上调用https://docs.flutter.io/flutter/widgets/ScrollController/animateTo.html。
https://stackoverflow.com/questions/49029993
复制相似问题