如您所见,有一个红色的水平线SingleChildScrollView
,每次点击滚动按钮时,它的偏移量都会增加 20。
这是我的代码:
import 'package:flutter/material.dart';
import 'package:flutter_storage/managers/screen_util.dart';
class DraftPage extends StatefulWidget {
DraftPage({Key key}) : super(key: key);
@override
_DraftPageState createState() => _DraftPageState();
}
class _DraftPageState extends State<DraftPage> {
int index = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Test'),
),
body: Column(
children: [
SizedBox(
width: ScreenUtil.screenWidth,
height: 30,
child: _LineView(currentIndex: index),
),
SizedBox(height: 20),
RaisedButton(
child: Text('scroll'),
onPressed: () {
setState(() {
index++;
});
},
),
],
),
);
}
}
class _LineView extends StatelessWidget {
const _LineView({Key key, @required this.currentIndex}) : super(key: key);
final int currentIndex;
@override
Widget build(BuildContext context) {
final offset = currentIndex * 20.0;
print(offset);
final controller =
ScrollController(initialScrollOffset: offset, keepScrollOffset: false);
controller.addListener(() {
print(controller.offset);
});
final row = Row(
children: [
Container(
width: 1000,
height: 30,
color: Colors.red,
child: Text('666666666666666666666'),
),
],
);
final scrollView = SingleChildScrollView(
controller: controller,
scrollDirection: Axis.horizontal,
child: row,
);
return scrollView;
}
}
但是,当我点击滚动按钮时,滚动视图的偏移量根本没有改变。
我的代码有什么问题?
你可以复制我的代码并测试。