gpt4 book ai didi

flutter - 如何在 SliverAppBar 上重叠 SliverList

转载 作者:IT老高 更新时间:2023-10-28 12:38:24 27 4
gpt4 key购买 nike

我试图在 SliverAppBar 上重叠几个像素的 SliverList。类似于 this post .我希望 FlexibleSpaceBar 中的图像位于 SliverList 的半径范围内。我正在尝试实现以下目标。

enter image description here

我只能这样得到半径。无法将 SliverList 重叠到他的 SliverAppBar 上。 enter image description here

  @override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
floating: false,
expandedHeight: MediaQuery.of(context).size.height * 0.50,
flexibleSpace: FlexibleSpaceBar(
background: Image.network(pet.photos.first)
),
),
SliverList(
delegate: SliverChildListDelegate([
Container(
height: 40,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
),
),
]),
)
],
),
);
}

任何方向都将不胜感激,谢谢!

最佳答案

您可以使用以下小部件,它使用堆栈和滚动监听器来实现类似屏幕截图的效果。它基于 https://pub.dartlang.org/packages/sliver_fab .

小部件:

import 'package:flutter/material.dart';

class DetailScaffold extends StatefulWidget {
final ScrollController controller;
final ScrollPhysics physics;
final List<Widget> slivers;

final double expandedHeight;

/// Changes edge behavior to account for [SliverAppBar.pinned].
///
/// Hides the edge when the [ScrollController.offset] reaches the collapsed
/// height of the [SliverAppBar] to prevent it from overlapping the app bar.
final bool hasPinnedAppBar;

DetailScaffold({
@required this.expandedHeight,
this.controller,
this.physics,
this.slivers,
this.hasPinnedAppBar = false,
}) {
assert(expandedHeight != null);
assert(hasPinnedAppBar != null);
}

@override
_DetailScaffoldState createState() => _DetailScaffoldState();
}

class _DetailScaffoldState extends State<DetailScaffold> {
ScrollController ctrl;

@override
void initState() {
super.initState();

ctrl = widget.controller ?? ScrollController();
ctrl.addListener(() => setState(() {}));
}

@override
void dispose() {
if (widget.controller == null) {
ctrl.dispose();
}

super.dispose();
}

@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
CustomScrollView(
controller: ctrl,
physics: widget.physics,
slivers: widget.slivers,
),
_buildEdge(),
],
);
}

_buildEdge() {
var edgeHeight = 12.0;
var paddingTop = MediaQuery.of(context).padding.top;

var defaultOffset =
(paddingTop + widget.expandedHeight) - edgeHeight;

var top = defaultOffset;
var edgeSize = edgeHeight;

if (ctrl.hasClients) {
double offset = ctrl.offset;
top -= offset > 0 ? offset : 0;

if (widget.hasPinnedAppBar) {
// Hide edge to prevent overlapping the toolbar during scroll.
var breakpoint =
widget.expandedHeight - kToolbarHeight - edgeHeight;

if (offset >= breakpoint) {
edgeSize = edgeHeight - (offset - breakpoint);
if (edgeSize < 0) {
edgeSize = 0;
}

top += (edgeHeight - edgeSize);
}
}
}

return Positioned(
top: top,
left: 0,
right: 0,
child: Container(
height: edgeSize,
decoration: BoxDecoration(
color: Theme.of(context).canvasColor,
borderRadius: BorderRadius.vertical(
top: Radius.circular(12),
),
),
),
);
}
}

使用它:

class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
var expandedHeight = 128.0;

return DetailScaffold(
expandedHeight: expandedHeight,
slivers: <Widget>[
SliverAppBar(
expandedHeight: expandedHeight,
flexibleSpace: FlexibleSpaceBar(
background: Container(color: Colors.purple),
),
),
SliverList(
delegate: SliverChildBuilderDelegate((_, i) {
return ListTile(title: Text('Item $i'));
}, childCount: 50),
),
],
);
}
}

关于flutter - 如何在 SliverAppBar 上重叠 SliverList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54856511/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com