gpt4 book ai didi

具有水平滚动和粘性标题的 Flutter SliverList

转载 作者:行者123 更新时间:2023-12-05 04:32:58 25 4
gpt4 key购买 nike

我已经尝试了一段时间,但我无法让它工作。

我想在带有 CupertinoSliverNavigationBarCustomScrollView 中创建一个 ListView 。 SliverList 必须可以水平滚动。此外,它应该有一个标题行,该行固定在 View 的顶部。

请在此处查看示例:https://dartpad.dev/?id=38c0825f27c1b3395dd22794a2567e64 .请注意,正文可以水平滚动。

我想使红色标题行具有粘性,以便在滚动蓝色内容时始终保持可见。

问题是标题行必须在 SingleChildScrollViewSizedBox 内,否则它不会有所需的宽度。但是一旦它在里面,我就不能再让它变粘了。

非常感谢任何指点。

最佳答案

解决方案^^使用 SliverPersistentHeader 使红色标题行具有粘性,并使用 SliverList 而不是 ListView。

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'dart:math' as math;

class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);


@override
Widget build(BuildContext context) {
final scrollController = ScrollController();
return MaterialApp(
home: Scaffold(
body: CustomScrollView(
slivers: [
const CupertinoSliverNavigationBar(
largeTitle: Text('Search'),
),
//CupertinoSliverRefreshControl(),
SliverPersistentHeader(
pinned: true,
delegate: _SliverAppBarDelegate(
minHeight: 20.0,
maxHeight: 30.0,
child: Container(
color: Colors.red, child: Center(child:
Text("red header "))),
),
),

SliverList(
delegate: SliverChildListDelegate(
List.generate(80, (index) => Container(
padding: const EdgeInsets.all(5),
color: Colors.blueAccent.shade100,
child: const Text('bar'),
)),
),
),

],
),
),
);
}
}

class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
_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: child);
}

@override
bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
return maxHeight != oldDelegate.maxHeight ||
minHeight != oldDelegate.minHeight ||
child != oldDelegate.child;
}
}

关于具有水平滚动和粘性标题的 Flutter SliverList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71555118/

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