如何同步滚动两个 SingleChildScrollView 小部件?
new Positioned(
child: new SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: new Scale()
),
),
new Positioned(
child: new SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: new chart()
)
)
我需要这两个小部件具有完全相同的滚动位置(它们具有相同的宽度并且只能水平滚动)。它必须在用户操作后和代码更改时同步。
最佳答案
喜欢xster说,你必须在一个 ScrollView 上使用滚动 Controller ,在另一个 ScrollView 上使用通知监听器,这是代码。
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,
),
),
),
],
),
);
}
}
关于dart - 如何同步滚动两个 SingleChildScrollView 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49029993/