Consider the following example that is intended to render a pinned title bar (with a potentially custom long/short text) inside of a CustomScrollView.
class TitleBar extends StatelessWidget {
TitleBar(this.text);
final String text;
@override
Widget build(BuildContext context) => Text(
text,
style: TextStyle(fontSize: 30),
maxLines: 3,
overflow: TextOverflow.ellipsis,
);
}
class TitleBarDelegate extends SliverPersistentHeaderDelegate {
final String text;
TitleBarDelegate(this.text);
@override
Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) => TitleBar(text);
@override
bool shouldRebuild(TitleBarDelegate oldDelegate) => oldDelegate.text != text;
@override
double get maxExtent => ???;
@override
double get minExtent => maxExtent; // doesn't shrink
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: CustomScrollView(
slivers: [
SliverPersistentHeader(
pinned: true,
delegate: TitleBarDelegate('Potentially very long text'),
),
SliverToBoxAdapter(
child: Text("Foo " * 1000),
),
],
),
),
);
}
}
The question is: How can I compute maxExtent based on the actual TitleBar. The problem is that the actual TitleBar's size depends on the text and is hence to generally computable in advance.
Note that TitleBar might also have some more complex layout than it has in the example above. So the general question is how to 'shrink-wrap' an SliverPersistentHeaderDelegate.