解决方案 ^^
使用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'),
),
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;
}
}