简体   繁体   English

具有 SliverFillRemaining 内容的 CustomScrollView 条不滚动

[英]CustomScrollView slivers with contents in SliverFillRemaining not scrolling

I am trying to create a flutter screen using the CustomScrollView and Sliver widgets where the SliverFillRemaining has a Text widget that can scroll down like reading scroll-able text content.我正在尝试使用 CustomScrollView 和 Sliver 小部件创建一个颤动屏幕,其中 SliverFillRemaining 有一个 Text 小部件,可以像阅读可滚动的文本内容一样向下滚动。

I can achieve this by wrapping the Text within the SingleChildScrollView however i want to have the functionality of a header that can automatically hide using SliverAppBar我可以通过将文本包装在 SingleChildScrollView 中来实现这一点,但是我希望拥有可以使用 SliverAppBar 自动隐藏的标题的功能

How can i achieve the behaviour similar to SingleChildScrollView?如何实现类似于 SingleChildScrollView 的行为?

When i add Text widget inside SliverFillRemaining it only scrolls down until the SliverAppBar is hidden and then stops.当我在 SliverFillRemaining 中添加 Text 小部件时,它只会向下滚动,直到 SliverAppBar 被隐藏然后停止。

var scaffold = Scaffold(
    body: CustomScrollView(
        slivers: <Widget>[
            SliverAppBar(
                pinned: false,
                snap: false,
                floating: true,
                expandedHeight: 40.0,
                flexibleSpace: FlexibleSpaceBar(
                    title: Text(widget.path.title),
                ),
            ),
            SliverFillRemaining(
                hasScrollBody: true,
                child: Scrollbar(
                    child: Text(
                        data, //this variable has a huge string.
                        textAlign: TextAlign.left,
                        style: new TextStyle(
                            height: 1.5,
                            fontFamily: widget.fontFamily,
                            fontSize: widget.fontSize,
                            fontWeight: (widget.bold)? FontWeight.bold:FontWeight.normal,
                        ),
                    ),
                ),
            )
        ]
    ),

I was able to solve my issues with the following approach.我能够通过以下方法解决我的问题。

Instead of using the SliverFillRemaining widget i could use the SliverToBoxAdapter to achieve what i needed.我可以使用 SliverToBoxAdapter 来实现我需要的,而不是使用 SliverFillRemaining 小部件。 i then had the Text widget inside that.然后我在里面有文本小部件。

see example below.请参见下面的示例。

     var scaffold = Scaffold(
          body: Scrollbar(
            child: CustomScrollView(controller: _controller, slivers: <Widget>[
              SliverAppBar(
                actions: <Widget>[
                  IconButton(
                    icon: Icon(Icons.settings),
                    onPressed: () {
                      setState(() {
                        if (_expandedHeight == 40) {
                        _expandedHeight = 500;
                        _title = AppConstants.EMPTY_STRING;
                        }
                        else {
                        _expandedHeight = 40;
                        _title = widget.path.title;
                        }
                      });
                    },
                  )
                  ],
                pinned: false,
                snap: false,
                floating: true,
                expandedHeight: _expandedHeight,
                flexibleSpace: FlexibleSpaceBar(
                  title: Text(_title),
                  background: OptionsPage(),
                ),
              ),
              SliverToBoxAdapter(
                child: Scrollbar(
                  child: Padding(
                    padding: const EdgeInsets.all(AppConstants.READER_PADDING),
                    child: MediaQuery(
                      data: MediaQuery.of(context).copyWith(
                        textScaleFactor: StoreProvider.of<AppState>(context).state.textScaleValue,
                      ),
                      child: Text(
                        data,
                        textAlign: TextAlign.left,
                        style: new TextStyle(
                          height: 1.5,
                          fontFamily: getLanguageMenuItemValueByName(StoreProvider.of<AppState>(context).state.languageName).fontName,
                          fontSize: getLanguageMenuItemValueByName(StoreProvider.of<AppState>(context).state.languageName).fontSize,
                          fontWeight:
                              (StoreProvider.of<AppState>(context).state.bold) ? FontWeight.bold : FontWeight.normal,
                        ),
                      ),
                    ),
                  ),
                ),
              )
            ]
          ),
        ),

SliverFillRemaining widget to add the rest of the UI so we can see it scrolling. SliverFillRemaining 小部件添加 UI 的其余部分,以便我们可以看到它滚动。 Add this as the second sliver, in the CustomScrollView under the SliverAppBar.在 SliverAppBar 下的 CustomScrollView 中将其添加为第二个 sliver。

Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            expandedHeight: 200,
            pinned: true,
            flexibleSpace: FlexibleSpaceBar(
              title: Text('FilledStacks'),
            ),
          ),

    SliverFillRemaining(
      child: Column(
        children: List<int>.generate(6, (index) => index)
            .map((index) => Container(
                  height: 40,
                  margin: EdgeInsets.symmetric(vertical: 10),
                  color: Colors.grey[300],
                  alignment: Alignment.center,
                  child: Text('$index item'),
                ))
            .toList(),
      ),
    )
        ],
      ),
    ),

对于那些找不到解决方案的人,您只需将hasScrollBody: true添加到 SliverFillRemaining

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:有效地滚动画布的内容? - Android: Efficiently scrolling the contents of a Canvas? ScrollView仍然使用隐藏内容滚动 - ScrollView still scrolling with hidden contents 在滚动列表视图时加载内容而不会中断列表视图 - Load contents on scrolling the listview without interrupting the listview Titanium Android-Tableview滚动弄乱了内容 - Titanium Android - Tableview scrolling messes up contents 滚动时更改列表视图内容 - Changing List View contents when scrolling 在Android中以循环方式自动滚动滚动文本视图的内容 - Automatic scrolling of contents of a text view in scroll in a circular fashion Android 在 RecyclerView 中检测到不一致,如何在滚动时更改 RecyclerView 的内容 - Inconsistency detected in RecyclerView, How to change contents of RecyclerView while scrolling webview 在 customScrollView 中全页加载 Flutter - webview full page load in customScrollView Flutter ViewPager + PagerSlidingTabStrip:立即从选项卡导航,滚动时不显示内容 - ViewPager + PagerSlidingTabStrip: Navigating from a tab immediately without displaying contents while scrolling 向下滚动电子邮件内容时,Android Gmail应用程序如何将详细信息视图“固定”到操作栏? - How does the Android Gmail app “pin” the details view to the action bar when scrolling down the email's contents?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM