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