Flutter - 自动向下滚动
Flutter - Scroll down automatically
我尝试做一个 ExpansionPanel
当我按下它时它会自动滚动。如果我想转到小部件的底部,我需要滚动。我尝试使用 jumpTo
和 animateTo
但效果不佳。
Container(
decoration: BoxDecoration(
boxShadow: [BoxShadow(color: Colors.grey.withOpacity(0.5), spreadRadius: 5, blurRadius: 7, offset: Offset(0, 3))]),
margin: EdgeInsets.fromLTRB(8, 8, 8, 25),
child: ExpansionPanelList(
expandedHeaderPadding: EdgeInsets.zero,
children: [
ExpansionPanel(
headerBuilder: (context, sOpen) {
return Container(
padding: EdgeInsets.zero,
margin: EdgeInsets.zero,
child: TextButton(
style: ButtonStyle(
overlayColor: MaterialStateProperty.all(Colors.grey[400].withOpacity(0.5)),
foregroundColor: MaterialStateProperty.all(Colors.black87),
),
onPressed: () {
setState(() {
isOpen = !isOpen;
expandList();
});
},
child: task('Tasks (${tasks.length})', icon: Icon(Icons.supervised_user_circle, color: Colors.grey))));
},
body: Column(children: taskContains()),
isExpanded: isOpen,
backgroundColor: Color(0xfffafafa))
],
expansionCallback: (i, iOpen) {
setState(() {
isOpen = !iOpen;
expandList();
});
}));
void expandList() {
if (isOpen) {
controller.jumpTo(0);
controller.animateTo((80 * tasks.length.toDouble()), duration: Duration(seconds: 1), curve: Curves.fastOutSlowIn);
}
}
问题是当我展开时,Widget 保持原来的大小,而当我展开它时,滚动条位于旧小部件的底部。所以我们需要等待构建结束。所以我这样做:
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
expandList();
});
我尝试做一个 ExpansionPanel
当我按下它时它会自动滚动。如果我想转到小部件的底部,我需要滚动。我尝试使用 jumpTo
和 animateTo
但效果不佳。
Container(
decoration: BoxDecoration(
boxShadow: [BoxShadow(color: Colors.grey.withOpacity(0.5), spreadRadius: 5, blurRadius: 7, offset: Offset(0, 3))]),
margin: EdgeInsets.fromLTRB(8, 8, 8, 25),
child: ExpansionPanelList(
expandedHeaderPadding: EdgeInsets.zero,
children: [
ExpansionPanel(
headerBuilder: (context, sOpen) {
return Container(
padding: EdgeInsets.zero,
margin: EdgeInsets.zero,
child: TextButton(
style: ButtonStyle(
overlayColor: MaterialStateProperty.all(Colors.grey[400].withOpacity(0.5)),
foregroundColor: MaterialStateProperty.all(Colors.black87),
),
onPressed: () {
setState(() {
isOpen = !isOpen;
expandList();
});
},
child: task('Tasks (${tasks.length})', icon: Icon(Icons.supervised_user_circle, color: Colors.grey))));
},
body: Column(children: taskContains()),
isExpanded: isOpen,
backgroundColor: Color(0xfffafafa))
],
expansionCallback: (i, iOpen) {
setState(() {
isOpen = !iOpen;
expandList();
});
}));
void expandList() {
if (isOpen) {
controller.jumpTo(0);
controller.animateTo((80 * tasks.length.toDouble()), duration: Duration(seconds: 1), curve: Curves.fastOutSlowIn);
}
}
问题是当我展开时,Widget 保持原来的大小,而当我展开它时,滚动条位于旧小部件的底部。所以我们需要等待构建结束。所以我这样做:
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
expandList();
});