[英]How to use a non-sliver widget in a CustomScrollView
I am trying to add a Text widget into CustomScrollView but I got issues like the target is not the same.我正在尝试向 CustomScrollView 添加一个 Text 小部件,但我遇到了目标不一样的问题。
This is my widget:这是我的小部件:
@override
Widget build(BuildContext context) {
final double statusBarHeight = MediaQuery.of(context).padding.top;
return Scaffold(
key: scaffoldKey,
body: CustomScrollView(
semanticChildCount: 2,
slivers: <Widget>[
_buildAppBar(context, statusBarHeight),
Text('test')
],
));
}
The _buildAppBar method returns a SliverAppBar. _buildAppBar 方法返回一个 SliverAppBar。
I need to use a Padding widget instead of the text, but I think that it will be like the same, that's the same issue.我需要使用 Padding 小部件而不是文本,但我认为它会是一样的,这是同样的问题。
I found a better way to use non-slivers inside a CustomScrollView, use SliverToBoxAdapter widget.我找到了一种在 CustomScrollView 中使用非条子的更好方法,使用 SliverToBoxAdapter 小部件。 Give your non-sliver widget as child of SliverToBoxAdapter widget and your work done.将您的非 sliver 小部件作为 SliverToBoxAdapter 小部件的子部件并完成您的工作。
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverToBoxAdapter(
child: Stack(
children: <Widget>[
Container(
height: 200,
width: 200,
color: Colors.green,
),
Positioned(
child: Container(color: Colors.yellow),
top: 50,
left: 50,
)
],
),
)
],
),
);
The best answer is not correct, it causes assertion padding == null
.最佳答案不正确,它会导致断言padding == null
。 @blaneyneil wrote the right solution: use to SliverToBoxAdapter. @blaneyneil 写了正确的解决方案:使用 SliverToBoxAdapter。
Once you are working with slivers, you need to use a sliver child wrapping your other non-sliver widgets.一旦你使用了条子,你需要使用一个条子子来包裹你的其他非条子小部件。 Since you want to use Padding
, you can actually take advantage of the SliverPadding
widget, which accepts your Text
as its child.由于您想使用Padding
,您实际上可以利用SliverPadding
小部件,它接受您的Text
作为其子项。
So, instead of having Text('test')
as you have, replace it by SliverPadding(child: Text('test))
instead.因此,不要像您那样使用Text('test')
,而是将其替换为SliverPadding(child: Text('test))
。
There are a few sliver widgets that you may want to be aware of.有一些您可能需要注意的 sliver 小部件。 Take a look here at Collin Jackson's accepted answer and also at Slivers, Demystified article for better understanding. 在此处查看 Collin Jackson 已接受的答案以及Slivers, Demystified文章,以便更好地理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.