如何根据底层滚动视图的偏移量设置ListView
中小部件的高度?
感谢您的任何提示:)
要了解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],)
],
),
),
);
}
}
希望有帮助!