【问题标题】:How to make scroll in silverList?如何在silverList中进行滚动?
【发布时间】:2021-09-18 20:55:34
【问题描述】:

我正在尝试制作银色应用栏,但遇到了问题。我使用 infitelist 来构建一个列表,因为我发现 infitelist 可以更巧妙地处理列表视图,因此应用程序栏不会像 Blow gif 中所示的那样位于顶部。由于不支持无限列表,我已经用容器包装了它。 下面是代码

        SliverList(
              delegate: SliverChildListDelegate(
                <Widget>[
                       Container(
                         height: 500,
                         child: Column(
                    children: [
                      TopListView(posts: state.posts),
                      Expanded(
                          child: InfiniteList(
                            loadingBuilder: (context) {
                              return const SizedBox(
                                height: 100,
                                child: LoadingIndicator(
                                  message: 'Fetching older posts',
                                ),
                              );
                            },
                            hasError: state.status.isFailure,
                            isLoading: state.status.isLoading,
                            itemCount: state.posts.length,
                            hasReachedMax: state.hasReachedMax,
                            onFetchData: () {
                              context.read<PostsBloc>().add(const PostsRequested());
                            },
                            itemBuilder: (context, index) {
                              final post = state.posts[index];
                              return PostCard(post: post);
                            },
                            errorBuilder: (context) {
                              return SizedBox(
                                height: 100,
                                child: Center(
                                  child: ReloadButton(
                                    onPressed: () {
                                      context
                                          .read<PostsBloc>()
                                          .add(const PostsRequested());
                                    },
                                    message: 'Failed to fetch more posts',
                                  ),
                                ),
                              );
                            },
                            emptyBuilder: (context) {
                              return Center(
                                child: ReloadButton(
                                  onPressed: () {
                                    context
                                        .read<PostsBloc>()
                                        .add(const PostsRequested());
                                  },
                                  message: 'No Posts Found',
                                ),
                              );
                            },
                          ),

                      ),
                    ],
                  ),
                       )
                ],
              ),
            )

我们如何解决这个问题?

【问题讨论】:

    标签: flutter flutter-layout flutter-animation


    【解决方案1】:

    您可以使用SingleChildScrollView 代替Container

    【讨论】:

    • 不,它不会工作,因为它说渲染框有大小
    • SliverList( delegate: SliverChildListDelegate( [ SingleChildScrollView( child: Column( children: [ TopListView(posts: state.posts), Expanded( child: InfiniteList()
    • 这是错误════════渲染库捕获的异常═════════════════════════ ════════ RenderBox 未布置:RenderRepaintBoundary#e0c4b relayoutBoundary=up4 NEEDS-PAINT 'package:flutter/src/rendering/box.dart':断言失败:第 1930 行 pos 12:'hasSize' 相关导致错误的小部件是 SliverList lib\...\posts\posts_page.dart:38 ═════════════════════════════════ ══════════════════════════════════════════════════ ═════ 调度程序库捕获的异常═════════════════════════════════空值检查运算符价值
    【解决方案2】:

    实际上,您只需要CustomScrollViewSliverAppBar。 这是一个小部件示例:

    Scaffold(
          body: CustomScrollView(
            slivers: [
              SliverAppBar(
                pinned: false,
                snap: true,
                floating: true,
                expandedHeight: 160.0,
                flexibleSpace: const FlexibleSpaceBar(
                  title: Text('SliverAppBar'),
                  background: FlutterLogo(),
                ),
              ),
              SliverList(
                delegate: SliverChildBuilderDelegate(
                  (BuildContext context, int index) {
                    return Container(
                      color: index.isOdd ? Colors.white : Colors.black12,
                      height: 100.0,
                      child: Center(
                        child: Text('$index', textScaleFactor: 5),
                      ),
                    );
                  },
                  childCount: 20,
                ),
              ),
            ],
          ),
        );
    

    If you want to know more this is SliverAppBar docs

    【讨论】:

    • 我想使用无限列表而不是那个,这是导致问题的原因
    • 您可以制作适合您自己用例的无限列表
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-15
    • 2022-12-19
    • 2011-01-21
    • 2014-12-26
    • 2019-11-06
    • 2017-10-04
    • 2023-03-14
    相关资源
    最近更新 更多