在SliverChildBuilderDelegate下显示内容

时间:2020-11-09 00:13:41

标签: flutter

是否可以在SliverToBoxAdapter下显示小部件(例如Container或普通SliverChildBuilderDelegate)?我有一个条件会打开SliverList,并且一个条件只能有一个父窗口小部件,我需要把它包装起来吗?

代码当前如下所示:

lass Testaaa extends StatelessWidget {
  final bool hasData;

  const Testaaa({Key key, this.hasData}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: DrawerSettings(),
      body: Container(
        child: CustomScrollView(
          slivers: <Widget>[
            AppBarSliver(),
            hasData
                ? SliverList(
                    delegate: SliverChildBuilderDelegate(
                      (BuildContext context, int i) {
                        return ListTile(
                          title: Text('cool'),
                        );
                      },
                    ),
                  ) // I would like to put another Container here that
                    // scrolls with the bottom, underneath the SliverList
                : Container(
                    child: Text('no data'),
                  ),
          ],
        ),
      ),
    );
  }

1 个答案:

答案 0 :(得分:1)

对于传播操作员来说似乎是一件工作

CustomScrollView(
  slivers: <Widget>[
    AppBarSliver(),
    ...hasData
      ? <Widget>[
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int i) {
                return ListTile(
                  title: Text('cool'),
                );
              },
            ),
          ),
          const SliverToBoxAdapter(
            child: SizedBox(
              child: Text('End of the list'),
            ),
          ),
        ]
      : <Widget>[
          const SliverToBoxAdapter(
            child: SizedBox(
              child: Text('no data'),
            ),
          )
      ]
  ],
)

这样,您就可以判断hasData是否为true,如果所有元素都为false,则将其添加到方括号中(带有文本“ End of list”的SliverList和SliverToBoxAdapter),如果为false,则添加所有其他这些部件(仅是SliverToBoxAdapter)