我想用代码更改滚动视图的偏移量,所以我使用ScrollController
:
ScrollController _controller;
_controller.addListener(() {
print('call listener');
});
我改变偏移量的方法:
_controller.jumpTo(200);
它会回调一次监听器。
或者
_controller.animateTo(200, duration: Duration(milliseconds: 1), curve: Curves.linear);
它也会回调监听器。
我想知道有没有办法在没有回调侦听器的情况下更改滚动视图偏移量。
这是我的所有代码,您可以复制和测试:
import 'package:flutter/material.dart';
class SingleChildScrollViewDemoPage extends StatefulWidget {
SingleChildScrollViewDemoPage({Key key}) : super(key: key);
@override
_SingleChildScrollViewDemoPageState createState() =>
_SingleChildScrollViewDemoPageState();
}
class _SingleChildScrollViewDemoPageState
extends State<SingleChildScrollViewDemoPage> {
ScrollController _controller;
@override
void initState() {
super.initState();
_controller = ScrollController();
_controller.addListener(() {
print('call listener');
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('SingleChildScrollView')),
body: SingleChildScrollView(
controller: _controller,
child: Column(
children: [
RaisedButton(
child: Text('change offset'),
onPressed: () {
//_controller.jumpTo(200);
_controller.animateTo(200,
duration: Duration(milliseconds: 1), curve: Curves.linear);
},
),
Container(
width: 375,
height: 200,
color: Colors.red,
),
SizedBox(height: 30),
Container(
width: 375,
height: 3000,
color: Colors.green,
),
],
),
),
);
}
}