flutter - 如何将小部件添加到 Sliver 应用栏区域?
问题描述
我想在 Sliver AppBar 区域添加小部件,让它感觉更自然。AppBar 可以自由添加图像,但它是否具有添加更多小部件的功能?
我专注于如何在 sliver appbar 上使用那些CircleAvatar和Text小部件。
解决方案
您将无法使用 SliverAppBar 做到这一点:/。
您应该做的是将 SliverPersistentHeader 与 SliverPersistentHeaderDelegate 一起使用。
您将不得不编写更多代码,但不要写太多:)。
例子:
slivers = [
SliverPersistentHeader(
pinned: True,
delegate: _SliverAppBarDelegate(
minHeight: 60.0,
maxHeight: 250.
),
),
SliverList(
delegate: SliverChildListDelegate([
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: Column(
children: ...
),
)
]),
),
];
...
class _SliverAppBarDelegate extends x {
_SliverAppBarDelegate({
@required this.minHeight,
@required this.maxHeight,
@required this.child,
});
final double minHeight;
final double maxHeight;
final Widget child;
@override
double get minExtent => minHeight;
@override
double get maxExtent => math.max(maxHeight, minHeight);
@override
Widget build(
BuildContext context,
double shrinkOffset,
bool overlapsContent)
{
return new SizedBox.expand(
child: LayoutBuilder(
builder: (_, constraints) {
DO WHAT YOU WANT HERE !
}
)
);
}
@override
bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
return maxHeight != oldDelegate.maxHeight ||
minHeight != oldDelegate.minHeight ||
child != oldDelegate.child;
}
}
推荐阅读
- javascript - 如何在 ES6 中使用可选参数扩展构造函数+方法?
- javascript - 为两个列表值取一个输入
- google-cloud-platform - 在 Cloud Build CI/CD 管道中发出 curl 请求
- reactjs - 使用带有 getFieldDecorator 的自定义类(React,Ant Design)
- python - jit 函数中是否可以包含函数列表?
- django - Pycharm 的 Django `Run manage.py Task` 控制台不适用于交互式命令?
- java - 在 EditText 和键盘之间添加边距
- python - 一个字典的类似字典的字符串列表
- jquery - 如何使用 jquery 删除 dropify 文件上传元素
- python - 从决策树算法生成的模型中进行预测