flutter - Why does Sliver App Bar's flexibleSpace parameter have to take a Widget with a const constructor?
问题描述
I am trying to build a custom scroll view which contains a sliver app bar to achieve something similar to what's shown here:
https://medium.com/@diegoveloper/flutter-collapsing-toolbar-sliver-app-bar-14b858e87abe
However I want to have the word Search and below it I want 3 IconButtons Evenly spaced, when the page (CustomScrollView) is scrolled, I want the 3 IconButtons to be pinned to the top of the SliverAppBar and the Search Text to disappear...
I Tried to achieve the above via the following code:
class SearchPage extends StatelessWidget {
const SearchPage();
@override
Widget build(BuildContext context) {
return CustomScrollView(slivers: <Widget>[
const SliverAppBar(
pinned: true,
expandedHeight: 250.0,
flexibleSpace: _buildSliverAppBarFlex(),
);
}
Widget _buildSliverAppBarFlex() {
return Container(
child: Column(
children: <Widget>[
Text("Search", style: TextStyle(fontSize: 24.0,
color: Colors.white,
fontWeight: FontWeight.bold)),
Row(children: <Widget>[
IconButton(icon: Icon(Icons.flight)),
IconButton(icon: Icon(Icons.hotel)),
IconButton(icon: Icon(Icons.drive_eta))
])
],
)
);
}
}
However I get a warning that flexibleSpace must take a const constructor widget and that the _buildSilverAppBarFlex Widget I've made is not - I cannot add const or final to it either... Any ideas how I can achieve what I want?
解决方案
The warning comes because you are using const
ahead of your SliverAppBar
, remove this, and warning will be gone.
So, instead of this
const SliverAppBar(...)
Use this.
SliverAppBar(...)
If you want to use const
there, make sure your FlexibleSpaceBar
is also a const
then.
推荐阅读
- ethereum - 以太坊 Smat 合约阻塞
- python - 在 mac 上为 python3 安装 venv 时出错
- php - PHP 脚本耗时过长。需要有人看看我的代码来优化它
- javascript - 用 JS 比较图片
- c - 我如何循环回到c程序的开头?
- php - 当我上传所有内容时,为什么我的包含/必需的变量文件停止工作?
- javascript - Photoshop - 有没有办法用脚本而不是导出数据集去下一个数据集
- html - CSS背景不显示颜色
- python - 如何将熊猫数据框转换为嵌套的 json
- flutter - Datepicker 的问题,如果您尝试更改月份或年份,它就会消失