Flutter 在颤振网格视图中滚动/跳至正确位置
我正在GridView中创建日历界面。我想滚动到当前月份的第一天。但是,每当我尝试滚动到我计算的位置时,它都是错误的。我目前的日历起点是2016年1月1日。我目前正在为每个日历行使用固定的高度,但它仍然没有滚动到正确的位置 注意:下面的图像是在2019年10月11日生成的。我正在尝试自动滚动至2019年10月1日 在纵向中,它滚动得太远: 当我旋转到横向时,滚动不够: 创建GridView:Flutter 在颤振网格视图中滚动/跳至正确位置,flutter,flutter-layout,flutter-sliver,flutter-gridview,Flutter,Flutter Layout,Flutter Sliver,Flutter Gridview,我正在GridView中创建日历界面。我想滚动到当前月份的第一天。但是,每当我尝试滚动到我计算的位置时,它都是错误的。我目前的日历起点是2016年1月1日。我目前正在为每个日历行使用固定的高度,但它仍然没有滚动到正确的位置 注意:下面的图像是在2019年10月11日生成的。我正在尝试自动滚动至2019年10月1日 在纵向中,它滚动得太远: 当我旋转到横向时,滚动不够: 创建GridView: double calendarHeight = 100.0; ... @override
double calendarHeight = 100.0;
...
@override
Widget build(BuildContext context) {
// Get the size of the screen
var size = MediaQuery.of(context).size;
var ratio = size.width / calendarHeight / 7; // Divide ratio by 7 because there are 7 columns
Scaffold scaffold = Scaffold(
...
body: new Stack(
children: <Widget>[
new PageView(
children: <Widget>[
// Calendar Page
new Container(
child: new Center(
child: GridView.builder(
gridDelegate: _calendarGridDelegate(ratio),
controller: scrollController,
itemCount: _calendarDates.length,
itemBuilder: (BuildContext context, int index) {
if (index < _calendarDates.length) {
return _calendarItemBuilder(index);
} else
return null;
},
),
),
color: Colors.blue,
),
...
WidgetsBinding.instance.addPostFrameCallback((_) {
double scrollTo = (dayInList ~/ 7) * calendarHeight;
...
scrollController.jumpTo(scrollTo);
});
return scaffold;
在日历中创建一天的内容:
/// Builds a [Widget] for one calendar day in the calendar page
Widget _calendarItemBuilder(int index) {
String output;
if (_calendarDates[index].day == 1) {
output = Constants().monthNames[_calendarDates[index].month - 1] +
" " +
_calendarDates[index].day.toString();
} else {
output = _calendarDates[index].day.toString();
}
return new Container(
padding: EdgeInsets.all(5.0),
child: new Center(
child: Text(output),
),
);
}
我找到了一个答案,但这远远不能令人满意,因为它毫无意义:
double scrollTo = (dayInList ~/ 7) * (calendarHeight - (calendarHeight - size.width/6)/(size.width/6));
...
scrollController.jumpTo(scrollTo);
这需要大量的尝试和错误,但它可以在我用来测试的两个设备上工作
double scrollTo = (dayInList ~/ 7) * (calendarHeight - (calendarHeight - size.width/6)/(size.width/6));
...
scrollController.jumpTo(scrollTo);