flutter - [Flutter]SliverAppBar在NestedScrollView中向下滚动时如何保持flexibleSpace展开?
问题描述
在 NestedScrollView 中向下滚动时如何保持 SliverAppBar 展开?
我想更改名为“_locked”的标志来锁定 SliverAppBar 的高度,但我不知道该怎么做。
可能需要使用 SliverPersistentHeader 小部件?
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final double expanded = 200;
bool _locked = false;
@override
Widget build(BuildContext context) {
final top = MediaQuery.of(context).padding.top;
return Scaffold(
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return [
SliverAppBar(
pinned: true,
floating: true,
title: Text(widget.title),
expandedHeight: expanded - top,
flexibleSpace: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Container(height: expanded, color: Colors.red);
},
),
),
];
},
body: ListView(
children: ListTile.divideTiles(
context: context,
tiles: List.generate(
50,
(index) => ListTile(
title: Text('Index: $index'),
))).toList(),
),
),
floatingActionButton: FloatingActionButton(
child: Icon(_locked ? Icons.lock_open : Icons.lock_outline),
onPressed: () {
/// Switch the flag
setState(() {
_locked = !_locked;
});
},
),
);
}
}
解决方案
我相信你想绑定_locked
到.SliverAppBar
我链接的文档中包含一个 gif 示例!
推荐阅读
- c - 你可以在 C 头文件中将 typedef 的变量重新转换为另一种类型吗?
- database - 两台计算机之间的 Oracle DB 连接
- php - 检索、调整大小和显示 MySQL BLOB 图像
- python - Django tuple/choices 字段未翻译
- nan - 是否可以在 Code.org AppLab 中检查 NaN?
- python - odoo 12. 我不能在 many2one 字段上做动态过滤
- c# - Google Cloud Endpoints 无效的 JWT 签名 (.NET Core)
- python - 在一列中搜索字符串并提取匹配行Python的所有列
- jupyter-notebook - Azure HDInsight Jupyter 笔记本不工作
- javascript - Mat-checkbox [checked]=“false”不起作用