简体   繁体   English

如何在 CustomScrollView 中使用非银色小部件

[英]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.

相关问题 如何在Flutter中的CustomScrollView中使用可禁用的小部件? - How to use a dismissible widget inside a CustomScrollView in Flutter? 使用Sliver小部件滚动问题展开iPhone XR CustomScrollView - Flutter iPhone XR CustomScrollView with Sliver widget(s) scroll issue CustomScrollView Flutter 中的复杂条子 - Complex sliver in CustomScrollView Flutter 在颤动中的 CustomScrollView 中使用 Dismissible Widget 的正确方法是什么? - What is the correct way to use Dismissible Widget inside CustomScrollView in flutter? 自定义 Flutter 小部件以返回两个小部件以与 CustomScrollView / slivers 一起使用 - Custom Flutter widget to return two widgets for use with CustomScrollView / slivers 我们如何在 Sliver App 小部件中添加底部导航栏 - how can we add bottomnavigationbar in Sliver App widget 我怎样才能像孩子一样使用 CustomScrollView - How can I use a CustomScrollView as a child 如何在 CustomScrollView 中控制 CustomScrollView? - How to control CustomScrollView inside CustomScrollView? 获取 Flutter CustomScrollView 内可见/滚动的当前条子 - Get the current sliver that is visible/scrolled inside a Flutter CustomScrollView 如何在 flutter 中使用带有 sliver 应用栏的剪辑路径 - How to use clip path with sliver app bar in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM