dart - 如何同步滚动两个 SingleChildScrollView 小部件

标签 dart flutter

如何同步滚动两个 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/

相关文章:

flutter - 如何在 video_player 上无延迟地顺序播放视频?

dart - 如何在Dart中解析和转换文本

flutter - 如何在 Flutter 中创建带条纹的背景

flutter - 我们如何从 future 的方法中获得值(value)?

flutter - 如何在没有等待的情况下获得 future 值(value)?

flutter - Flutter中dio的全局配置(拦截器)

flutter - 你如何在 dart 中使用抽象类进行继承?错误 : superclass SpanishData doesn't have a zero argument constructor

flutter - 如何在 Flutter 中从 XML 中获取文本值

android - flutter + firebase + Stripe

dart - 如何为 CustomClipper 创建的小部件制作合适的边框和阴影