I'm starting on Flutter, I have this error that appeared from the moment I created a SliverPersistentHeader. I tried to find an explanation but nothing.
Does the error come from the Sliver ?
I don't have the error regularly but I have to save my work 2 times in a row to have a normal display otherwise the error comes back
Also, is it simply an error when debugging or could it be displayed in the eyes of users ?
My page:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class homepage extends StatefulWidget{
const homepage({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return homepageState();
}
}
const BackgroundColor = const Color(0xFFF2F2F2);
class homepageState extends State<homepage> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
backgroundColor: Colors.white,
body: CustomScrollView(
slivers: <Widget>[
SliverPersistentHeader(
delegate: _topBarContent(),
pinned: true,
),
SliverToBoxAdapter(
child: Container(
color: BackgroundColor,
padding: EdgeInsets.all(20),
child: Text(
"Lorem ipsum"
),
)
)
],
),
);
}
}
const _maxHeaderExtent = 280.0;
const _minHeaderExtent = 100.0;
const _maxIconSize = 150.0;
const _minIconSize = 60.0;
class _topBarContent extends SliverPersistentHeaderDelegate{
@override
Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
// TODO: implement build
final size = MediaQuery.of(context).size;
return Container(
color: Colors.white,
padding: EdgeInsets.all(20),
child: Stack(
children: <Widget>[
Positioned(
top: 50.0,
left: 0,
height: _maxIconSize,
child: Column(
children: <Widget>[
Text(
"My text",
textAlign: TextAlign.start,
style: TextStyle(
fontSize: 20,
),
),
Text(
"My second text",
textAlign: TextAlign.start,
style: TextStyle(
fontSize: 20
),
)
],
),
),
Positioned(
bottom: 20.0,
left: 35.0,
height: _maxIconSize,
child: Icon(
Icons.cloud_outlined,
size: 50,
),
)
],
),
);
}
@override
// TODO: implement maxExtent
double get maxExtent => _maxHeaderExtent;
@override
// TODO: implement minExtent
double get minExtent => _minHeaderExtent;
@override
bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) {
// TODO: implement shouldRebuild
throw UnimplementedError();
}
}
here, the error that tells me an internal file to flutter so the error must come from else:
package flutter/src/widgets/framework.dart failed assertion: line 4479 pos 14
CodePudding user response:
Probably because you haven't implemented shouldRebuild
:
@override
bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) {
// TODO: implement shouldRebuild
throw UnimplementedError();
}
You should return true if anything has changed which requires the header to repaint itself, and false otherwise. In your case it looks like you can just return false
:
@override
bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) {
return false;
}