颤动,根据scrolloffset调整列表中的小部件

时间:2017-12-29 18:06:36

标签: mobile dart scrollview flutter

如何根据基础滚动视图的偏移量在ListView中设置窗口小部件的高度?

感谢任何提示:)

1 个答案:

答案 0 :(得分:0)

要知道ListView的当前滚动偏移,您只需传递一个控制器并检查控制器的偏移量。

示例:

class ScrollOffsetWidget extends StatefulWidget {
  @override
  _ScrollOffsetWidgetState createState() => new _ScrollOffsetWidgetState();
}

class _ScrollOffsetWidgetState extends State<ScrollOffsetWidget> {
  List<double> height = <double>[200.0,200.0,200.0,200.0,200.0];

  double scrollOffset = 0.0;

  static ScrollController _controller;

  _ScrollOffsetWidgetState(){
    _controller = new ScrollController(initialScrollOffset: scrollOffset);
    _controller.addListener(listen);
  }

  void listen(){
    final List<double> newHeight = height;

    if(_controller.offset>100.0){
      newHeight[1] = 400.0;
    }else{
      newHeight[1] = 200.0;
    }

    setState((){
      height = newHeight;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        body: new ListView(
          controller: _controller,
          children: <Widget>[
            new AnimatedContainer(duration: const Duration(milliseconds: 300),height: height[0],color: Colors.primaries[0],),
            new AnimatedContainer(duration: const Duration(milliseconds: 300),height: height[1],color: Colors.primaries[1],),
            new AnimatedContainer(duration: const Duration(milliseconds: 300),height: height[2],color: Colors.primaries[2],),
            new AnimatedContainer(duration: const Duration(milliseconds: 300),height: height[3],color: Colors.primaries[3],),
            new AnimatedContainer(duration: const Duration(milliseconds: 300),height: height[4],color: Colors.primaries[4],)
          ],
        ),
      ),
    );
  }
}

希望有所帮助!