Flutter ScrollController附加到多个滚动视图 [英] flutter ScrollController attached to multiple scroll views

查看:569
本文介绍了Flutter ScrollController附加到多个滚动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当呈现窗口小部件的多个实例并且调用getValue方法时,将引发错误ScrollController附加到多个滚动视图。我假设这是因为他们都使用相同的控制器,但是我不知道一种解决方法,而每次使用时都不会创建单独的小部件。是否有更好的方法来解决此问题?

when multiple instances of widget are rendered and the getValue method is called flutter throws the error ScrollController attached to multiple scroll views. I'm assuming this is because they all are using the same controller but I don't know a way to fix this without creating a separate widget for each time it is used. Is there a better way to fix this?

class NumScroller extends StatelessWidget{
  final int max,min;
  final double height,width;
  final TextAlign alignment;

  static ScrollController controller;

  NumScroller({this.height,this.width,this.alignment,this.min,this.max, initialOffset}){
    controller = new ScrollController(initialScrollOffset: initialOffset);
  }

  getValue() => (controller.offset~/height) + min;

  @override
  Widget build(BuildContext context) {
    return new Container(
        width: width,
        height: height,
        child: ListView.builder(itemBuilder: (context, index) {
          return new Container(height: height, child:Text((max - index).toString(),textAlign: alignment,));
          },
          itemCount: max - min+1,
          controller: controller,
          physics: PageScrollPhysics(),
          itemExtent: height,
        )
    );
  }

}


推荐答案

正如您所描述的,当多个实例被渲染时,您会遇到此错误。当您的 ScrollController 是多个(一个视图一个)时,您不会有任何问题。但是您只有一个ScrollController(因为您具有静态)。

As you are telling when multiple instances are rendered, you are getting this error. When your ScrollController is multiple(one for one view), you will not get any problem. But you have only one ScrollController (because you have static).

删除静态对象,它应该可以工作。

Remove the static and it should work.

请让我知道是否无效。

这篇关于Flutter ScrollController附加到多个滚动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆