我正在创建一个自定义小部件,该小部件将页脚作为最后一项添加到收到的任何可滚动小部件中。
理想的用途是这样的:
ScrollableWithFooter(
child: ListView.builder(...),
footer: Text('Footer'),
)
ScrollableWithFooter(
child: GridView.builder(...),
footer: Text('Footer'),
)
ScrollableWithFooter(
child: CustomListView.builder(...),
footer: Text('Footer'),
)
ScrollableWithFooter(
child: SingleChildScrollView.builder(...),
footer: Text('Footer'),
)
ScrollableWithFooter(
child: StaggeredGridView.builder(...),
footer: Text('Footer'),
)
这是我正在采取的方法:
class ScrollableWithFooter extends StatefulWidget {
final ScrollView child;
final Widget footer;
ScrollableWithFooter({Key key, this.child, this.footer}) : super(key: key);
@override
State<StatefulWidget> createState() => ScrollableWithFooterState();
}
class ScrollableWithFooterState extends State<ScrollableWithFooter> {
final _scrollController = ScrollController();
@override
Widget build(BuildContext context) {
return ListView.builder(
controller: _scrollController,
scrollDirection: widget.child.scrollDirection,
itemCount: 2,
itemBuilder: (context, index) {
if (index == 0) {
return widget.child;
}
return widget.footer;
},
);
}
}
请注意,它有一个ScrollController, 因为我需要知道页脚对于其他功能是否可见。
问题
widget.child必须包装其内容,否则 :Vertical viewport was given unbounded height. 我尝试widget.child用Wrapor包裹,IntrinsicHeight但它不起作用。我唯一能做的就是做一个assertifwidget.childdoesn't haveshrinkWrap: true。widget.child不必滚动,否则自定义小部件不会滚动,因为widget.child用ListView. 我尝试widget.child用IgnorePointeror包裹,GestureDetector但我无法点击这些项目。我唯一能做的就是做一个assertif thewidget.childphyisicsare notNeverScrollableScrollPhysics。- 如果
widget.child有controller, 因为widget.child不应该是可滚动的,我必须将它传递controller给父级ListView,但我会得到:ScrollController attached to multiple scroll views. 我唯一能做的就是允许自定义小部件接收 acontroller并使assertifwidget.child具有controller.
问题
- 我该如何解决这些问题?
- 这是一个好方法吗?
- 我还可以采取哪些其他方法?